POJO: Serialization and Deserialization in Java – Devstringx

Back to Blog
Pojo Serialization Blog

POJO: Serialization and Deserialization in Java – Devstringx

What are serialization and deserialization in Java?

  • Serialization: Serialization is a mechanism or a concept by which we can convert the java object into a byte stream. Example – converting java object in JSON.
  • Deserialization: The reverse of serialization calls deserialization where a byte stream converts into a java object. Example – converting Json into a java object.

So, to do serialization and deserialization we use POJO Class in Java.

What Is POJO Class in Java?

POJO is a Plain Old Java Object. A POJO categorize not so much by what it has to have to be a POJO but it has to not have. There are 3 Basic things –

  • It has to not extend to any other class. So, it can’t be a child class of another class.
  • It can’t implement any interfaces.
  • No Outside annotations.

Let’s Understand with an example how we do serialization and deserialization in java –

We have a Post API in which we need to post a Payload which looks like this –

{
  "id": 0,
  "category": {
    "id": 0,
    "name": "string"
  },
  "name": "Cattie",
  "photoUrls": [
    "string"
  ],
  "tags": [
    {
      "id": 0,
      "name": "string"
    }
  ],
  "status": "available"
}

To pass this Payload in Our Automation test we need to do the Serialization.

Go tohttps://www.jsonschema2pojo.org

Post your Json in the option given. Choose your class name, Choose Source type as – JSON, Annotation style as None, and select the include getter setter option.

Click On Preview

As shown in the preview, create Classes in your framework –

----------------------------------com.example.Category.java-----------------------------------

packagecom.example;

importjavax.annotation.Generated;

@Generated("jsonschema2pojo")
publicclass Category {

private Integer id;
private String name;

public Integer getId() {
return id;
}

publicvoidsetId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

publicvoidsetName(String name) {
this.name = name;
}

}
-----------------------------------com.example.PetsData.java-----------------------------------

packagecom.example;

importjava.util.List;
importjavax.annotation.Generated;

@Generated("jsonschema2pojo")
publicclassPetsData {

private Integer id;
private Category category;
private String name;
private List<String>photoUrls = null;
private List<Tag> tags = null;
private String status;

public Integer getId() {
return id;
}

publicvoidsetId(Integer id) {
this.id = id;
}

public Category getCategory() {
return category;
}

publicvoidsetCategory(Category category) {
this.category = category;
}

public String getName() {
return name;
}

publicvoidsetName(String name) {
this.name = name;
}

public List<String>getPhotoUrls() {
returnphotoUrls;
}

publicvoidsetPhotoUrls(List<String>photoUrls) {
this.photoUrls = photoUrls;
}

public List<Tag>getTags() {
return tags;
}

publicvoidsetTags(List<Tag> tags) {
this.tags = tags;
}

public String getStatus() {
return status;
}

publicvoidsetStatus(String status) {
this.status = status;
}

}
-----------------------------------com.example.Tag.java-----------------------------------

packagecom.example;

importjavax.annotation.Generated;

@Generated("jsonschema2pojo")
publicclass Tag {

private Integer id;
private String name;

public Integer getId() {
return id;
}

publicvoidsetId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

publicvoidsetName(String name) {
this.name = name;
}

}

Recommended to Read- Retrieve Test Data from Json file in C# Selenium Framework

Now Create the object of these classes in your test-

PetsDatapetData = new PetsData();
    petData.setId(0);
    petData.setName("Cattie");
    petData.setStatus("available");
    
    Category category = new Category();
    category.setId(100);
    category.setName("cat1");
    petData.setCategory(category);
    
    Tag tag=new Tag();
    tag.setId("1000");
    tag.setName("Tag1");
    
    Tag tag1 =new Tag();
    tag.setId("1001");
    tag.setName("Tag2");
    
    List<Tag> tags =new ArrayList();
    tags.add(tag);
    tags.add(tag1);
    
    petData.setTags(tags);
    
    List<String> photo =new ArrayList();
    tags.add(Url1);
    tags.add(Url2);
    
    petData.setPhotoUrls(photo);
    
    Response response = given().header("accept", "application/json").header({"content-Type", "application/json").body(petData).post("API");
    System.out.println(response.asPrettyString());

The output of this test will be the JSON Response as Shown above.

Now let’s see how can we do Deserialization i.e to convert JSON data in Java Object

PetsDatapetsResponse = response.body().as(PetsData.class);
System.out.println(petsResponse.getName());
System.out.println(petsResponse.getId());
System.out.println(petsResponse.getCategory().getName());

FAQs

  • What is deserialization in Java?

Deserialization is the opposite procedure, in which the actual Java object is recreated in memory using a byte stream. The object is persistent thanks to this approach. The generated byte stream is cross-platform. Therefore, an object that has been serialized on one platform can be deserialized on another.

  • Why do we need serialization and deserialization in rest assured?

The process of transforming a stream of data into objects is known as deserialization. Data persistence and re-creation are the primary goals of serialization and deserialization. For making calls to the REST API, we have taken into account the Rest Assured library.

  • What is the use of POJO in Java?

Plain Old Java Object is referred to as POJO. It is a typical Java object that doesn’t have any additional restrictions than those imposed by the Java Language Specification and doesn’t need the classpath. POJOs use to make a program easier to read and more adaptable.

  • What is POJO in Java for example?

Plain Old Java Object refer to as POJO. It is a typical Java object that doesn’t have any additional restrictions than those imposed by the Java Language Specification and doesn’t need the classpath. POJOs are used to make a program easier to read and more adaptable.

If you are interested in even more software testing-related articles and information from us here at Devstringx, then we have a lot to choose from for you.

Share this post

Back to Blog