Use `@JsonSetter(nulls=...)` to specify handling of `null` values during deserialization
See original GitHub issue(note: replaces #1269, earlier #988 and #830)
It looks like there is strong desire to define alternate methods for handling incoming null
values; instead of just assigning as Java null
s to:
- Ignore value (avoid calling setter)
- Fail (throw exception)
- Convert to a default value
Since applicability is probably not universal for all cases (that is, different types could well follow different rules), it seems that approach similar to @JsonInclude
would work.
However, I don’t think use of @JsonInclude
itself works here, as its settings are partially output-specific; so ideas are reusable, implementation not.
Instead, I think use of existing @JsonSetter
by adding:
Nulls
value enumeration for a small set of standard behaviors (and perhaps, in future, for custom handler?)nulls
property for dealing with value itself (POJO, Collection, wrapper type)contentNulls
for contents of structured types: array/collection elements, Map values, POJO properties
Changes to annotation are simple; changes to deserializers would be sizable.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:13
- Comments:23 (12 by maintainers)
Top Results From Across the Web
Jackson deserializer - change null collection to empty one
In 2.9 the defaults can be configured using ObjectMapper , like this mapper.setDefaultSetterInfo(JsonSetter.Value.forContentNulls(Nulls.AS_EMPTY)); ...
Read more >Nulls (Jackson-annotations 2.9.0 API) - FasterXML
Enumeration used with JsonSetter (for properties `nulls` and `contentNulls`) to define how explicit `null` values from input (if input format has the ...
Read more >Jackson 2.9 features - cowtowncoder - Medium
This means that the only way to change handling of nulls has been to define a setter in which null value is changed...
Read more >Ignore Null Fields with Jackson - Baeldung
This quick tutorial is going to cover how to set up Jackson to ignore null fields when serializing a java class.
Read more >Jackson JSON - Using @JsonProperty, @JsonSetter and ...
Handling Nulls with @JsonSetter. Besides renaming , the annotation @JsonSetter can be used to specify how 'null' values in JSON should be ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Is there any ongoing progress to be able to deserialize
null
s to empty collections? We regularly suffer fromNullPointerExecptions
in our API clients. It would be great to solve the issue systematically.@Tharsalbird please use mailing lists for usage questions in future.
But I think what you are asking is possible by using
Nulls.AS_EMPTY
value and then implementingJsonDeserializer.getEmptyValue(...)
method. Luckily for POJOs it will work already, since this method is implemented to try to create an instance using default (0-arg) constructor (if one exists).