Skip to content

Instantly share code, notes, and snippets.

@Mahoney
Last active November 7, 2022 10:39
Show Gist options
  • Select an option

  • Save Mahoney/aa8d2b228d1f2c38b68495331b8a272c to your computer and use it in GitHub Desktop.

Select an option

Save Mahoney/aa8d2b228d1f2c38b68495331b8a272c to your computer and use it in GitHub Desktop.
Issue with getting null in the right place with Jackson
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Objects;
import static com.fasterxml.jackson.annotation.Nulls.AS_EMPTY;
import static java.util.Objects.requireNonNull;
public class Main {
public static final class Wrapper {
@Nonnull private final String value;
@Nonnull private final Inlined inlined;
public @JsonCreator Wrapper(
@Nonnull @JsonProperty("value") String value,
@Nonnull @JsonProperty("inlined") @JsonSetter(nulls = AS_EMPTY)
Inlined inlined
) {
this.value = requireNonNull(value);
this.inlined = requireNonNull(inlined);
}
public @Nonnull String getValue() { return value; }
public @Nonnull Inlined getInlined() { return inlined; }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Wrapper wrapper = (Wrapper) o;
return value.equals(wrapper.value) && inlined.equals(wrapper.inlined);
}
@Override
public int hashCode() {
return Objects.hash(value, inlined);
}
@Override
public String toString() {
return "Wrapper{" +
"value=" + value + ", " +
"limit=" + inlined +
'}';
}
}
public static final class Inlined {
@Nullable @JsonValue private final Integer inlined;
public Inlined(@Nullable Integer inlined) {
this.inlined = inlined;
}
public Inlined() {
this(null);
}
@Nullable public Integer getLimit() { return inlined; }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Inlined inlined1 = (Inlined) o;
return Objects.equals(inlined, inlined1.inlined);
}
@Override
public int hashCode() { return Objects.hash(inlined); }
@Override
public String toString() { return inlined != null ? inlined.toString() : null; }
}
private static final ObjectMapper objectMapper = JsonMapper.builder().build();
public static void main(String[] args) throws JsonProcessingException {
checkSerializeDeserialize(new Wrapper("whatever", new Inlined(1)), "1");
checkSerializeDeserialize(new Wrapper("whatever", new Inlined(null)), "null");
}
private static void checkSerializeDeserialize(Wrapper original, String inlined) throws JsonProcessingException {
String json = objectMapper.writeValueAsString(original);
System.out.println(json);
assert json.equals("{\"value\":\"whatever\",\"inlined\":"+inlined+"}");
Wrapper remade = objectMapper.readValue(json, Wrapper.class);
System.out.println(remade);
assert remade.equals(original);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment