top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

The Importance of Understanding Different Deserialization Methods

In this article,we can see the serialization and the different methods of De-serialization in detail.

In Java, Serialization is a process of converting an object from its current state to a stream of bytes which can be written to a file or transported through a network or stored in a database.

De-serialization is rebuilding the object from a stream of bytes.



Why use serialization and deserialization in REST API automation?

To access the REST service,we have to transfer data between the client and the REST service .Using serialization,an object having request details can be serialized into JSON/XML format which can be handled by REST service.And using deserialization REST response which can be in JSON/XML format can be converted back to objects which be consumed by the client.

To put in nutshell,Serialization is the process of converting POJO into JSON and De-serialization is converting JSON back into POJO


Before going deep into Serialization and De-serialization,we will see POJO in detail.

what is POJO?

POJO in Java stands for Plain Old Java Object. It is an ordinary object, which is not bound by any special restriction. The POJO file does not require any special classpath. It increases the readability & re-usability of a Java program.

POJOs are now widely accepted due to their easy maintenance. They are easy to read and write. A POJO class does not have any naming convention for properties and methods. It is not tied to any Java Framework; any Java Program can use it.

Generally, a POJO class contains variables and their Getters and setters


Properties of POJO class

Below are some properties of POJO class:

  • The POJO class must be public.

  • It must have a public default constructor.

  • It may have the arguments constructor.

  • All objects must have some public Getters and Setters to access the object values by other Java Programs.

  • The object in the POJO Class can have any access modifies such as private, public, protected. But, all instance variables should be private for improved security of the project.

  • A POJO class should not extend predefined classes.

  • It should not implement prespecified interfaces.

  • It should not have any prespecified annotation.


What are JSON and its structure?

JSON (JavaScript Object Notation) is a lightweight data-interchange format. JSON data is represented as key-value pairs, similar to a dictionary or a hash table.

Below are three components of JSON

  • JSON Object - A JSON object is an unordered collection of key-value pairs, where the keys are strings and the values can be any valid JSON data type enclosed in curly braces {}.

  • Key and values (JSON Data type)- The keys are always strings, followed by a colon, and the values can be any valid JSON data type such as strings, numbers, Boolean, arrays or objects.

  • JSON array - A JSON array is an ordered list of values enclosed in square brackets []


Before creating POJO class,add the following dependencies in pom.xml

Now we ll see the creation of POJO

For example,we can create an Employee POJO to define its objects.

Below is an example of POJO class:

The above Employee class is an example of an Employee POJO class.If you are working on Eclipse,you can easily

generate Setters and Getters by right click on the Java Program and navigate to Source->Generate Getters and Setters.


Right click on the Java program and select Generate Getters and Setters

Now,click on the Generate option given at the bottom of the Generate window. It will  auto-generate setters and getters.


Now,set the values for firstname,last name and email using setter method and perform Serialization using object mapper.

Below is the output of Serialization is Jsonobject

Now we can perform Deserialization with different methods

  1. Deserialization using object mapper

  2. Deserialization using Jway JsonPath

  3. Deserialization using Restassured Json Path

  4. Deserialization using Restassured As function


Deserialization using object mapper:

In this method,we are deserializing the json object using object mapper.

output :





Deserialization using Jway Jsonpath:

In this method,we are deserializing the json object using Jway Json path,so we are importing Jway Json path


output :





Deserialization using Restassured Json path:

In this method,we are deserializing the json object using Restassured Json path,so we are importing Restassured Json path

output:






Deserialization using Rest assured As function:

In this method ,we are deserializing Json object and getting the first_name,last_name,email for the Id :3

Below is the Jsonobject running in localhost:3000/employees


















Using As function,we are extracting the responsebody of POJO.

Using get method, we are getting the values for keys(first_name,last_name,email).


Output:





With the above methods, we can perform De-serialization effectively.

Conclusion

Serialization and deserialization are crucial in REST Assured API testing as they facilitate smooth data exchange between the client and the server, enhance test script readability, maintainability, and automation capabilities, and ensure type safety and consistency in the tests.

Different serialization and deserialization methods are vital for effectively handling various data formats and structures in API testing and development. They provide flexibility, performance optimization, and compatibility with different systems and requirements. Using the appropriate method based on the context ensures efficient data exchange, integrity, and application performance.

53 views1 comment

1 comentario

Obtuvo 0 de 5 estrellas.
Aún no hay calificaciones

Agrega una calificación
Invitado
03 jun
Obtuvo 5 de 5 estrellas.

Nice article to understand clearly the concepts.

Me gusta
bottom of page