Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 02/08/2025 By admin

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.

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 –


    com.fasterxml.jackson.core
    jackson-databind
    2.13.1

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);
        }

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

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 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"));
        }

}
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

Uncategorized

Post navigation

Previous Post: Git Tutorial 21 – Git Clean – Remove Untracked Files From The Working Tree
Next Post: Contact

Related Posts

August 27, 2017 – Make Selenium Easy Uncategorized
August 15, 2018 – Make Selenium Easy Uncategorized
Hierarchy of Classes & Interfaces of WebDriver Interface in Selenium WebDriver – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
Handling Website Popups In Selenium webdriver | Make Selenium Easy Uncategorized
fluent wait in selenium Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark