REST Assured Tutorial 34 – Serialization – Java Object To JSON Object Using Gson API

As a part of End to End REST Assured Tutorial, in this post, We will learn the “Serialization” concept where we will create JSON Object or Payload from Java Objects using Gson library.

We have learned about POJOs in the previous posts. If you have not refereed those posts then I will advise you to go through about POJO concepts here:-

What Is Plain Old Java Object (POJO)?

How To Create POJO Classes Of A JSON Object Payload?

How To Create POJO Classes Of A JSON Array Payload?

How To Create POJO Classes Of A Nested JSON Payload?

Now we will learn how to create JSON objects using POJOs. To create a JSON object (JSON) from Java Objects( POJOs) is called Serialization. For this, we can use any JSON parser APIs. We have learned serialization using Jackson API already. In this post, we will use the Gson Java library to serialize a Java object to JSON object.

About GSON

As per Gson official document, Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.

  • Provide simple toJson() and fromJson() methods to convert Java objects to JSON and vice-versa
  • Allow pre-existing unmodifiable objects to be converted to and from JSON
  • Extensive support of Java Generics
  • Allow custom representations for objects
  • Support arbitrarily complex objects (with deep inheritance hierarchies and extensive use of generic types)

Let’s first add the latest Gson dependency in your maven project. I have used below dependency for this post which is the latest version:-

Gson library provides a class called “Gson“. This is the main class for using Gson. Gson is typically used by first constructing a Gson instance and then invoking toJson(Object) or fromJson(String, Class) methods on it. Gson instances are Thread-safe so you can reuse them freely across multiple threads.

You can create a Gson instance by invoking new Gson(), if the default configuration is all you need. You can also use GsonBuilder to build a Gson instance with various configuration options such as versioning support, pretty-printing, custom JsonSerializer, JsonDeserializer, and InstanceCreator.

let’s learn about serialization using Gson in this post.

Target JSON Object to be created:-

POJO Class

Let’s create a class with field name exactly (case sensitive) the same as node names in above JSON payload because with default setting while parsing Java Object to JSON object, it will look on getter setter methods of field names. In fact, access specifiers do not matter here. We will cover some interesting facts on it later.

POJO to JSON String

Gson class provides multiple overloaded methods called “toJson()”. Below is a glimpse:-

We will convert a Java Object to a JSON object as a String and also will write into a .json file.

Output

You can download/clone the above sample project from here.

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

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Writing technical posts and creating YouTube videos are my hobbies.

1 thought on “REST Assured Tutorial 34 – Serialization – Java Object To JSON Object Using Gson API

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.