REST Assured Tutorial 75 – What Is Serialization And Deserialization In Rest Assured?

Introduction

As a part of RestAssured Tutorials End To End, we will learn about the Serialization and Deserialization in Rest Assured in this post.

In fact, Serialization and Deserialization are not specific to RestAssured. It is related to programming language i.e. Java here.

What is Serialization and Deserialization?

Serialization is a conversion of the state of a Java object to a byte stream and Deserialization is the reverse of it i.e. conversion of a byte stream to corresponding Java object. A serialized object can be stored in files, external sources, databases etc and can also be transferred over networks.

To make a Java object serializable, either its class or any of its superclasses implements either the Serializable interface or its subinterface Externalizable. A class that implements Serializable or Externalizable are actually Java Beans.

A Plain Old Java Object i.e. POJO has no such restriction. We generally use POJOs to create JSON payload and convert JSON response payload to Java objects. Converting a POJO object to a JSON object is Serialization and converting a JSON object to a POJO object is called deserialization. These conversions can be done with help of Java libraries like Jackson, Gson etc. We don’t need to implement the required interfaces for POJO classes.

We will use Jackson Databind in this post. You can either download Jackson jar or use its dependency in your Maven/Gradle project. I am using below latest dependency of Jackson Databind for this post –

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.1</version>
</dependency>

Conversion of POJO Object to JSON Object in Rest Assured Using Jackson

I will create a very simple POJO as below:-

package pojo;

public class Person {

	private String name;
	private int age;

	public String getName() {
		return name;
	}

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

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}

To convert any object of the above Pojo class to a JSON object, we need to perform the below steps:-

  1. Create an object of POJO class.
  2. Set values of properties of Pojo class
  3. Create an object of ObjectMapper class provided by Jackson.
  4. Use overloaded writeXXXXXX() method as per need. I will convert POJO object to JSON string.
package SerializationDeserialization;

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

import pojo.Person;

public class ConvertPojoToJson {

	public static void main(String[] args) throws JsonProcessingException {
		// Create object of Pojo and set values
		Person person = new Person();
		person.setName("Amod");
		person.setAge(30);
		// ObjectMapper class to serialize Pojo object to JSON
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(person);
		System.out.println("Json Object is :-");
		System.out.println(json);
	}

}

Output

Json Object is :-
{
  "name" : "Amod",
  "age" : 30
}

Conversion of JSON Object to POJO Object in Rest Assured Using Jackson

To convert a JSON object to a POJO object we can use ObjectMapper again with its overloaded readXXXX() methods. Please note we can also convert a JSON object to any other type i.e. Map and List instead of POJO.

package SerializationDeserialization;

import java.util.Map;

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

import pojo.Person;

public class ConvertJsonToPojo {
	public static void main(String[] args) throws JsonProcessingException {
		String jsonObject = "{\r\n" + "  \"name\" : \"Amod\",\r\n" + "  \"age\" : 30\r\n" + "}";
		ObjectMapper objectMapper = new ObjectMapper();
		// Deserialize JSON object to POJO Object
		Person person = objectMapper.readValue(jsonObject, Person.class);
		// Once we get Person Object we can use getter method to fetch values
		System.out.println("Name is :" + person.getName());
		System.out.println("Age is  :" + person.getAge());

		// Deserialize JSON object to Map Object
		Map<String, Object> personAsMap = objectMapper.readValue(jsonObject, Map.class);
		// Once we get Map Object we can use keys to fetch values
		System.out.println("Name is :" + personAsMap.get("name"));
		System.out.println("Age is  :" + personAsMap.get("age"));
	}

}

Output

Name is :Amod
Age is  :30
Name is :Amod
Age is  :30

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe to my YouTube channel.
#ThanksForReading
#HappyLearning

2 thoughts on “REST Assured Tutorial 75 – What Is Serialization And Deserialization In Rest Assured?

Leave a Reply

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