Code Monkey home page Code Monkey logo

Comments (5)

cowtowncoder avatar cowtowncoder commented on July 30, 2024 2

Right, "NULL" means exactly and only Java null. Not Optional.empty(). This is intentional.
There is also value NON_ABSENT which would include Optional (and equivalent, AtomicReference) without content, but not empty Collections.

So if you want absent optionals to work like Java nulls, use setting of NON_ABSENT.

from jackson-modules-java8.

sparty02 avatar sparty02 commented on July 30, 2024

Ah! I think I figured it out. I found the NON_EMPTY serialization inclusion, which is what Optional types would use to determine whether to be omitted. However, I didn't want to necessarily use NON_EMPTY for everything, so I used config overrides to only apply it to Optional types. The following is what I came up with (modified main method).

Is this the best way to approach it? Should Optional types be handled like this by default in the Jdk8Module?

   public static void main(String[] args) throws Exception {
        Model model = new Model();
        model.setId(OptionalLong.empty());
        model.setOrder(null);
        model.setName(Optional.ofNullable(null));
        model.setType(null);

        ObjectMapper mapper = new ObjectMapper()
            .registerModule(new Jdk8Module())
            .setSerializationInclusion(NON_NULL);

        mapper.configOverride(Optional.class)
            .setIncludeAsProperty(JsonInclude.Value.construct(JsonInclude.Include.NON_EMPTY, null));

        mapper.configOverride(OptionalLong.class)
            .setIncludeAsProperty(JsonInclude.Value.construct(JsonInclude.Include.NON_EMPTY, null));

        mapper.writeValue(System.out, model);
    }

from jackson-modules-java8.

sparty02 avatar sparty02 commented on July 30, 2024

Awesome, thanks for the heads up on NON_ABSENT, works great.

from jackson-modules-java8.

cowtowncoder avatar cowtowncoder commented on July 30, 2024

Np! This is an area that has been slowly but steadily improved; good to hear it works as expected!

from jackson-modules-java8.

garethsb avatar garethsb commented on July 30, 2024

@cowtowncoder Apologies for commenting on a closed issue, but this seems relevant.

I've found that NON_EMPTY did not have the behaviour I expected, when trying to map a tri-state JSON property (i.e. one which may be omitted, present or null).

If I have:

@JsonInclude(value = JsonInclude.Include.NON_EMPTY)
private Optional<String> foo;

I expected that the correspondences would be between an explicit null JSON value and a null Java value on the one hand, and an Optional.empty() value and the omission of the property in the JSON. However, actually what I think I found was that both Java null and Optional.empty() result in the omission of the property in the JSON.

It seems that if I want a tri-state mapping, I have to use:

@JsonInclude(value = JsonInclude.Include.NON_NULL)
private Optional<String> foo;

Then a null value causes the property to be omitted, and an empty value is serialized - as null. This seems backwards to me, but I think that's the only way of mapping all three possibilities?

from jackson-modules-java8.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.