Do you think IIT Guwahati certified course can help you in your career?
No
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.
Before starting with the main topic, let us first see what POJO is.
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;
}
}
Serialization and Deserialization in Java
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());
}
}
You can also try this code with Online Java Compiler
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.
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.
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
It is challenging to handle since any property that has to be ignored must be marked with @JsonIgnore.
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.
@JsonIgnoreProperties(value = {"racketName"}, allowGetters = true) - Property that can be enabled to allow "getters" to be used
@JsonIgnoreProperties(value = {"racketName"}, allowSetters = true) - Property that can be enabled to allow "setters" to be used
@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());
}
}
You can also try this code with Online Java Compiler
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());
}
}
You can also try this code with Online Java Compiler
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