Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
POJO
2.1.
Example
3.
Serialization and Deserialization in Java
4.
How Getter and Setter Methods Matter for Serialization Deserialization Using POJO
4.1.
POJO Class Without Setter Methods
4.2.
POJO Class Without Getter Methods
5.
Exclude the Field of POJO from Serialization and Deserialization using @JsonIgnore Annotation
5.1.
Limitations of @JsonIgnore
6.
Exclude the Field of POJO from Serialization or Deserialization or Both using @JsonIgnoreProperties Annotation
6.1.
POJO Class with allowGetters
6.2.
POJO Class with allowSetters
6.3.
POJO Class with @JsonIgnoreProperties
7.
Frequently Asked Questions
7.1.
What is the advantage of serialization?
7.2.
What do you mean by JSON serialization?
7.3.
How can we deserialize JSON objects?
7.4.
What types of objects can be serialized?
7.5.
Can a non-serialized object be sent over a network?
8.
Conclusion
Last Updated: Mar 27, 2024
Hard

Serialization and Deserialization of POJO in Rest Assured

Author Aashna Luthra
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

POJO stands for Plain Old Java Object. POJO is just like a regular object of Java. In this article, we will learn the concept of serialization and deserialization of POJO. We will discuss methods associated with these concepts and why they are essential.

intro image

Before starting with the main topic, let us first see what POJO is.

POJO

pojo

POJO stands for Plain Old Java Object. POJO is just like a regular object of Java. The difference is that POJO is not restricted by anything other than the restrictions imposed by the java language specification. The task of POJO is to increase the reusability of the code. It is also used to improve the readability of the code. One crucial point for POJO is that there is no special naming convention followed in the case of a POJO.

Let us see an example of POJO.

Example

Below is an example of a POJO class with some private properties and their public accessors(getter and setter methods).

public class POJOClass {
	
	/*Data members*/
	private String badmintonBrand;
	private String racketName;
		
	/*Getters and Setters*/
	public String getBadmintonBrand() {
		return badmintonBrand;
	}
	public void setBadmintonBrand(String badmintonBrand){
		this.badmintonBrand = badmintonBrand;
	}
	public String getRacketName() {
		return racketName;
	}
	public void setRacketName(String racketName) {
		this.racketName = racketName;
	}
}
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Serialization and Deserialization in Java

let us learn

In simple words, serialization converts the java object to a byte stream. As the name suggests, deserialization works the opposite of serialization. Deserialization is the conversion of this byte stream back to the actual object.


Serialization – Convert the class's objects of POJO to JSON or Object representation.

Deserialization – Convert a JSON object to an object of the class of POJO.

Let us see the code implementation of the above two concepts.
 

SerializationDeserialization.java

package javaPackageName;

import org.testng.annotations.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;

public class SerializationDeserialization {
	
	/*Data members*/
	private String badmintonBrand;
	private String racketName;
		
	/*Getters and Setters*/
	public String getBadmintonBrand() {
		return badmintonBrand;
	}
	public void setBadmintonBrand(String badmintonBrand){
		this.badmintonBrand = badmintonBrand;
	}
	public String getRacketName() {
		return racketName;
	}
	public void setRacketName(String racketName) {
		this.racketName = racketName;
	}
	
	@Test
	public void Serialization_Deserialization() throws JsonProcessingException {
		
		/*Creating object of class*/
		SerializationDeserialization objpojo = new SerializationDeserialization();
		
		/*Set Values*/
		objpojo.setBadmintonBrand("Yonex");
		objpojo.setRacketName("Voltric 50 E-tune");
		
		/*Class Object -> String(JSON Object PAYLOAD)*/
		System.out.println("Serialization...");
		ObjectMapper objectMapper = new ObjectMapper();
		String objpojoJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(objpojo);
		System.out.println(objpojoJson);
		
		/*String(JSON Object PAYLOAD) -> Class Object*/
		System.out.println("DeSerialization...");
		SerializationDeserialization objpojo2 = objectMapper.readValue(objpojoJson, SerializationDeserialization.class);
		System.out.println("Badminton Brand : "+objpojo2.getBadmintonBrand());
		System.out.println("Racket Name : "+objpojo2.getRacketName());
	}
}


Output: 

output

How Getter and Setter Methods Matter for Serialization Deserialization Using POJO

To understand how getter and setter methods work and how they matter. Let us first try to serialize and deserialize without them one by one.

POJO Class Without Setter Methods

Here is a POJO class with only getter methods.


SerializationDeserialization.java

package javaPackageName;

import org.testng.annotations.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;

public class SerializationDeserialization {
	
	/*Data members*/
	private String badmintonBrand;
	private String racketName;
		
	/*Getters and Setters*/
	public String getBadmintonBrand() {
		return badmintonBrand;
	}
	/*public void setBadmintonBrand(String badmintonBrand){
		this.badmintonBrand = badmintonBrand;
	}*/
	public String getRacketName() {
		return racketName;
	}
	/*public void setRacketName(String racketName) {
		this.racketName = racketName;
	}*/
	
	@Test
	public void Serialization_Deserialization() throws JsonProcessingException {
		
		/*Creating object of class*/
		SerializationDeserialization objpojo = new SerializationDeserialization();
		
		/*Set Values*/
		/*objpojo.setBadmintonBrand("Yonex");
		objpojo.setRacketName("Voltric 50 E-tune");*/
		
		/*Class Object -> String(JSON Object PAYLOAD)*/
		System.out.println("Serialization...");
		ObjectMapper objectMapper = new ObjectMapper();
		String objpojoJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(objpojo);
		System.out.println(objpojoJson);
		
		/*String(JSON Object PAYLOAD) -> Class Object*/
		System.out.println("DeSerialization...");
		SerializationDeserialization objpojo2 = objectMapper.readValue(objpojoJson, SerializationDeserialization.class);
		System.out.println("Badminton Brand : "+objpojo2.getBadmintonBrand());
		System.out.println("Racket Name : "+objpojo2.getRacketName());
	}
}


Output:

output

We cannot serialize with expected values since we do not have setter methods. As you can see in the output, all class-level fields are initialized with default values that will be used for serialization.

But the absence of setter methods had no negative effects on deserialization. That means we can conclude that the getter method matters for deserialization. But still, we are left with the part about how the setter method matters for serialization. So let us write the POJO class without the getter method and implement only the serialization part.

POJO Class Without Getter Methods

Here is a POJO class with only setter methods.


SerializationDeserialization.java

package javaPackageName;

import org.testng.annotations.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;

public class SerializationDeserialization {
	
	/*Data members*/
	private String badmintonBrand;
	private String racketName;
		
	/*Getters and Setters*/
	/*public String getBadmintonBrand() {
		return badmintonBrand;
	}*/
	public void setBadmintonBrand(String badmintonBrand){
		this.badmintonBrand = badmintonBrand;
	}
	/*public String getRacketName() {
		return racketName;
	}*/
	public void setRacketName(String racketName) {
		this.racketName = racketName;
	}
	
	@Test
	public void Serialization_Deserialization() throws JsonProcessingException {
		
		/*Creating object of class*/
		SerializationDeserialization objpojo = new SerializationDeserialization();
		
		/*Set Values*/
		objpojo.setBadmintonBrand("Yonex");
		objpojo.setRacketName("Voltric 50 E-tune");
		
		/*Class Object -> String(JSON Object PAYLOAD)*/
		System.out.println("Serialization...");
		ObjectMapper objectMapper = new ObjectMapper();
		String objpojoJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(objpojo);
		System.out.println(objpojoJson);
	}
}


Output:

output


Error:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class javaPackageName.SerializationDeserialization and no properties discovered to create BeanSerializer (to avoid the exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS).
...


We can conclude that we cannot serialize if we only have setter methods.

Exclude the Field of POJO from Serialization and Deserialization using @JsonIgnore Annotation

To exclude the field of POJO from serialization and deserialization, we can use @JsonIgnore annotation.


Note - To use the @JsonIgnore annotation, we need to ensure we have the latest dependency of Jackson Databind in our project classpath.

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.11.1</version>
</dependency>

For the latest version, click here.


If we want to ignore any property in our POJO class from serialization and deserialization, we can use @JsonIgnore annotation on that property.


Example - 

@JsonIgnore
private String racketName;

Now let us see code implementation.


SerializationDeserialization.java

package javaPackageName;

import org.testng.annotations.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;

public class SerializationDeserialization {
	
	/*Data members*/
	private String badmintonBrand;
	@JsonIgnore
	private String racketName;
		
	/*Getters and Setters*/
	public String getBadmintonBrand() {
		return badmintonBrand;
	}
	public void setBadmintonBrand(String badmintonBrand){
		this.badmintonBrand = badmintonBrand;
	}
	public String getRacketName() {
		return racketName;
	}
	public void setRacketName(String racketName) {
		this.racketName = racketName;
	}
	
	@Test
	public void Serialization_Deserialization() throws JsonProcessingException {
		
		/*Creating object of class*/
		SerializationDeserialization objpojo = new SerializationDeserialization();
		
		/*Set Values*/
		objpojo.setBadmintonBrand("Yonex");
		objpojo.setRacketName("Voltric 50 E-tune");
		
		/*Class Object -> String(JSON Object PAYLOAD)*/
		System.out.println("Serialization...");
		ObjectMapper objectMapper = new ObjectMapper();
		String objpojoJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(objpojo);
		System.out.println(objpojoJson);
		
		/*String(JSON Object PAYLOAD) -> Class Object*/
		System.out.println("DeSerialization...");
		SerializationDeserialization objpojo2 = objectMapper.readValue(objpojoJson, SerializationDeserialization.class);
		System.out.println("Badminton Brand : "+objpojo2.getBadmintonBrand());
		System.out.println("Racket Name : "+objpojo2.getRacketName());
	}
}


Output:

output


We can notice in the output that racketName is excluded from serialization. And shows a "null" value for "Racket Name" which is the default value instead of the "Voltric 50 E-tune" that we tried to set.

So we can say we have excluded the racketName(Field) of POJO from serialization and deserialization.

Limitations of @JsonIgnore

  1. It is challenging to handle since any property that has to be ignored must be marked with @JsonIgnore.
  2. Anywhere you use the annotation @JsonIgnore, such as a property or one of its accessors, that property will be ignored during serialization and deserialization. We have no control over whether we want a property to be ignored by serialization or deserialization only.


Alright! Now let us learn how we can exclude a particular field of POJO from serialization, deserialization, or both.

Exclude the Field of POJO from Serialization or Deserialization or Both using @JsonIgnoreProperties Annotation

To exclude a particular field of POJO from serialization or deserialization or from both, we can use @JsonIgnoreProperties. We can use this annotation to either suppress the serialization of properties (during serialization) or ignore the processing of JSON properties read (during deserialization).
 

Note - To use the @JsonIgnoreProperties annotation, we need to ensure we have the latest dependency of Jackson Databind in our project classpath.

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.11.1</version>
</dependency>

For the latest version, click here.


Example - 

  1. @JsonIgnoreProperties(value = {"racketName"}, allowGetters = true) - Property that can be enabled to allow "getters" to be used
  2. @JsonIgnoreProperties(value = {"racketName"}, allowSetters = true) - Property that can be enabled to allow "setters" to be used
  3. @JsonIgnoreProperties({"racketName"}) - To exclude racketName(field) of POJO from both serialization and deserialization. 

Now let us see code implementation.

POJO Class with allowGetters

Setting allowGetters to true for the @JsonIgnoreProperties annotation will allow some fields of a POJO class to be used just for serialization and not deserialization. With this setting, fields will become read-only.


SerializationDeserialization.java

package javaPackageName;

import org.testng.annotations.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonProcessingException;

@JsonIgnoreProperties(value = {"racketName"}, allowGetters = true)
public class SerializationDeserialization {
	
	/*Data members*/
	private String badmintonBrand;

	private String racketName;
		
	/*Getters and Setters*/
	public String getBadmintonBrand() {
		return badmintonBrand;
	}
	public void setBadmintonBrand(String badmintonBrand){
		this.badmintonBrand = badmintonBrand;
	}
	public String getRacketName() {
		return racketName;
	}
	public void setRacketName(String racketName) {
		this.racketName = racketName;
	}
	
	@Test
	public void Serialization_Deserialization() throws JsonProcessingException {
		
		/*Creating object of class*/
		SerializationDeserialization objpojo = new SerializationDeserialization();
		
		/*Set Values*/
		objpojo.setBadmintonBrand("Yonex");
		objpojo.setRacketName("Voltric 50 E-tune");
		
		/*Class Object -> String(JSON Object PAYLOAD)*/
		System.out.println("Serialization...");
		ObjectMapper objectMapper = new ObjectMapper();
		String objpojoJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(objpojo);
		System.out.println(objpojoJson);
		
		/*String(JSON Object PAYLOAD) -> Class Object*/
		System.out.println("DeSerialization...");
		SerializationDeserialization objpojo2 = objectMapper.readValue(objpojoJson, SerializationDeserialization.class);
		System.out.println("Badminton Brand : "+objpojo2.getBadmintonBrand());
		System.out.println("Racket Name : "+objpojo2.getRacketName());
	}
}


Output:

output

POJO Class with allowSetters

Setting allowSetters to true for the @JsonIgnoreProperties annotation will allow some fields of a POJO class to be used only for deserialization and not for serialization. Fields will be made write-only with this option.


SerializationDeserialization.java

package javaPackageName;

import org.testng.annotations.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonProcessingException;

@JsonIgnoreProperties(value = {"racketName"}, allowSetters = true)
public class SerializationDeserialization {
	
	/*Data members*/
	private String badmintonBrand;

	private String racketName;
		
	/*Getters and Setters*/
	public String getBadmintonBrand() {
		return badmintonBrand;
	}
	public void setBadmintonBrand(String badmintonBrand){
		this.badmintonBrand = badmintonBrand;
	}
	public String getRacketName() {
		return racketName;
	}
	public void setRacketName(String racketName) {
		this.racketName = racketName;
	}
	
	@Test
	public void Serialization_Deserialization() throws JsonProcessingException {
		
		/*Creating object of class*/
		SerializationDeserialization objpojo = new SerializationDeserialization();
		
		/*Set Values*/
		objpojo.setBadmintonBrand("Yonex");
		objpojo.setRacketName("Voltric 50 E-tune");
		
		/*Class Object -> String(JSON Object PAYLOAD)*/
		System.out.println("Serialization...");
		ObjectMapper objectMapper = new ObjectMapper();
		String objpojoJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(objpojo);
		System.out.println(objpojoJson);
		
		/*String(JSON Object PAYLOAD) -> Class Object*/
		System.out.println("DeSerialization...");
		SerializationDeserialization objpojo2 = objectMapper.readValue(objpojoJson, SerializationDeserialization.class);
		System.out.println("Badminton Brand : "+objpojo2.getBadmintonBrand());
		System.out.println("Racket Name : "+objpojo2.getRacketName());
	}
}


Output:

output

POJO Class with @JsonIgnoreProperties

We can use POJO Class with @JsonIgnoreProperties to exclude the field of POJO from both serialization and deserialization.


SerializationDeserialization.java

package javaPackageName;

import org.testng.annotations.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonProcessingException;

@JsonIgnoreProperties({"racketName"})
public class SerializationDeserialization {
	
	/*Data members*/
	private String badmintonBrand;

	private String racketName;
		
	/*Getters and Setters*/
	public String getBadmintonBrand() {
		return badmintonBrand;
	}
	public void setBadmintonBrand(String badmintonBrand){
		this.badmintonBrand = badmintonBrand;
	}
	public String getRacketName() {
		return racketName;
	}
	public void setRacketName(String racketName) {
		this.racketName = racketName;
	}
	
	@Test
	public void Serialization_Deserialization() throws JsonProcessingException {
		
		/*Creating object of class*/
		SerializationDeserialization objpojo = new SerializationDeserialization();
		
		/*Set Values*/
		objpojo.setBadmintonBrand("Yonex");
		objpojo.setRacketName("Voltric 50 E-tune");
		
		/*Class Object -> String(JSON Object PAYLOAD)*/
		System.out.println("Serialization...");
		ObjectMapper objectMapper = new ObjectMapper();
		String objpojoJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(objpojo);
		System.out.println(objpojoJson);
		
		/*String(JSON Object PAYLOAD) -> Class Object*/
		System.out.println("DeSerialization...");
		SerializationDeserialization objpojo2 = objectMapper.readValue(objpojoJson, SerializationDeserialization.class);
		System.out.println("Badminton Brand : "+objpojo2.getBadmintonBrand());
		System.out.println("Racket Name : "+objpojo2.getRacketName());
	}
}


Output:

output

 

We hope you have learned everything about the serialization and deserialization of POJO in Rest assured. 

Frequently Asked Questions

What is the advantage of serialization?

The main advantage of serialization is that you can transfer objects over a network when converted into a byte stream.

What do you mean by JSON serialization?

JSON serialization refers to converting between JSON and equivalent foundation objects.

How can we deserialize JSON objects?

Deserializing JSON objects involve creating a class with properties and then calling the JsonSerializer.Deserialize method.

What types of objects can be serialized?

All objects in Java are not serializable. An object can be serialized if its classes or superclass implements either the java.io.Serializable interface or the java.io.Externalizable interface.

Can a non-serialized object be sent over a network?

No, an object that is not serialized or does not support the serializable interface cannot be sent over a network. You will encounter the NotSerializableException.

Conclusion

In this article, we explored various things related to serialization and deserialization of POJO in Rest assured, leaving no stone unturned. We explored how getter and setter methods matter for the serialization and deserialization of POJO in Rest assured. Later we studied excluding the field of POJO from serialization or deserialization or from both.

We believe this article on serialization and deserialization of POJO in Rest assured was helpful. To learn more about Rest assured, check out our articles on


You may refer to our Guided Path on Code Studios for enhancing your skill set on DSACompetitive ProgrammingSystem Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Happy Learning Ninja!

Previous article
Ignore Default and Null values in Payload in Rest Assured
Next article
Ignore Unknown Properties During Deserialization in Rest Assured
Live masterclass