这是indexloc提供的服务,不要输入任何密码
Skip to content

JsonInclude.Include.NON_EMPTY does not replace deprecated WRITE_EMPTY_JSON_ARRAYS #102

@rpatrick00

Description

@rpatrick00

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions