question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JsonInclude.Include.NON_EMPTY does not replace deprecated WRITE_EMPTY_JSON_ARRAYS

See original GitHub issue

In moving from Jackson 2.7.6 to 2.8.3, we are now getting compiler warnings with regards to our use of the SerializationFeature.WRITE_EMPTY_JSON_ARRAYS. From what I can tell, the recommendation is to replace this with JsonInclude.Include.NON_EMPTY.

Our code previously looked like this:

public static ObjectMapper getJacksonObjectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    mapper.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
    return mapper;
}

and when serializing JSON, empty arrays are omitted.

If I comment out the deprecated WRITE_EMPTY_JSON_ARRAYS line, and write the exact same object, the JSON has empty array elements within it.

Here is a simple test case, just unzip and run mvn package and look at the test output and you will see something that looks like the following:

Running test.EmptyArraysTest

Printing object with WRITE_EMPTY_JSON_ARRAYS set to false:

{ “id” : “123” }

Printing object without WRITE_EMPTY_JSON_ARRAYS:

{ “id” : “123”, “databases” : [ ] }

What am I missing?
jackson-empty-arrays-test.zip

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Sep 28, 2016

@rpatrick00 The problem here is that MyClass has override for @JsonInclude:

@JsonInclude(JsonInclude.Include.NON_NULL)

which will only suppress writing of nulls; since it is more specific than global defaults it will have precedence.

Another thing to note on replacement is that in addition to global inclusion default, and per-property annotation (that is, can put @JsonInclude on field/setter/getter), with 2.8 you can also specify type defaults:

        mapper.configOverride(Map.class)
            .setInclude(JsonInclude.Value.construct(JsonInclude.Include.NON_EMPTY, null));

which should then make all List<> valued properties default to excluding empty, as well as null, values.

0reactions
cowtowncodercommented, Sep 9, 2019

Quick note here: if anyone wants to re-open the feature request, please do so at jackson-databind as Annotations package only deals with definition of annotations themselves and not any logic taken on them. If filing a new one (I’d rather do that than transfer this one), you can add reference this issue as background, but please summarize how “config overrides” is not workable solution.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Don't return property when it's an empty list with jackson
If you can change your original model with Jackson annotations, here is the way how to achieve ... Include only not null values;...
Read more >
Jackson JSON - @JsonInclude NON_EMPTY Example
@JsonInclude(NON_EMPTY) can be used to exclude values that are empty. Following values are considered to be empty:.
Read more >
Micronaut Framework/questions - Gitter
I have something like data class SomeWrapper(val list: List<SomeObject>) and a funtion annotated with @Cacheable like fun getWrapperType(id: String): ...
Read more >
JsonInclude.Include (Jackson-annotations 2.7.0 API)
Value that indicates that properties are included unless their value is: null "absent" value of a referential type (like Java 8 `Optional`, ...
Read more >
Ignore Null Fields with Jackson - Baeldung
This quick tutorial is going to cover how to set up Jackson to ignore null fields when ... public class MyDto { @JsonInclude(Include....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found