On Sat, Mar 24, 2018 at 9:14 AM, Darius <[email protected]> wrote: > I have a set of POJOs where all the fields are Strings. I am deserializing > JSON into the POJO, and the JSON can have some null values. > Is there a single setting that can make these nulls be deserialized as > strings, based on the field of the POJO into which they're being moved? > Do I need to write a Custom Deserializer to do this? > > I know I can handle it "on the way out" of the POJO, but I'd like to have a > single flag that turns on this behavior during deserialization.
Jackson 2.9 introduced something that lets you do just this. I wrote about this in https://medium.com/@cowtowncoder/jackson-2-9-features-b2a19029e9ff under "Null replacement/error/skipping", and you can apply it either on specific properties like: @JsonSetter(nulls=Nulls.AS_EMPTY) // or on getter, setter, whatever public String name; but also for specific type you can default globally, with: mapper.configOverride(String.class) .setSetterInfo(JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY); It is also possible to specify other behaviors like failing on encountering null, or skipping value (leaving whatever default POJO property has). Definition of empty value depends on deserializer in question; for `StringDeserializer` that is empty String. 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.
