GSON and JACKSON For Data Conversion

Posted By : Rozi Ali | 27-Aug-2020

This blog post describes different ways to convert Java objects to JSON objects with a focus on GSON and JACKSON for data conversion. To convert JSON objects into Java objects or vice versa, Java provides 2 different libraries.

 

JSON (JavaScript Object Notation) is a standard format to interchange data between server and web application. To convert JSON objects into Java objects or vice versa, Java provides 2 different libraries:

  1. GSON (Open source library)
  2. JACKSON

In this blog, you will see how to use both of them.

For example, I am using StudentInfo class.

public class StudentInfo {
private long studentId;
private String name;
private String course;
private String standard;

public StudentRecord(long studentId, String name, String course,
String standard) {
this.studentId = studentId;
this.name = name;
this.course = course;
}
//getters and setters
}

 

JSON to Java Objects using GSON

Gson fromJson() method is useful to parse Json into Java objects.

Gson gsonObject = new Gson();

String jsonObject = "{\"studentId\":101, \"name\": \"John\", \"course\": \"BA\"}";

StudentInfo student= gsonObject.fromJson(jsonObject, StudentInfo.class);

 

JSON from Java Objects using GSON

To convert java object to json, we use toJson() method of Gson

Gson gsonObject = new Gson();

StudentInfo student = new StudentInfo();
student.studentId = 101;
student.name = "John";
student.course = "BA";

String jsonObject = gsonObject.toJson(student);

 

JSON to Java Objects using JACKSON

For Jackson, we need to create object of ObjctMapper class that provides the functionality for reading or writing JSON. To convert a JSON into Java Object, readValue() function is useful and for vice-versa, use writeValueAsString() function.

ObjectMapper objectMapper = new ObjectMapper();

String jsonObject =
    "{\"studentId\":101, \"name\": \"John\", \"course\": \"BA\"}";

StudentInfo student = objectMapper.readValue(jsonObject, StudentInfo.class);

 

JSON from Java Objects using JACKSON

ObjectMapper objectMapper = new ObjectMapper();

StudentInfo student = new StudentInfo();
      student.setStudentId(001);
      student.setName("John");
      student.setCourse("BA");

String jsonObject = objectMapper.writeValueAsString(student);

 

Comparison

Both ways of converting data are good and both the libraries support generic types but there are some differences:

  1. When data is small or of medium sized, GSON is more efficient. For large amount of data, JACKSON is preferrable.
  2. If we talk about ease, JACKSON is rich in annotations which makes it easy to acheive tasks. For simple serialization or deserialization, Gson is easier to use.
  3. Unlike Gson, Jackson by default fill the properties for null (which you can change). Gson simply ignores those properties.

 

We, at Oodles Technologies, provide end-to-end SaaS application development services to address varied business requirements. Our development team specializes in using agile methodologies and advanced JavaScript frameworks to build high-quality web and mobile applications for multiple platforms. Our custom-tailored SaaS application development services enable businesses to strengthen their brand's online presences and engage their customers or end-users in the most effective way. For further assistance, contact us at info@oodlestechnologies.com. 

About Author

Author Image
Rozi Ali

She is self-motivated and dedicated person in Development team. She is working on Java Technology.

Request for Proposal

Name is required

Comment is required

Sending message..