Serialization of Java Objects using Google’s GSON Library

In this post, we will discuss Serialization and deserialization of Java objects using Google’s GSON library.


 

Nowdays almost all RESTful web services consumes and produces JSON data instead of XML but unfortunately Java SE doesn’t provide any support to convert JSON to Java Object (and vice-versa). But there are many third party libraries available which are very reliable and offers high performance. One such library is Gson.

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 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 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 default no-args constructor. If no-args constructor is not provided, we can register an InstanceCreator with Gson which also allows to deserialize instances of classes.
     

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

Java

Download


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 custom configuration for Gson instance. The complete list can be seen here. The following example shows how to use the GsonBuilder to construct a Gson instance using below 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.
     

Java

Download


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]]]

 
Serialization of Java Objects using Jackson Library

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz