Comments (5)
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.
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.
Awesome, thanks for the heads up on NON_ABSENT
, works great.
from jackson-modules-java8.
Np! This is an area that has been slowly but steadily improved; good to hear it works as expected!
from jackson-modules-java8.
@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)
- `LocalDateTime` serialization issue with custom-configured `LocalDateTimeSerializer` HOT 12
- InstantDeserializer fails to parse negative numeric timestamp strings for pre-1970 values. HOT 2
- Custom java time module ignored after upgrade to 2.16 HOT 3
- `Optional` deserialization, serialization ignore `contentConverter` HOT 1
- NPE when serializing a `LocalDate` or `LocalDateTime` using `AsDeductionTypeSerializer` HOT 9
- OffsetTimeSerializer not writing seconds for time object like 15:30:00Z (where seconds is 00) HOT 2
- Implicit name provided by `ParameterNamesModule` overridden by "incomplete" `@JsonProperty` HOT 1
- I don't deserialize java 8 date/time type `java.time.Instant` HOT 2
- ZonedDateTime not serialised as UTC by default HOT 1
- Instants get not deserialized as expected sometimes HOT 2
- Only `DateTimeFormatter.ISO_OFFSET_DATE_TIME` accepted by `ZonedDateTimeKeyDeserializer` HOT 1
- Can't deserialize OffsetDateTime.MIN: Invalid value for EpochDay
- Date and time serialization are very inconsistent HOT 3
- Absent nested optional shennanigans HOT 1
- The serialization of an object of type LocalDate did not record its type HOT 2
- Q on Serializing java.time dates w/o nano parts HOT 2
- Add javaTimeModule.addDeserializer(String.class,...) Error parsing generated later HOT 4
- java.lang.NoSuchFieldError: READ_DATE_TIMESTAMPS_AS_NANOSECONDS after update to 2.16 or 2.17 HOT 3
- Parameter names: Class with a single-parameter implicit constructor fails deserialization, works if more (unused) parameters are added HOT 3
- Inconsistent BeanPropertyDefinition#couldDeserialize and actual deserialization behavior HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jackson-modules-java8.