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.
- GSON (Open source library)
In this blog, you will see how to use both of them.
For example, I am using StudentInfo class.
JSON to Java Objects using GSON
Gson fromJson() method is useful to parse Json into Java objects.
JSON from Java Objects using GSON
To convert java object to json, we use toJson() method of Gson
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.
JSON from Java Objects using JACKSON
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.