Skip to content

Class level @JsonInclude(JsonInclude.Include.NON_EMPTY) is ignored #1327

@elruwen

Description

@elruwen

Hi!

I am upgrading from Jackson 2.4.6 to 2.8.1 and I have an issue with a class level @JsonInclude annotation.

The following class runs perfectly with 2.4.6:

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.List;

public class JacksonBug {

    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    public static class MyObject {

        private String myString;
        private List<String> myList = new ArrayList<>();

        public String getMyString() {
            return myString;
        }

        public void setMyString(String myString) {
            this.myString = myString;
        }

        public List<String> getMyList() {
            return myList;
        }

        public void setMyList(List<String> myList) {
            this.myList = myList;
        }
    }

    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper om = new ObjectMapper();
        om.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        final MyObject myObject = new MyObject();
        myObject.setMyString("hehe");

        final String jsonString = om.writeValueAsString(myObject);
        System.out.println(jsonString);

        if (jsonString.contains("myList")) {
            throw new RuntimeException("JsonString contains myList");
        }
    }
}

When I switch to 2.8.1, it does not worker anymore. The list myList is serialised as an empty list: {"myString":"hehe","myList":[]}

I can work around that issue by moving the exclude to the field level.

I guess this is a bug? Or was the behaviour change intended?

Cheers
Ruwen

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions