On Tue, May 8, 2018 at 11:29 AM, David Karr <[email protected]> wrote: > I just joined a project with a lot of existing code. The following is a > modified example of a pattern I see a lot: > @JsonInclude(JsonInclude.Include.NON_NULL) > @JsonPropertyOrder({ > "things" > }) > public class CodeRequest { > > @JsonProperty("things") > private List<Thing> things = null; > @JsonIgnore > private Map<String, Object> additionalProperties = new HashMap<>(); > > @JsonProperty("things") > public List<Thing> getThings() { > return things; > } > > @JsonProperty("things") > public void setThings(List<Thing> things) { > this.things = things; > } > > I have a problem with how they have redundant @JsonProperty annotations on > the variable, getter, and setter, but that's a different issue. > > In many places where deserialized instances of this are iterated over, > people often check to see if the list entry is null, to avoid NPEs. > > My question is, if Jackson is the only framework creating this instance and > writing to it, is there any way someone could produce JSON that would result > in null list entries? Does the "NON_NULL" reference at the top prevent any > of that?
No, `NON_NULL` only affects serialization, meaning that instead of outputting entry for property with value of `null`, entry is skipped and nothing is written for that property. On input side (reading, deserialization), Jackson 2.9 does add a new mechanism for avoiding use of `null`s: with `@JsonSetter(nulls = ...)`. This blog post: https://medium.com/@cowtowncoder/jackson-2-9-features-b2a19029e9ff (see "Null replacement...") talks a bit about what can be done. Note that this feature can work on per-property, per-type (values of specific type) and global default level, but there is no way to add annotation on POJO class to mean "for all properties contained". I hope this helps, -+ Tatu +- -- You received this message because you are subscribed to the Google Groups "jackson-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. For more options, visit https://groups.google.com/d/optout.
