11

I have a Java class with an Optional field. I am serializing the class to JSON using Jackson 2.8.3 (called from Spring web 4.3.3).

I am hoping to get the serializer to skip the field if the Optional is empty, and serialize the contained string if it is present. An example of the result I'm looking for with a list of two objects:

[
    {
        "id": 1,
        "foo": "bar"
    },
    {
        "id": 2,
    }
]

Here the foo Optional is empty for the object with id 2.

Instead, what I get is:

[
    {
        "id": 1,
        "foo": {
            "present": true
        }
    },
    {
        "id": 2,
        "foo": {
            "present": false
        }
    }
]

This is the result even if I annotate the "bar" field in the class like

@JsonInclude(JsonInclude.Include.NON_ABSENT)
public Optional<String> getFoo() { ...

Is there any way I can achieve a result like the first list using the Jackson annotations or a custom serializer?

André Risnes
  • 137
  • 1
  • 1
  • 8
  • 1
    As an aside, using `Optional` as a field of a bean is a Bad Idea™ in general. – biziclop Nov 03 '16 at 19:05
  • Why is that considered problematic? – André Risnes Nov 03 '16 at 20:18
  • It wasn't designed for that kind of use. It's a very subtle semantic difference but in the case of a field, `null` is considered just another value. While if you're designing a method in the API of a library, you might want to emphasise that it may not return a value, and thus use `Optional`. See more [here](http://stackoverflow.com/questions/26327957/should-java-8-getters-return-optional-type/26328555#26328555) – biziclop Nov 03 '16 at 20:55

3 Answers3

26

No need to write custom serializer. Annotate your class with @JsonInclude(JsonInclude.Include.NON_ABSENT).

You also need to:

  • include com.fasterxml.jackson.datatype:jackson-datatype-jdk8 as your dependency
  • and to register the corresponding module with your object mapper: objectMapper.registerModule(new Jdk8Module());
Vic Seedoubleyew
  • 9,888
  • 6
  • 55
  • 76
Bartosz Bilicki
  • 12,599
  • 13
  • 71
  • 113
  • Thanks, including the dependency on com.fasterxml.jackson.datatype:jackson-datatype-jdk8 did the trick. – André Risnes Nov 03 '16 at 20:18
  • 1
    @JsonInclude(JsonInclude.Include.NON_ABSENT) will be good enough. – GKVM May 19 '17 at 09:11
  • 2
    To exclude Optional.empty() fields, as the OP originally asked, one MUST add the depencandy and MUST set NON_ABSENT. NON_NULL is not enough. – Robert Mar 17 '19 at 17:43
2

You can use objectMapper.registerModule(new Jdk8Module()); but it serializes with null values.

But still you want to remove null values also from JSON, please use the following code:

objectMapper.registerModule(new Jdk8Module().configureAbsentsAsNulls(true));
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
Greenonline
  • 1,330
  • 8
  • 23
  • 31
1

Use a JsonSerializer to your needs.

Something like this (semi-pseudo):

public class MySer extends JsonSerializer<Opional<?>> {

        @Override
        public void serialize(Optional<?> optString, JsonGenerator generator, SerializerProvider provider)
                                          throws IOException, JsonProcessingException {
        //Check Optional here...        
        generator.writeString(/* DO SOMETHING HERE WHATEVER */);
    }

//Then in your model:

public class ClassWhatever {           
    @JsonSerialize(using = MySer .class)
    public Optional<String> getFoo() { ...
 }

To avoid annotating every field with @JsonSerialize you may register your custom serializer to object mapper using

  ObjectMapper mapper = new ObjectMapper();
SimpleModule testModule = new SimpleModule("MyModule", new Version(1, 0, 0, null));
testModule.addSerializer(new MyCustomSerializer()); // assuming serializer declares correct class to bind to
mapper.registerModule(testModule);

Also, given solution works only for serialization. Deserialization will fail unless you write your own deserializer. Then you need to annotate every field with @JsonDeserialize or register your custom deserializer.

Bartosz Bilicki
  • 12,599
  • 13
  • 71
  • 113
Mechkov
  • 4,294
  • 1
  • 17
  • 25
  • 1
    com.fasterxml.jackson.datatype:jackson-datatype-jdk8 is better option. with such custom solution you need to annotate every field. Also, deserialization will not work unless -you write custom deserializer and annotate every field with @JsonDeserialize – Bartosz Bilicki Nov 03 '16 at 18:42
  • @BartoszBilicki Certainly, in agreement with you there. Both would work, but your recommendation is better. – Mechkov Nov 03 '16 at 18:47