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.

Include.NON_NULL does not work on Java 8 Optional

See original GitHub issue

Following is a minimal reproduction of the issue. In short, I would have expected that, when using the Jdk8Module, empty Optionals wouldn’t be serialized when using the Include.NON_NULL serialization inclusion.

public class Issue {

    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.writeValue(System.out, model); // prints {"id":null,"name":null}
    }

    static class Model {
        private OptionalLong id;
        private Long order;
        private Optional<String> name;
        private String type;

        public OptionalLong getId() {
            return id;
        }

        public void setId(OptionalLong id) {
            this.id = id;
        }

        public Long getOrder() {
            return order;
        }

        public void setOrder(Long order) {
            this.order = order;
        }

        public Optional<String> getName() {
            return name;
        }

        public void setName(Optional<String> name) {
            this.name = name;
        }
        
        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }
    }

}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
cowtowncodercommented, Sep 23, 2017

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.

0reactions
garethsbcommented, Sep 12, 2018

@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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Annotate functions that return Optional with @NonNull?
Conclusion My conclusion so far is to not add @NonNull when returning Optional and if it can be null then annotate with @Nullable...
Read more >
Tired of Null Pointer Exceptions? Consider Using Java SE 8's ...
In this article, we have seen how you can adopt the new Java SE 8 java.util.Optional<T> . The purpose of Optional is not...
Read more >
Optional (Guava: Google Core Libraries for Java 30.0-jre API)
An immutable object that may contain a non-null reference to another object. ... Optional (JDK 8 and higher): A new Optional class was...
Read more >
Avoid Check for Null Statement in Java - Baeldung
Learn several strategies for avoiding the all-too-familiar boilerplate conditional statements to check for null values in Java.
Read more >
Java 8 - Optional Class - Tutorialspoint
Optional is a container object used to contain not-null objects. Optional object is used to represent null with absent value. This class has...
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