Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Optional deserialization for "absent" value as Java null (like other Reference types), not "empty" #251

Closed
cowtowncoder opened this issue Sep 22, 2022 · 1 comment

Comments

@cowtowncoder
Copy link
Member

So, for case like:

public class OptionalnBean {
   Optional<String> value;

   @JsonCreator
   public OptionalnBean(@JsonProperty("value") Optional<String> value) {
     this.value = value;
   }
}

with JSON like

{ }

current handling would assign Optional.empty() to value. But other reference types, like AtomicReference will instead assign null. This is to differentiate case of reading JSON like:

{ "value" : null }

in which value for all reference types becomes "empty" value (new AtomicReference<>, Optional.empty()).

This should be fixed for 2.14. But since this is a behavioral change it is also necessary to add configurability to allow old behavior, at least for 2.x.
And to maximize backwards compatibility we probably have to default to old and (IMO) sub-optimal handling.

@cowtowncoder
Copy link
Member Author

Ok, to configure new behavior -- absent (missing) Optionals becoming Java nulls you need to configure module, register it. So:

        Jdk8Module module = new Jdk8Module()
                .configureReadAbsentAsNull(true);
        final ObjectMapper mapper = JsonMapper.builder()
                .addModule(module)
                .build();

This will be in 2.14.0 release.

@cowtowncoder cowtowncoder changed the title Optional deserialization for "absent" value should be null (like other Reference types), not "empty" Allow Optional deserialization for "absent" value as Java null (like other Reference types), not "empty" Sep 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant