How to exclude in JSON property of an object (inside output class)?

Let's say I an output class that will be converted to JSON:

StudentSummary.java

public class StudentSummary {

    private StudentList studentList;

    // getters setters
}

and

StudentList.java

public class StudentList {

    private int numberOfStudents;
    private int totalExpenditures;
    private List<Student> students;

    // getters setters
}

After making a service call, I get this JSON output:

{ 
 "studentSummary": {
     "studentList": {
         "numberOfStudents": 500,
         "totalExpenditures": 250000,
         "students": [ /* students listed */ ]
     }
}

I want to exclude from JSON the studentList in the StudentSummary class:

{ 
 "studentSummary": {
     "studentList": {
         "numberOfStudents": 500,
         "totalExpenditures": 250000
      }
  }
}

I've tried using (in the StudentSummary output class) @JsonIgnore and @JsonProperties , by specifying to only exclude "studentList.students", but that doesn't do anything.

EDIT: Further clarification, for why I couldn't do the changes inside the StudentList class, it's because it is used by another service and the service with StudentSummary class is of a different service, so I can only make the changes inside the latter class, without modifying the previous service.

Upvotes: 2

Views: 12719

Answers (6)

Menelaos
Menelaos

Reputation: 26549

Summary

This will solve your problem:

public class StudentSummary {

    @JsonIgnoreProperties(value = { "students" })
    public StudentList studentList = new StudentList();

You do not need to edit the StudentList class.

All Test Class Definitions

Student

public class Student {

    String name = "Student" + Math.random()*100;

    public String getName() {
        return name;
    }

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

StudentList

public class StudentList {

    int numberOfStudents;
    int totalExpenditures;
    List<Student> students = new ArrayList<>();

    public StudentList(){
        for(int i =0; i<10; i++){
            students.add(new Student());
        }
    }

    public int getNumberOfStudents() {
        return numberOfStudents;
    }

    public void setNumberOfStudents(int numberOfStudents) {
        this.numberOfStudents = numberOfStudents;
    }

    public int getTotalExpenditures() {
        return totalExpenditures;
    }

    public void setTotalExpenditures(int totalExpenditures) {
        this.totalExpenditures = totalExpenditures;
    }

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    //getters setters
}

StudentSummary

public class StudentSummary {

    @JsonIgnoreProperties(value = { "students" })
    public StudentList studentList = new StudentList();

    public StudentSummary(){
        studentList = new StudentList();
    }

    public StudentList getStudentList() {
        return studentList;
    }

    public void setStudentList(StudentList studentList) {
        this.studentList = studentList;
    }

    //getters setters
}

Main Class

ObjectMapper mapper = new ObjectMapper();

StudentSummary summary = new StudentSummary();
String test = mapper.writeValueAsString(summary);
System.out.println(test);
System.out.print("DONE");

Upvotes: 3

Yogi
Yogi

Reputation: 1895

If you cannot change source code of class, you need to create Mixin class for omitting Studentlist ...

Follow below link for Jackson Mixin..

How can I tell jackson to ignore a property for which I don't have control over the source code?

Upvotes: 1

albert_nil
albert_nil

Reputation: 1658

I suggest reading here

Basically, if you cannot modify anything on the class itself, then you will need to tell jackson globally how to handle this on the serialization config (through the mixing annotations on the object mapper)

Upvotes: 0

utkarsh31
utkarsh31

Reputation: 1559

I believe from the question you cannot touch the StudentList class and everything should be handled from the StudentSummary class. What my idea is create another class SpecialStudent and override the setter/getter for the student list and then put the @JsonIgnore property on them. Below is the example for the same. Let me know if this helps

class SpecialStudent extends StudentList{
    @JsonIgnore
    @Override
    public List<Student> getStudents()
    {
        return students;
    }
    /**
     * @param students the students to set
     */
    @Override
    @JsonIgnore
    public void setStudents(List<Student> students)
    {
        this.students = students;
    }
}

Student Summary would look like this

public class StudentSummary
{
    SpecialStudent studentList;

    /**
     * @return the studentList
     */
    public StudentList getStudentList()
    {
        return studentList;
    }

    /**
     * @param studentList the studentList to set
     */
    public void setStudentList(SpecialStudent studentList)
    {
        this.studentList = studentList;
    }

}

Test class

StudentSummary sum = new  StudentSummary();
        SpecialStudent l = new SpecialStudent();
        l.setNumberOfStudents(4);
        l.setTotalExpenditures(12);
        sum.setStudentList(l);


        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(sum);

        System.out.println(json);

Output

{
  "studentList" : {
    "numberOfStudents" : 4,
    "totalExpenditures" : 12
  }
}

Upvotes: 0

Yogi
Yogi

Reputation: 1895

You can use @JsonIgnoreProperties or @JsonIgnore for ignoring any property of class while JSON serialization. For e.g.

***public class StudentList {
int numberOfStudents;
int totalExpenditures;
@JsonIgnore
List<Student> students;
//getters setters
}***

Or

***@JsonIgnoreProperties(value = { "students" })
public class StudentList {
int numberOfStudents;
int totalExpenditures;
List<Student> students;
//getters setters
}***

Upvotes: 0

Kh.Taheri
Kh.Taheri

Reputation: 957

Since you could NOT change the StudentList; You do not need to include an instance of StudentList in StudentSummary. So, you could have some sort of mapping like the following:

 public class StudentSummary {

    int numberOfStudents;
    int totalExpenditures;

    public StudentSummary (){
         StudentList studentList = new StudentList();
         this.numberOfStudents = studentList.getNumberOfStudents();
         this.totalExpenditures = studentList.getTotalExpenditures();
    }

    //getters setters for numberOfStudents & totalExpenditures
  }

Upvotes: -1

Related Questions