Bartosz Bilicki Bartosz Bilicki - 1 year ago 86
Java Question

How to make Immutables library skip serializing Optional fields

I am working with Immutables library ( I need my classes to serialize to JSON (I use Jackson). I want to skip Optional fields in my output JSON. Before Immutables, I provided

at class level.

It seems, Immutables is not respecting that annotation.
If I annotate every Optional field with
it works fine.

Is it possible to instruct Immutables to skip serializing empty Optionals using class-level (not field-level) annotation? I see
but there is no customize passed annotation using it.

I have jackson-datatype-jdk8 in my classpath, and my


Answer Source

Issue is resolved in Immutables since version v2.3.6 (possibly earlier). Immutables is respecting @JsonInclude, @JsonPropertyOrder and other class-level (or field-level) annotations from Jackson. Those annotations are copied to generated Immutable class and they work as expected.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download