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.

Differentiate a null value from an absent one in JsonDeserializer

See original GitHub issue

Hi,

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:

  1. to set a nickname to “lolcontrol”: { "nickname": "lolcontrol" }
  2. to unset a nickname: { "nickname": null }
  3. 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:closed
  • Created 9 years ago
  • Reactions:20
  • Comments:17 (7 by maintainers)

github_iconTop GitHub Comments

6reactions
garretwilsoncommented, Apr 29, 2018

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 HTTP PATCH:

{
  "foo": 123
}

This would update FooBar.foo on the server to 123. But what would it do with the existing FooBar.bar value, which might already be set to 789? If my Java DTO object assumes that bar here is null because it is absent, it would set FooBar.bar to null on the server. But in reality I want to recognize that bar wasn’t specified at all, so that FooBar.bar should remain unchanged.

Where is the best place to discuss this issue further? Thanks.

3reactions
garretwilsoncommented, Apr 29, 2018

Ah, maybe I can use the behavior indicated in https://github.com/FasterXML/jackson-datatype-jdk8/issues/2 to set Optional<> fields defaulting to null, that are let as null if no value is specified in the JSON. I’ll try that.

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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