1

I've searched and found Jackson ObjectMapper throwing NullPointerException even with NON_NULL, but I don't have control of the class to change my setter.

I have am being given

{... "fieldNames": null,...}

and am supposed to deserialize it to

Collection<String> fieldNames

I don't have control of the class or the json I'm getting.

Is there some setting I can use to handle for this? I've looked at DeserializationFeature, but could not find it

johnnyB
  • 77
  • 1
  • 10

1 Answers1

1

You can use mix-ins when you don't control the class you are deserializing. You don't mention the name of the class containing Collection<String> fieldNames so lets assume it's called Fields. Then create a new class:

class FieldsMixin {
    @JsonSetter(nulls = Nulls.SKIP)
    Collection<String> fieldNames;
}

and add the mixin class to your ObjectMapper associating it with the original unmodified class:

mapper.addMixIn(Fields.class, FieldsMixin.class);

This is a new feature in Jackson 2.9 and as you guess it will skip calling a setter method or otherwise set a field if the value in JSON is null. Documentation

Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82