Serialization and Deserialization

In this lesson, we will see what serialization and deserialization of requests is.

What is serialization?

Serialization is the process of converting objects into a stream of data.

What is deserialization?

Deserialization is the process of converting a stream of data into objects.

The main purpose of serialization and deserialization is to persist the data and recreate it whenever needed.

We have considered the Rest Assured library for making REST API calls. We will keep the scope within those capabilities of Rest Assured and the libraries it depends on.

As we keep learning about REST API automation and the data that is exchanged between client and server is of JSON format, we will learn how to serialize objects into a stream of JSON data and deserialize stream of data to objects that are exchanged between the REST web service.

Rest Assured can use the Jackson 2 library, GSON library or Jackson library for serialization and deserialization. The internal behavior of io.restassured.mapper.ObjectMapper is dependent on the library in the classpath.

Sample data for demonstration & understanding the data

We will use the Jackson 2 library for serialization and deserialization purposes, for which we will ensure the following dependency from here is added:

  • Maven

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.10.3</version>
    </dependency>
    
  • Gradle

    compile 'com.fasterxml.jackson.core jackson-databind:2.10.3'
    

Let us consider the class Student for demonstration purposes.

import com.fasterxml.jackson.annotation.JsonProperty;

public class Student {

	@JsonProperty("id")
	private Long id;

	@JsonProperty("first_name")
	private String firstName;

	@JsonProperty("last_name")
	private String lastName;

	@JsonProperty("gender")
	private String gender;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	@Override
	public String toString() {
		return String.format("Student [id=%s, firstName=%s, lastName=%s, gender=%s]", id, firstName, lastName, gender);
	}

}

The above class Student contains the following:

  • Fieldsid, firstName, lastName, gender
  • Getters – for fetching the field values
  • Setters – for setting the field values
  • toString() – for printing the object

As we can see in the Student class, all the fields are annotated with @JsonProperty. The purpose of having that annotation over the fields is that, during the process of serialization to JSON or deserialization from JSON, we need to know what key the fields should be mapped to.

As per Java conventions, we tend to name all the field names in camel case. The Jackson library, by default, takes the field name as the key during the process of serialization and deserialization. However, sometimes, we may want to have a different key for the field during the process. For that purpose, we use @JsonProperty which overrides the default behavior and the String given in the annotation will be taken as the key for that field.

@JsonProperty can be annotated over setter methods also. If the annotation is given in both places, we get the following exception:

java.lang.IllegalStateException: Conflicting/ambiguous property name definitions (implicit name 'firstName'): found multiple explicit names: [f_name, first_name], but also implicit accessor: [method restassured.Student#getFirstName(0 params)][visible=true,ignore=false,explicitName=false]

The Jackson library provides us with so many other annotations to help us override the default behavior of the library upon fields during the process of serialization and deserialization. To know more, please follow this link.

How to serialize an object?

Now, we will see how to set Java objects to request the body of an API so that Rest Assured can serialize the object into a stream of JSON data before making the API call.

Get hands-on with 1400+ tech skills courses.