Analysis Of Jackson And Gson

In this blog we will read about the Gson and Jackson Libraries which are used to convert JSON data to Java Object and vice-versa.

Gson is the open source  Java Library provided by Google. If we have to use Gson then we have to import the dependency of it whereas there is no need to import the dependency of Jackson when we are working with Spring Boot.

-> Serialization And Deserialization :

Steps of processing the data for the conversion ->

1. Add the Dependency to XML file : 

Firstly, add the Gson dependency to the pom.xml file to create an object of Gson and use its features.

If you are not using Spring Boot then for using Jackson you have to add Jackson Dependency.

 

2. Tree Model: 

It prepares an in-memory tree representation of the JSON document. It builds a tree of JsonObject nodes.

 

3. Data Binding :

It converts JSON to and from POJO (Plain Old Java Object) using a property accessor.

  • Serialization :

(i) Conversion of data from the Java Object to the JSON is known as serialization.

(ii) By performing serialization we pass the object of the model as a parameter of the Gson or Jackson method which converts to JSON String.

(a) By using Gson :

Firstly, we will create a GSON  Object and then we directly use a method toJson() which converts the data from Java Object to JSON String.

 

(b) By using Jackson :

We will create an object of mapper class and then use the method for converting to JSON format which should be in try-catch block or should throw an exception.

 

Output :

Note : The output for Gson and Jackson are same.

 

  • Deserialization :

(i) Conversion of data from the JSON to the JAVA Object is known as deserialization.

(ii) By performing deserialization we have to pass the Json element and class of tree model as parameters to perform conversion from Json string to Java Object.

(a) By using Gson :

After creating the object of the Gson we will use the fromJson() method to convert the data into Java Object.

 

(b) By using Jackson :

After creating the object of the Jackson we will use thereadValue() method to convert the data into Java Object which is surrounded by the try-catch block.

 

Output :

Note : The output for Gson and Jackson are same.

 

-> Limitations :

1. If we are using Jackson then we have to use a try-catch block or throw an Exception.

2. Lines of code are more in Jackson.

-> Note :

Other features provided by Gson and Jackson are:

1. Nested Objects :

  • Firstly, add the respective dependencies.
  • Create the Tree Model :

  • Data Binding for nested objects is the same as we did for the simple objects. But now we have to pass the object of the nested class.

  • Similarly we can do for Jackson also.

 

Output :

Note : The output for Gson and Jackson are same.

 

2. Read JSON File :

  • Previously we were passing the Json String as an input but now we are passing the Json File.
  • Firstly create a .json file in the resource folder.

 

  • Then we find the path of the Json file by creating the File Object.
  • After that when we call the method fromJson() then in place of passing the json String we pass the object of the FileReader with the file path as the parameter. And the rest part is the same.
  • Similarly we can do Jackson also.

 

3. Write JSON File :

  • In this we are storing the output of Json String in a separate file.
  • We will use the FileWriter to write the output in the file.
  • We will pass the object of the tree model in the method.
  • Then in the object of the FileWriter we pass the path where we want to add our file.
  • After writing in the file, we have to close the writer object.
  • Similarly we can do for Jackson also.

 

4. JsonIgnore , JsonIgnoreProperties And JsonIgnoreType :

  • This works only with Jackson not with Gson.

 

-> JsonIgnoreProperties :

  • It is used to ignore the specified logical properties.
  • It is annotated at the class level.
  • We write the @JsonIgnoreProperties in the Tree Model.

  • Then our main method looks like :

  • Output :

 

-> JsonIgnore :

  • It is used to ignore the specified logical properties.
  • It can be used with Getter, Setter, Field.
  • The only difference is in the Tree Model and the main function remains same as of JsonIgnoreProperties.

  • Output :

 

-> JsonIgnoreType :

  • It is annotated at the class level.
  • It is used to ignore the complete class.
  • In the below example we are writing @JsonIgnoreType to the nested Information class due to which the whole Information class gets ignored.
  • No change in main function.

  • Output :

 

Comments