Skip to content

Differentiate a null value from an absent one in JsonDeserializer #578

@antonbobukh

Description

@antonbobukh

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(); }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions