question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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 nulls to:

  1. Ignore value (avoid calling setter)
  2. Fail (throw exception)
  3. 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:closed
  • Created 7 years ago
  • Reactions:13
  • Comments:23 (12 by maintainers)

github_iconTop GitHub Comments

6reactions
raindevcommented, Dec 28, 2016

Is there any ongoing progress to be able to deserialize nulls to empty collections? We regularly suffer from NullPointerExecptions in our API clients. It would be great to solve the issue systematically.

1reaction
cowtowncodercommented, Dec 8, 2017

@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 implementing JsonDeserializer.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).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found