简体   繁体   中英

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 .

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 .

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. 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.

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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