custom deserialization with absent Optional returns null
See original GitHub issueWhen using a custom deserialization for an Optional field, then the absence of that field is returning null
instead of Optional.empty()
.
I reproduced with my own test method in the test class OptionalTest.java
:
public void testWithCustomDeserializerIfOptionalAbsent() throws Exception
{
assertEquals(Optional.empty(), MAPPER.readValue("{}",
CaseChangingStringWrapper.class).value);
assertEquals(Optional.empty(), MAPPER.readValue(aposToQuotes("{'value':null}"),
CaseChangingStringWrapper.class).value);
}
The first check fails, because of Jackson setting the value
field to null
.
Issue Analytics
- State:
- Created 6 years ago
- Comments:11 (11 by maintainers)
Top Results From Across the Web
Jackson deserializes absent optionals as nulls instead of ...
I've noticed that deserializing a json string to this object ... As you can notice the optional parameter is absent from this json...
Read more >Using Optional with Jackson - Baeldung
In this case, isPresent() is a public getter on the Optional class. This means it will be serialized with a value of true...
Read more >Jackson - Ignoring Null, Empty and Absent Values
Learn to serialize the fields and ignore NULL, empty and absent values using Jackson. In this tutorial, we will learn the difference between...
Read more >Java 8 Optional Class - GeeksforGeeks
Additional methods that depend on the presence or absence of a contained value are provided, such as orElse() which returns a default value...
Read more >DeserializationFeature (jackson-databind 2.9.0 API) - FasterXML
If disabled, and if property is NOT marked as required, missing Creator properties are filled with null values provided by deserializer for the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top 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
Finally I found the root cause of my misunderstanding. The deserialization was inconsistent amongst my POJOs.
I just realized that this is because of Lombok generating the
@ConstructorProperties
to the@AllArgsConstructor
. Lombok does that even if the constructor is implicit by@Builder
, and even if access level is set toPRIVATE
explicitely.Jackson calls this annotated constructor always with
Optional.empty()
values for all absent fields that are of typeOptional
. Even if the constructor is private. Hence my POJOs behave differently when defined with or without Lombok’s@AllArgsConstructor
.Similar issues have been discussed last year already, e.g. https://github.com/FasterXML/jackson-databind/issues/618 Not sure, if someone actually realized before that the different behavior for
Optional
fields comes from Lombok’s generated@ConstructorProperties
.Yes, there is special tracking for members coming via creators, since they must be buffered anyway (unlike field/setter assigned ones). There is also an issue (quite old) for trying to extend optional handling of missing values to all properties. Its implementation has not started, there is some hope it could be done with Jackson 3.x.
So I think it can be considered an implementation limitation at this point.
Will close.