Include.NON_NULL does not work on Java 8 Optional
See original GitHub issueFollowing 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:
- Created 6 years ago
- Comments:5 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Right, “NULL” means exactly and only Java
null
. NotOptional.empty()
. This is intentional. There is also valueNON_ABSENT
which would includeOptional
(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
.@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 ornull
).If I have:
I expected that the correspondences would be between an explicit
null
JSON value and anull
Java value on the one hand, and anOptional.empty()
value and the omission of the property in the JSON. However, actually what I think I found was that both Javanull
andOptional.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:
Then a
null
value causes the property to be omitted, and an empty value is serialized - asnull
. This seems backwards to me, but I think that’s the only way of mapping all three possibilities?