As a part of End to End REST Assured Tutorial, in this post, We will learn about an important annotation called @JsonIgnore of Jackson library which helps in restricting properties of a POJO class from serialization and deserialization.
Prerequisite
Since we are using Jackson API of Java for this example, make sure you have the latest dependency of Jackson Databind in your project classpath. I have used below Jackson dependency for this post:-
com.fasterxml.jackson.core jackson-databind 2.11.1
There may be multiple fields in a Pojo class and sometimes you don’t require some fields to participate in serialization and deserialization process. I can give a scenario where we may need to ignore some properties of a Pojo class.
In a POJO class, we may have properties whose values are derived from other properties. For example- If we know the first name and last name of a person we can get their full name. If we know the age of a person we can get to know if a person is eligible for the vote or not. We can have getter methods for such properties to return values but should not be allowed to set from outside.
Consider below POJO.
package JacksonTutorials; public class EmployeePojoWithoutJsonIgnore { // private variables or data members of pojo class private String firstName; private String lastName; private String gender; private int age; private double salary; private boolean married; private String fullName; private boolean eligibleForVote; // Getter and setter methods public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public boolean getMarried() { return married; } public void setMarried(boolean married) { this.married = married; } public String getFullName() { return this.fullName; } public boolean getEligibleForVote() { return this.eligibleForVote; } public void setFullName(String fullName) { this.fullName = fullName; } public void setEligibleForVote(boolean eligibleForVote) { this.eligibleForVote = eligibleForVote; } }
Serialization using above POJO
@Test public void serializationWithoutJsonIgnore() throws JsonProcessingException { // Just create an object of Pojo class EmployeePojoWithoutJsonIgnore employeePojoWithoutJsonIgnore = new EmployeePojoWithoutJsonIgnore(); employeePojoWithoutJsonIgnore.setFirstName("Amod"); employeePojoWithoutJsonIgnore.setLastName("Mahajan"); employeePojoWithoutJsonIgnore.setAge(29); employeePojoWithoutJsonIgnore.setGender("Male"); employeePojoWithoutJsonIgnore.setSalary(12323.56); employeePojoWithoutJsonIgnore.setMarried(false); employeePojoWithoutJsonIgnore.setFullName("Animesh Prashant"); employeePojoWithoutJsonIgnore.setEligibleForVote(false); // Converting a Java class object to a JSON payload as string ObjectMapper objectMapper = new ObjectMapper(); String employeeJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(employeePojoWithoutJsonIgnore); System.out.println("Serialization..."); System.out.println(employeeJson); }
Output
Serialization... { "firstName" : "Amod", "lastName" : "Mahajan", "gender" : "Male", "age" : 29, "salary" : 12323.56, "married" : false, "fullName" : "Animesh Prashant", "eligibleForVote" : false }
Deserialization using above POJO
@Test public void deserializationWithoutJsonIgnore() throws JsonMappingException, JsonProcessingException { String employeeString = "{\r\n" + " \"firstName\" : \"Amod\",\r\n" + " \"lastName\" : \"Mahajan\",\r\n" + " \"gender\" : \"Male\",\r\n" + " \"age\" : 29,\r\n" + " \"salary\" : 12323.56,\r\n" + " \"married\" : false,\r\n" + " \"fullName\" : \"Amod Mahajan Gupta\",\r\n" + " \"eligibleForVote\" : false\r\n" + "}"; ObjectMapper objectMapper = new ObjectMapper(); EmployeePojoWithoutJsonIgnore employeePojoWithoutJsonIgnore2 = objectMapper.readValue(employeeString, EmployeePojoWithoutJsonIgnore.class); System.out.println("Deserialization..."); System.out.println("First name :- "+employeePojoWithoutJsonIgnore2.getFirstName()); System.out.println("Last name :- "+employeePojoWithoutJsonIgnore2.getLastName()); System.out.println("Age :- "+employeePojoWithoutJsonIgnore2.getAge()); System.out.println("Gender :- "+employeePojoWithoutJsonIgnore2.getGender()); System.out.println("Salary :- "+employeePojoWithoutJsonIgnore2.getSalary()); System.out.println("Married :- "+employeePojoWithoutJsonIgnore2.getMarried()); System.out.println("Eligible for vote :- "+employeePojoWithoutJsonIgnore2.getEligibleForVote()); System.out.println("Full name :- "+employeePojoWithoutJsonIgnore2.getFullName()); }
Output:-
Deserialization... First name :- Amod Last name :- Mahajan Age :- 29 Gender :- Male Salary :- 12323.56 Married :- false Eligible for vote :- false Full name :- Amod Mahajan Gupta
If you observe output carefully we see values of properties of “fullName” and “eligibleForVote” have set wrongly. We should have calculated it. Ideally, these fields should have getter methods only just for quick access.
Complete Program
package JacksonTutorials; import org.testng.annotations.Test; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; public class SerialiDeserialExampleWithoutJsonIgnore { @Test public void serializationWithoutJsonIgnore() throws JsonProcessingException { // Just create an object of Pojo class EmployeePojoWithoutJsonIgnore employeePojoWithoutJsonIgnore = new EmployeePojoWithoutJsonIgnore(); employeePojoWithoutJsonIgnore.setFirstName("Amod"); employeePojoWithoutJsonIgnore.setLastName("Mahajan"); employeePojoWithoutJsonIgnore.setAge(29); employeePojoWithoutJsonIgnore.setGender("Male"); employeePojoWithoutJsonIgnore.setSalary(12323.56); employeePojoWithoutJsonIgnore.setMarried(false); employeePojoWithoutJsonIgnore.setFullName("Animesh Prashant"); employeePojoWithoutJsonIgnore.setEligibleForVote(false); // Converting a Java class object to a JSON payload as string ObjectMapper objectMapper = new ObjectMapper(); String employeeJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(employeePojoWithoutJsonIgnore); System.out.println("Serialization..."); System.out.println(employeeJson); } @Test public void deserializationWithoutJsonIgnore() throws JsonMappingException, JsonProcessingException { String employeeString = "{\r\n" + " \"firstName\" : \"Amod\",\r\n" + " \"lastName\" : \"Mahajan\",\r\n" + " \"gender\" : \"Male\",\r\n" + " \"age\" : 29,\r\n" + " \"salary\" : 12323.56,\r\n" + " \"married\" : false,\r\n" + " \"fullName\" : \"Amod Mahajan Gupta\",\r\n" + " \"eligibleForVote\" : false\r\n" + "}"; ObjectMapper objectMapper = new ObjectMapper(); EmployeePojoWithoutJsonIgnore employeePojoWithoutJsonIgnore2 = objectMapper.readValue(employeeString, EmployeePojoWithoutJsonIgnore.class); System.out.println("Deserialization..."); System.out.println("First name :- "+employeePojoWithoutJsonIgnore2.getFirstName()); System.out.println("Last name :- "+employeePojoWithoutJsonIgnore2.getLastName()); System.out.println("Age :- "+employeePojoWithoutJsonIgnore2.getAge()); System.out.println("Gender :- "+employeePojoWithoutJsonIgnore2.getGender()); System.out.println("Salary :- "+employeePojoWithoutJsonIgnore2.getSalary()); System.out.println("Married :- "+employeePojoWithoutJsonIgnore2.getMarried()); System.out.println("Eligible for vote :- "+employeePojoWithoutJsonIgnore2.getEligibleForVote()); System.out.println("Full name :- "+employeePojoWithoutJsonIgnore2.getFullName()); } }
There may be some fields that may be optional or just want to ignore it for time being.
If we want to ignore any property of a POJO class from serialization and deserialization we can use @JsonIgnore provided by Jackson API on that property. It is a marker annotation that indicates that the logical property that the accessor is to be ignored by introspection-based serialization and deserialization functionality. Annotation only needs to be added to one of the accessors (often getter method, but may be setter, field or creator parameter), if the complete removal of the property is desired.
POJO with JsonIgnore
package JacksonTutorials; import com.fasterxml.jackson.annotation.JsonIgnore; public class EmployeePojoWithJsonIgnore { // private variables or data members of pojo class private String firstName; private String lastName; private String gender; private int age; private double salary; private boolean married; @JsonIgnore private String fullName; @JsonIgnore private boolean eligibleForVote; // Getter and setter methods public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public boolean getMarried() { return married; } public void setMarried(boolean married) { this.married = married; } public String getFullName() { return this.fullName; } public boolean getEligibleForVote() { return this.eligibleForVote; } public void setFullName(String fullName) { this.fullName = fullName; } public void setEligibleForVote(boolean eligibleForVote) { this.eligibleForVote = eligibleForVote; } }
Serialization using POJO with JsonIgnore
@Test public void serializationWithJsonIgnore() throws JsonProcessingException { // Just create an object of Pojo class EmployeePojoWithJsonIgnore employeePojoWithJsonIgnore = new EmployeePojoWithJsonIgnore(); employeePojoWithJsonIgnore.setFirstName("Amod"); employeePojoWithJsonIgnore.setLastName("Mahajan"); employeePojoWithJsonIgnore.setAge(29); employeePojoWithJsonIgnore.setGender("Male"); employeePojoWithJsonIgnore.setSalary(12323.56); employeePojoWithJsonIgnore.setMarried(false); employeePojoWithJsonIgnore.setFullName("Animesh Prashant"); employeePojoWithJsonIgnore.setEligibleForVote(false); // Converting a Java class object to a JSON payload as string ObjectMapper objectMapper = new ObjectMapper(); String employeeJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(employeePojoWithJsonIgnore); System.out.println("Serialization..."); System.out.println(employeeJson); }
Output:-
Serialization... { "firstName" : "Amod", "lastName" : "Mahajan", "gender" : "Male", "age" : 29, "salary" : 12323.56, "married" : false }
Deserialization using POJO with JsonIgnore
@Test public void deserializationWithJsonIgnore() throws JsonMappingException, JsonProcessingException { String employeeString = "{\r\n" + " \"firstName\" : \"Amod\",\r\n" + " \"lastName\" : \"Mahajan\",\r\n" + " \"gender\" : \"Male\",\r\n" + " \"age\" : 29,\r\n" + " \"salary\" : 12323.56,\r\n" + " \"married\" : false,\r\n" + " \"fullName\" : \"Amod Mahajan Gupta\",\r\n" + " \"eligibleForVote\" : false\r\n" + "}"; ObjectMapper objectMapper = new ObjectMapper(); EmployeePojoWithJsonIgnore employeePojoWithJsonIgnore2 = objectMapper.readValue(employeeString, EmployeePojoWithJsonIgnore.class); System.out.println("Deserialization..."); System.out.println("First name :- "+employeePojoWithJsonIgnore2.getFirstName()); System.out.println("Last name :- "+employeePojoWithJsonIgnore2.getLastName()); System.out.println("Age :- "+employeePojoWithJsonIgnore2.getAge()); System.out.println("Gender :- "+employeePojoWithJsonIgnore2.getGender()); System.out.println("Salary :- "+employeePojoWithJsonIgnore2.getSalary()); System.out.println("Married :- "+employeePojoWithJsonIgnore2.getMarried()); System.out.println("Eligible for vote :- "+employeePojoWithJsonIgnore2.getEligibleForVote()); System.out.println("Full name :- "+employeePojoWithJsonIgnore2.getFullName()); }
Output
Deserialization... First name :- Amod Last name :- Mahajan Age :- 29 Gender :- Male Salary :- 12323.56 Married :- false Eligible for vote :- false Full name :- null
Complete program
package JacksonTutorials; import org.testng.annotations.Test; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; public class SerialiDeserialExampleWithJsonIgnore { @Test public void serializationWithJsonIgnore() throws JsonProcessingException { // Just create an object of Pojo class EmployeePojoWithJsonIgnore employeePojoWithJsonIgnore = new EmployeePojoWithJsonIgnore(); employeePojoWithJsonIgnore.setFirstName("Amod"); employeePojoWithJsonIgnore.setLastName("Mahajan"); employeePojoWithJsonIgnore.setAge(29); employeePojoWithJsonIgnore.setGender("Male"); employeePojoWithJsonIgnore.setSalary(12323.56); employeePojoWithJsonIgnore.setMarried(false); employeePojoWithJsonIgnore.setFullName("Animesh Prashant"); employeePojoWithJsonIgnore.setEligibleForVote(false); // Converting a Java class object to a JSON payload as string ObjectMapper objectMapper = new ObjectMapper(); String employeeJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(employeePojoWithJsonIgnore); System.out.println("Serialization..."); System.out.println(employeeJson); } @Test public void deserializationWithJsonIgnore() throws JsonMappingException, JsonProcessingException { String employeeString = "{\r\n" + " \"firstName\" : \"Amod\",\r\n" + " \"lastName\" : \"Mahajan\",\r\n" + " \"gender\" : \"Male\",\r\n" + " \"age\" : 29,\r\n" + " \"salary\" : 12323.56,\r\n" + " \"married\" : false,\r\n" + " \"fullName\" : \"Amod Mahajan Gupta\",\r\n" + " \"eligibleForVote\" : false\r\n" + "}"; ObjectMapper objectMapper = new ObjectMapper(); EmployeePojoWithJsonIgnore employeePojoWithJsonIgnore2 = objectMapper.readValue(employeeString, EmployeePojoWithJsonIgnore.class); System.out.println("Deserialization..."); System.out.println("First name :- "+employeePojoWithJsonIgnore2.getFirstName()); System.out.println("Last name :- "+employeePojoWithJsonIgnore2.getLastName()); System.out.println("Age :- "+employeePojoWithJsonIgnore2.getAge()); System.out.println("Gender :- "+employeePojoWithJsonIgnore2.getGender()); System.out.println("Salary :- "+employeePojoWithJsonIgnore2.getSalary()); System.out.println("Married :- "+employeePojoWithJsonIgnore2.getMarried()); System.out.println("Eligible for vote :- "+employeePojoWithJsonIgnore2.getEligibleForVote()); System.out.println("Full name :- "+employeePojoWithJsonIgnore2.getFullName()); } }
We have values for fields fullName and eligibleForVote in Json but it has not been deserialized as you can see it has default values not from Json.
@JsonIgnore annotation can be used with getter and setter methods as well.
You can download/clone the above sample project from here.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning
Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.
Many other topics you can navigate through the menu.