Differentiate a null value from an absent one in JsonDeserializer
See original GitHub issueHi,
I have a class representing a field update:
public class OptionalUpdate<T> {
private final Optional<T> value;
private Present(Optional<T> value) {
this.value = value;
}
public boolean isPresent() {
return value != null;
}
public Optional<T> getValue() {
return value;
}
public static <T> OptionalUpdate<T> unset() {
return new OptionalUpdate<>(Optional.empty());
}
public static <T> OptionalUpdate<T> set(T value) {
return new OptionalUpdate<>(Optional.of(value));
}
public static <T> OptionalUpdate<T> empty() {
return new OptionalUpdate<>(null);
}
}
and an entity update:
public class UserUpdate {
private final OptionalUpdate<String> nickname;
@JsonCreator
public UserUpdate(OptionalUpdate<String> nickname) {
this.nickname = nickname;
}
public OptionalUpdate<String> getNickname() {
return nickname;
}
}
I would like to handle three types of updates:
- to set a nickname to “lolcontrol”:
{ "nickname": "lolcontrol" }
- to unset a nickname:
{ "nickname": null }
- not to update a nickname:
{ }
How relevant is it to add a new method to JsonDeserializer
that can be overridden in the custom deserializer?
/**
* Method called to determine value to be used for absent values.
* Usually this is same as {@link #getNullValue} (which in turn
* is usually simply Java null), but it can be overridden
* for types.
*<p>
* Default implementation simple calls {@link #getNullValue} and
* returns value.
*/
public T getAbsentValue() {
return getNullValue();
}
Issue Analytics
- State:
- Created 9 years ago
- Reactions:20
- Comments:17 (7 by maintainers)
Top Results From Across the Web
How to differentiate null value fields from absent fields in ...
If you want to differentiate null value fields from absent fields the most generic method will be using Map or JsonNode instead of...
Read more >how to exclude null value properties from JSON serialization
Jackson offers two main alternatives to exclude null values from JSON serialization: At single class level, using the JsonInclude annotation ...
Read more >Jackson JSON - @JsonInclude NON_ABSENT Example
@JsonInclude(NON_ABSENT) can be used to exclude null values and values that are "absent". Here absent value means a non-null referential ...
Read more >[Solved]-How to deserialise a field and tell the difference ...
Coding example for the question How to deserialise a field and tell the difference between the value being null or absent?-kotlin.
Read more >Ignore Null Fields with Jackson - Baeldung
1. Overview. This quick tutorial is going to cover how to set up Jackson to ignore null fields when serializing a java class....
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 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
This is indeed an interesting problem. @cowtowncoder maybe you can point me to where the discussion should take place.
This becomes a more important issue when patching a resource using JSON Merge Patch. If I want to update a
FooBar
resource on the server, I can send via HTTPPATCH
:This would update
FooBar.foo
on the server to 123. But what would it do with the existingFooBar.bar
value, which might already be set to789
? If my Java DTO object assumes thatbar
here isnull
because it is absent, it would setFooBar.bar
tonull
on the server. But in reality I want to recognize thatbar
wasn’t specified at all, so thatFooBar.bar
should remain unchanged.Where is the best place to discuss this issue further? Thanks.
Ah, maybe I can use the behavior indicated in https://github.com/FasterXML/jackson-datatype-jdk8/issues/2 to set
Optional<>
fields defaulting tonull
, that are let asnull
if no value is specified in the JSON. I’ll try that.