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:
- GSON (Open source library)
- 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:
- When data is small or of medium sized, GSON is more efficient. For large amount of data, JACKSON is preferrable.
- 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.
- 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 [email protected].
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Rozi Ali
Rozi Ali is an accomplished software developer with extensive experience in the field of JAVA. She possesses a solid grasp of programming languages such as Java/Spring-boot, Python, and Typescript/Nodejs/GraphQL. Rozi has a strong background in Object-oriented programming (OOP) and is skilled in working with both relational databases like MySql, PostgreSQL and non-relational databases like MongoDb. She is proficient in REST APIs, Microservices, and code deployment, along with the development tools such as Jira, Git, and Bash. Additionally, Rozi has experience working with Cloud providers such as AWS and Azure. She has contributed significantly to a number of projects, including Konfer, VNS, Influsoft, VN Platform, QuickDialog, and Oodles-Dashboard.