This post will discuss the serialization and deserialization of Java objects using Google’s Gson library.

Nowadays, almost all RESTful web services consume and produce JSON data instead of XML. Unfortunately, Java SE doesn’t support converting JSON to Java Object (and vice-versa). But there are many third-party libraries available that are very reliable and offer high performance. One such library is Gson.

Gson is a Java library that can be used to convert Java objects into their JSON representation. We can also use it to convert a JSON string to an equivalent Java object. Gson provide simple toJson() and fromJson() methods to convert Java objects to JSON and vice-versa.

  1. Gson does not require additional modifications to classes of serialized/deserialized objects as it uses reflection.
  2. Gson cannot serialize any transient fields as a transient keyword in Java indicates that a field should not be serialized.
  3. Gson will not work on objects with recursive references.
  4. Gson requires the class to have a default no-args constructor. If the no-args constructor is not provided, we can register an InstanceCreator with Gson, allowing us to deserialize instances of classes.

In fact, Gson can deserialize a class even without a no-args constructor or registered InstanceCreator. Try removing the no-args constructor from the following code, and the code will run fine. This StackOverflow post explains it!

Download Code

Output:

Converting Person object to JSON string:
{"name":"Jon Snow","age":22,"student":{"id":"Jon_Snow_22","subjects":["Maths","Science"]}}
 
Converting JSON string to Person object:
[Jon Snow, 22, [Jon_Snow_22, [Maths, Science]]]

 
Gson is highly customizable. We can use GsonBuilder to construct a Gson instance when we need to set configuration options other than the default. There are many interesting methods provided by GsonBuilder to set the custom configuration for the Gson instance. The complete list can be seen here. The following example shows how to use the GsonBuilder to construct a Gson instance using the following methods:

  1. serializeNulls() : By default, Gson excludes all null fields during serialization. This method configures Gson to serialize null fields as well.
  2. setPrettyPrinting() : This method configures Gson to output JSON for pretty-printing.
  3. setFieldNamingStrategy(FieldNamingStrategy) : This method configures Gson to apply a specific naming policy strategy to an object’s field during serialization and deserialization.

The following program demonstrates it:

Download Code

Output:

Converting Person object to JSON string:

{
  "Name": "Jon Snow",
  "Age": 22,
  "Student": {
    "Id": null,
    "Subjects": [
    "Maths",
    "Science"
    ]
  }
}

Converting JSON string back to Person object:
[Jon Snow, 22, [null, [Maths, Science]]]

That’s all about serialization of Java objects using Google’s Gson library.

 
Also See:

Serialization of Java objects using Jackson Library

 
Useful JSON Tools: