简体   繁体   English

Jackson JSON 序列化:如何在嵌套对象的所有字段都为空时忽略它?

[英]Jackson JSON serialization: How to ignore a nested object when all its fields are null?

I'm using Jackson and I have some JSON schema objects set up something like this:我正在使用 Jackson 并且我有一些 JSON 模式对象设置如下:

@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Person {

    String name;
    Child child = new Child();
    Sibling sibling = new Sibling();

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public Child getChild() {
        return child;
    }
    public void setChild(Child child) {
        this.child = child;
    }

    public Sibling getSibling() {
        return sibling;
    }
    public void setSibling(Sibling sibling) {
        this.sibling = sibling;
    }
}

@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Child {

    String name;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Sibling {

    String name;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

I'm attempting to ignore all fields that are null or empty, which works fine.我试图忽略所有为空或空的字段,这很好用。 But I also want to ignore objects with fields that are all null or empty.但我也想忽略字段全部为空或空的对象。 For example:例如:

Person person = new Person();
person.setName("John Doe");
ObjectMapper mapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(person);

The resulting JSON string is {"name":"John Doe","child":{},"sibling":{}} , but I want it to be {"name":"John Doe"} .生成的 JSON 字符串是{"name":"John Doe","child":{},"sibling":{}} ,但我希望它是{"name":"John Doe"} Child and Sibling need to be initialized when Person is created so I don't want to change that.创建Person时需要初始化ChildSibling ,所以我不想更改它。 Is there a way to make Jackson treat objects with null fields as null with a custom serializer?有没有办法让杰克逊使用自定义序列化程序将具有空字段的对象视为空? I've seen examples of using custom serializers for specific types of objects but I need one that would work for any object.我见过为特定类型的对象使用自定义序列化程序的示例,但我需要一个适用于任何对象的示例。

You can achieve this in an arguably simpler way without custom serialiser(s) for Person or Child and Sibling but with CUSTOM include and passing the type of the field as filter. 您可以以一种可能更简单的方式来实现此目的,而无需为PersonChildSibling使用CUSTOM定义序列化程序,而使用CUSTOM include并将字段类型作为过滤器传递。

First of all define correct equals methods for Child and Sibling . 首先,为ChildSibling定义正确的equals方法。 Then, to filter nested objects that are equal to what their default constructor would return, annotate the pertinent getters in Person like this: 然后,要过滤与其默认构造函数将返回的嵌套对象相等的嵌套对象,请在Person注释相关的获取器,如下所示:

@JsonInclude(value = JsonInclude.Include.CUSTOM, valueFilter = Child.class)
public Child getChild() {
    return child;
}

@JsonInclude(value = JsonInclude.Include.CUSTOM, valueFilter = Sibling.class)
public Sibling getSibling() {
    return sibling;
}

Setting valueFilter to Child.class above has the effect of creating an object with the default constructor Child emptyChild = new Child() and then to decide that another object Child child should be serialised checks that emptyChild.equals(child) is false valueFilter设置为上面的Child.class的作用是使用默认构造函数Child emptyChild = new Child()创建一个对象,然后确定应该序列化另一个对象Child child来检查emptyChild.equals(child)是否为false

docs for valueFilter 用于valueFilter的文档

In your case, I think ignoring null values on the serializer level should be enough:在您的情况下,我认为在序列化程序级别忽略空值就足够了:

mapper.setSerializationInclusion(Include.NON_EMPTY);
mapper.setSerializationInclusion(Include.NON_NULL);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果所有嵌套字段都为空,Jackson 会忽略顶级字段的序列化 - Jackson ignore serialization of top-level field if all its nested fields are null 如何告诉 Jackson 在序列化过程中忽略带有 EMPTY 或 NULL 字段的 Object? - How to tell Jackson to ignore a Object with EMPTY or NULL fields during serialization? 如果字段值为空,如何告诉杰克逊在序列化期间忽略该字段? - How to tell Jackson to ignore a field during serialization if its value is null? JSON序列化的Jackson Java对象不包含空字段 - Jackson java object to json serialization is not including null fields 使用 Gson 或 Jackson 反序列化 JSON 时忽略空字段 - Ignore null fields when DEserializing JSON with Gson or Jackson 使用Jackson进行序列化和反序列化:如何以编程方式忽略字段? - Serialization and Deserialization with Jackson: how to programmatically ignore fields? 使用杰克逊(ObjectMapper)如何将对象序列化为json并忽略除我注释为@JsonProperty的字段以外的所有其他字段? - Using Jackson (ObjectMapper) how serialize object to json and ignore all fields except the ones I annotate as @JsonProperty? 如何忽略 Jackson JSON-to-Object 映射中的枚举字段? - How to ignore enum fields in Jackson JSON-to-Object mapping? Jackson ObjectMapper:如何从序列化中省略(忽略)某些类型的字段? - Jackson ObjectMapper: How to omit (ignore) fields of certain type from serialization? 杰克逊序列化视图:嵌套对象 - jackson serialization views: nested object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM