REST Assured Tutorial 40 – @JsonIgnoreProperties Annotation Of Jackson API – Exclude Field Of Pojo From Serialization Or Deserialization or Both

Introduction

As a part of End to End REST Assured Tutorial, in this post, We will learn about an important annotation called @JsonIgnoreProperties of Jackson library which helps in restricting properties of a POJO class from serialization or deserialization or both at the class level or property level.

Prerequisite

Supported Posts

You must refer below these prerequisite posts to understand this concept better:-

How Getter & Setter Methods Matter For Serialization Deserialization Using POJO

@JsonIgnore Annotation Of Jackson API – Exclude Field Of Pojo From Serialization And Deserialization

Required Java Library

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

Limitations of @JsonIgnore

We use @JsonIgnore annotation at an individual property level or its one of the accessors to prevent a particular field from serialization and deserialization process. It has some limitations as below:-

  1. To ignore multiple properties, you need to mark each property with @JsonIgnore that makes it difficult to manage.
  2. If you mention @JsonIgnore at any one place i.e. at a property or its one of accessors then that property will be ignored from serialization and deserialization. We can not control if we want a property to be ignored by serialization or deserialization only.

These limitations can be overcome using @JsonIgnoreProperties.

Introduction of @JsonIgnoreProperties

It is an annotation that can be used to either suppress serialization of properties (during serialization) or ignore the processing of JSON properties read (during deserialization). This can be used at class level as well as at an individual property level like @JsonIgnore.

Let’s see some basic examples to understand it more clear.

Examples

Ignore multiple properties from serialization and deserialization

If we want to ignore multiple properties from both serialization and deserialization process then we can mention all property names at class level as below:-

@JsonIgnoreProperties({"gender","fullName"})
public class EmployeePojoWithJsonIgnoreProperties {

OR

@JsonIgnoreProperties(values = {"gender","fullName"})
public class EmployeePojoWithJsonIgnoreProperties {

We will create a POJO class in which some fields will be ignored using above annotation.

Pojo class with @JsonIgnoreProperties
package JacksonTutorials;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties({"gender","fullName"})
public class EmployeePojoWithJsonIgnoreProperties {

	// 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 Code
package JacksonTutorials;

import org.testng.annotations.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class SerialiDeserialExampleWithJsonIgnoreProperties {

	@Test
	public void serializationWithJsonIgnoreProperties() throws JsonProcessingException {
		// Just create an object of Pojo class
		EmployeePojoWithJsonIgnoreProperties employeePojoWithJsonIgnoreProperties = new EmployeePojoWithJsonIgnoreProperties();
		employeePojoWithJsonIgnoreProperties.setFirstName("Amod");
		employeePojoWithJsonIgnoreProperties.setLastName("Mahajan");
		employeePojoWithJsonIgnoreProperties.setAge(29);
		employeePojoWithJsonIgnoreProperties.setGender("Male");
		employeePojoWithJsonIgnoreProperties.setSalary(12323.56);
		employeePojoWithJsonIgnoreProperties.setMarried(false);
		employeePojoWithJsonIgnoreProperties.setFullName("Animesh Prashant");
		employeePojoWithJsonIgnoreProperties.setEligibleForVote(false);

		// Converting a Java class object to a JSON payload as string
		ObjectMapper objectMapper = new ObjectMapper();
		String employeeJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(employeePojoWithJsonIgnoreProperties);
		System.out.println("Serialization...");
		System.out.println(employeeJson);
		
	}
}
Output
Serialization...
{
  "firstName" : "Amod",
  "lastName" : "Mahajan",
  "age" : 29,
  "salary" : 12323.56,
  "married" : false,
  "eligibleForVote" : false
}

We have ignored properties “gender” and “fullName” which are not part of JSON output above.

Deserialization Code
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 SerialiDeserialExampleWithJsonIgnoreProperties {
	
	@Test
	public void deserializationWithJsonIgnoreProperties() 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();
		//objectMapper.con
		EmployeePojoWithJsonIgnoreProperties employeePojoWithJsonIgnoreProperties = objectMapper.readValue(employeeString, EmployeePojoWithJsonIgnoreProperties.class);
		System.out.println("Deserialization...");
		System.out.println("First name :- "+employeePojoWithJsonIgnoreProperties.getFirstName());
		System.out.println("Last name :- "+employeePojoWithJsonIgnoreProperties.getLastName());
		System.out.println("Age :- "+employeePojoWithJsonIgnoreProperties.getAge());
		System.out.println("Gender :- "+employeePojoWithJsonIgnoreProperties.getGender());
		System.out.println("Salary :- "+employeePojoWithJsonIgnoreProperties.getSalary());
		System.out.println("Married :- "+employeePojoWithJsonIgnoreProperties.getMarried());
		System.out.println("Eligible for vote :- "+employeePojoWithJsonIgnoreProperties.getEligibleForVote());
		System.out.println("Full name :- "+employeePojoWithJsonIgnoreProperties.getFullName());
	}
}
Output
Deserialization...
First name :- Amod
Last name :- Mahajan
Age :- 29
Gender :- null
Salary :- 12323.56
Married :- false
Eligible for vote :- false
Full name :- null

Similarly, during deserialization, fields “gender” and “fullName” are ignored and have default values while retrieving.

Allow fields to participate only in serialization

If we have a requirement to allow some fields of a POJO class only for serialization but not for deserialization then that we can achieve this by setting allowGetters as true for @JsonIgnoreProperties annotation. This setting will make fields read-only.

POJO with allowGetters

Note below that to add more than one elements i.e. list of properties to be ignored and value for allowGetters, you need to use with the element name.

package JacksonTutorials;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(value = {"gender","fullName"}, allowGetters = true )
public class EmployeePojoWithJsonIgnoreProperties {

	// 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 Code
package JacksonTutorials;

import org.testng.annotations.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class SerialiDeserialExampleWithJsonIgnoreProperties {

	@Test
	public void serializationWithJsonIgnoreProperties() throws JsonProcessingException {
		// Just create an object of Pojo class
		EmployeePojoWithJsonIgnoreProperties employeePojoWithJsonIgnoreProperties = new EmployeePojoWithJsonIgnoreProperties();
		employeePojoWithJsonIgnoreProperties.setFirstName("Amod");
		employeePojoWithJsonIgnoreProperties.setLastName("Mahajan");
		employeePojoWithJsonIgnoreProperties.setAge(29);
		employeePojoWithJsonIgnoreProperties.setGender("Male");
		employeePojoWithJsonIgnoreProperties.setSalary(12323.56);
		employeePojoWithJsonIgnoreProperties.setMarried(false);
		employeePojoWithJsonIgnoreProperties.setFullName("Animesh Prashant");
		employeePojoWithJsonIgnoreProperties.setEligibleForVote(false);

		// Converting a Java class object to a JSON payload as string
		ObjectMapper objectMapper = new ObjectMapper();
		String employeeJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(employeePojoWithJsonIgnoreProperties);
		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
}

You can see in the above output fields were part of serialization as getters were allowed. You can ask that I said the above fields will be read-only but I can set value by calling setter methods. Read-only means you can not set the value using deserialization. See the next code.

Deserialization code
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 SerialiDeserialExampleWithJsonIgnoreProperties {
	
	@Test
	public void deserializationWithJsonIgnoreProperties() 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();
		//objectMapper.con
		EmployeePojoWithJsonIgnoreProperties employeePojoWithJsonIgnoreProperties = objectMapper.readValue(employeeString, EmployeePojoWithJsonIgnoreProperties.class);
		System.out.println("Deserialization...");
		System.out.println("First name :- "+employeePojoWithJsonIgnoreProperties.getFirstName());
		System.out.println("Last name :- "+employeePojoWithJsonIgnoreProperties.getLastName());
		System.out.println("Age :- "+employeePojoWithJsonIgnoreProperties.getAge());
		System.out.println("Gender :- "+employeePojoWithJsonIgnoreProperties.getGender());
		System.out.println("Salary :- "+employeePojoWithJsonIgnoreProperties.getSalary());
		System.out.println("Married :- "+employeePojoWithJsonIgnoreProperties.getMarried());
		System.out.println("Eligible for vote :- "+employeePojoWithJsonIgnoreProperties.getEligibleForVote());
		System.out.println("Full name :- "+employeePojoWithJsonIgnoreProperties.getFullName());
	}
}

Output
Deserialization...
First name :- Amod
Last name :- Mahajan
Age :- 29
Gender :- null
Salary :- 12323.56
Married :- false
Eligible for vote :- false
Full name :- null

We have values for fields “Gender” and “Full name” in JSON string but while deserialization they were ignored as setters were not allowed.

Allow fields to participate only in deserialization

If we have a requirement to allow some fields of a POJO class only for deserialization but not for serialization then that we can achieve this by setting allowSetters as true for @JsonIgnoreProperties annotation. This setting will make fields write-only.

POJO with allowSetters
package JacksonTutorials;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(value = {"gender","fullName"}, allowSetters = true )
public class EmployeePojoWithJsonIgnoreProperties {

	// 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 Code
package JacksonTutorials;

import org.testng.annotations.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class SerialiDeserialExampleWithJsonIgnoreProperties {

	@Test
	public void serializationWithJsonIgnoreProperties() throws JsonProcessingException {
		// Just create an object of Pojo class
		EmployeePojoWithJsonIgnoreProperties employeePojoWithJsonIgnoreProperties = new EmployeePojoWithJsonIgnoreProperties();
		employeePojoWithJsonIgnoreProperties.setFirstName("Amod");
		employeePojoWithJsonIgnoreProperties.setLastName("Mahajan");
		employeePojoWithJsonIgnoreProperties.setAge(29);
		employeePojoWithJsonIgnoreProperties.setGender("Male");
		employeePojoWithJsonIgnoreProperties.setSalary(12323.56);
		employeePojoWithJsonIgnoreProperties.setMarried(false);
		employeePojoWithJsonIgnoreProperties.setFullName("Animesh Prashant");
		employeePojoWithJsonIgnoreProperties.setEligibleForVote(false);

		// Converting a Java class object to a JSON payload as string
		ObjectMapper objectMapper = new ObjectMapper();
		String employeeJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(employeePojoWithJsonIgnoreProperties);
		System.out.println("Serialization...");
		System.out.println(employeeJson);
		
	}
}

Output
Serialization...
{
  "firstName" : "Amod",
  "lastName" : "Mahajan",
  "age" : 29,
  "salary" : 12323.56,
  "married" : false,
  "eligibleForVote" : false
}
Deserialization code
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 SerialiDeserialExampleWithJsonIgnoreProperties {

	@Test
	public void deserializationWithJsonIgnoreProperties() 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();
		//objectMapper.con
		EmployeePojoWithJsonIgnoreProperties employeePojoWithJsonIgnoreProperties = objectMapper.readValue(employeeString, EmployeePojoWithJsonIgnoreProperties.class);
		System.out.println("Deserialization...");
		System.out.println("First name :- "+employeePojoWithJsonIgnoreProperties.getFirstName());
		System.out.println("Last name :- "+employeePojoWithJsonIgnoreProperties.getLastName());
		System.out.println("Age :- "+employeePojoWithJsonIgnoreProperties.getAge());
		System.out.println("Gender :- "+employeePojoWithJsonIgnoreProperties.getGender());
		System.out.println("Salary :- "+employeePojoWithJsonIgnoreProperties.getSalary());
		System.out.println("Married :- "+employeePojoWithJsonIgnoreProperties.getMarried());
		System.out.println("Eligible for vote :- "+employeePojoWithJsonIgnoreProperties.getEligibleForVote());
		System.out.println("Full name :- "+employeePojoWithJsonIgnoreProperties.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

Since setters were allowed for ignored fields “gender” and “fullName”, they have participated in the deserialization process but not in serialization.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *