简体   繁体   English

在序列化为JSON时是否忽略声明的对象的某些变量?

[英]Make Ignore some of variable of the declared object when serializing to JSON?

may I know how to ignore some of member variables from the class that declared it. 我可能知道如何忽略声明它的类中的某些成员变量。 For example, below is the 3 classes, which is PersonalInfo and declared by AcedemicInfo and FamilyInfo . 例如,下面是3类,它们是PersonalInfo和通过声明AcedemicInfoFamilyInfo

public class PersonalInfo {
    @JsonPropetry
    private String name;

    @JsonPropetry
    String universityName;

    @JsonPropetry
    private String motherName;

    @JsonPropetry
    private String fatherName;

    /* Set and Get*/
}

public class AcademicInfo {
    @JsonPropetry
    private PersonalInfo info; // need name and university only

    /* Set and Get*/
}

public class FamilyInfo {
    @JsonPropetry
    private PersonalInfo info; // need name and fatherName and motherName only

    /* Set and Get*/
}

However, I need to ignore some of the member variables of PersonalInfo as the AcedemicInfo and FamilyInfo does not need all the attribute from PersonalInfo . 但是,我需要忽略一些成员变量的PersonalInfo作为AcedemicInfoFamilyInfo并不需要所有的属性PersonalInfo

Below is my desired output 以下是我想要的输出

// Acedemic info json
{
    "info" : {
        "name":"Adam",
        "universityName":"University"
        }
}

// Family info json
{
    "info" : {
        "name":"Adam",
        "fatherName":"Matt"
        "motherName":"Jane"
        }
}

I know about @JsonIgnore , but if I put the annotation in the PersonalInfo class, the variable will be ignore by all the class that declare it, which is not what I want. 我知道@JsonIgnore ,但是如果我将注释放在PersonalInfo类中,则声明该变量的所有类都会忽略该变量,这不是我想要的。 May I know how to ignore the variable conditionally? 我可以知道如何有条件地忽略该变量吗? Sorry for my bad English. 对不起,我的英语不好。

One way to do is use @JsonFilter with SimpleFilterProvider and SimpleBeanPropertyFilter to exclude properties not to serialize. 一种方法是将@JsonFilterSimpleFilterProviderSimpleBeanPropertyFilter以排除不进行序列化的属性。

You class will look like follows: 您的课程如下所示:

public class AcademicInfo {
    @JsonFilter("academicPersonalInfoFilter")
    private PersonalInfo info; 
}

and the example to serialize the object: 和序列化对象的示例:

SimpleFilterProvider filterProvider = new SimpleFilterProvider();
filterProvider.addFilter("academicPersonalInfoFilter",
        SimpleBeanPropertyFilter.serializeAllExcept("motherName", "fatherName"));

ObjectMapper mapper = new ObjectMapper();
mapper.setFilters(filterProvider);
mapper.writeValueAsString(academicInfo);

Another way to do is use multiple @JsonView to define a set of properties to serialize. 另一种方法是使用多个@JsonView定义一组要序列化的属性。

In you case, you can define views like follows: 您可以定义如下视图:

public class Views {

    public static class BasicPersonalInfo {
    }

    public static class AcademicPersonalInfo extends BasicPersonalInfo  {
    }

    public static class FamilyPersonalInfo extends BasicPersonalInfo {
    }
}

and annotate the field to be serialized in the corresponding view: 并在相应的视图中注释要序列化的字段:

public class PersonalInfo {
    @JsonView(Views.BasicPersonalInfo.class)
    private String name;

    @JsonView(Views.AcademicPersonalInfo.class)
    String universityName;

    @JsonView(Views.FamilyPersonalInfo.class)
    private String motherName;

    @JsonView(Views.FamilyPersonalInfo.class)
    private String fatherName;
}

and serialize object as follows: 并序列化对象,如下所示:

String result = mapper.writerWithView(Views.AcademicPersonalInfo.class)
                      .writeValueAsString(academicInfo);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM