0

I am working with Immutables library (https://immutables.github.io). 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 @JsonInclude(JsonInclude.Include.NON_EMPTY) at class level.

It seems, Immutables is not respecting that annotation. If I annotate every Optional field with @JsonInclude(JsonInclude.Include.NON_EMPTY) it works fine.

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

I have jackson-datatype-jdk8 in my classpath, and my com.fasterxml.jackson.databind.ObjectMapper has

mapper.registerModule(new Jdk8Module().configureAbsentsAsNulls(true));

Bartosz Bilicki
  • 12,599
  • 13
  • 71
  • 113
  • 1
    Are you using https://github.com/FasterXML/jackson-datatype-jdk8 ? – slim Oct 10 '16 at 15:00
  • yes. It works if I configure @JsonInclude(JsonInclude.Include.NON_EMPTY) at field-level but Immutables library is not respecting this annotation at class-level. – Bartosz Bilicki Oct 10 '16 at 15:04
  • I can't replicate your problem locally. Annotating my immutable class with `@JsonInclude` makes Jackson omit the empty, optional field. Which version of Immutables are you using? – Henrik Aasted Sørensen Nov 19 '16 at 13:10
  • 1
    @Henrik issue is indeed resolved in latest immutables. I posted answer to my question, clarifying that. Thanks for bumping the question! – Bartosz Bilicki Nov 19 '16 at 13:41

1 Answers1

3

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.

Bartosz Bilicki
  • 12,599
  • 13
  • 71
  • 113