How to serialize/deserialize objects in java
In this tutorial, we are going to learn how to serialize and de-serialize java objects to/from json.
For serialization to json, there are multiple libraries provided which can be used for this purpose. Though the most widely used libraries are jackson, gson and json-simple.
json-simple’s github repo has been inactive since 2014 so that goes out of comparison right away (You don’t want to
use a dead library !!). jackson is quite popular but in terms of performance, gson scores better.
Hence in this article, we are going to focus on gson.
Let us quickly get down to action. Below are examples of java<->json using gson.
-
First of all, we need to include
gsonjar into our project. If you are using any dependency management tool likemavenorgradle, you can include thegson jarin the following way:gradle:dependencies { implementation 'com.google.code.gson:gson:2.8.5' }maven:<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> </dependency>If you are working in native style, you can download the latest
gsonjar from herePS: Whichever way you include
gson, please ensure to use latest version. - Next thing, let us quickly create a sample java class and try to convert it into json.
public class Employee { private int id; private String name; private List<Address> addresses; public Employee(final int id, final String name, final List<Address> addresses) { this.id = id; this.name = name; this.addresses = addresses; } @Override public String toString() { return "[id = " + id + ", name = " + name + ", address = [" + addresses + "]]"; } } //And the Address class looks like this: public class Address { private String city; public Address(final String city) { this.city = city; } @Override public String toString() { return "city = " + city; }Few things to note:
- fields are marked
private, you can also mark thempublicbut it is just poor design. - Fields are initialized in constructor. If you don’t like that, you can simply get rid of the constructor and just have
setterfor each field. - The class looks pure
POJOwith zero influence ofgson. - The
toStringmethod is added purely to print the object nicely on the console.
- fields are marked
- Next thing, we would initialize the Gson class, like:
Gson gson = new Gson();OR
Gson gson = new GsonBuilder().create();Though, the latter one gives you more control over
gsonobject. You can toggle features like, null serialization and many more using the latter one. gsonprovides an intuitive methodtoJsonto convert a java object TOjson. Let us create object ofEmployeeclass and supply that object in thetoJsonmethod, like://prepare the employee object Address temporaryAddress = new Address("New York"); Address permanentAddress = new Address("New Jersey"); List<Address> addresses = new ArrayList<Address>(2); addresses.add(temporaryAddress); addresses.add(permanentAddress); Employee employee = new Employee(1, "John Doe", addresses); // json-ify the object Gson gson = new Gson(); String json = gson.toJson(employee); System.out.println("json string is : " + json);This gives the output as:
json string is : {"id":1,"name":"John Doe","addresses":[{"city":"New York"},{"city":"New Jersey"}]}As we can see, the object has been successfully converted to json.
- Now let us convert the json back to java object. To do that, again,
gsonprovides a naturally obvious named methodfromJsonto get a java object FROMjson. The code for it looks like:Employee convertedFromJson = gson.fromJson(json, Employee.class); System.out.println("Employee object created from json : " + convertedFromJson);This gives us output as:
Employee object created from json : [id = 1, name = John Doe, address = [[city = New York, city = New Jersey]]]The reason we see the employee object being nicely printed is due to
toStringimplementation provided. We can see that the object has been successfully converted back to java.
This concludes our tutorial explaining conversion of java object to/from json using gson. For downloading the complete code
of the above example, please go to this link.
If you want to explore more advanced features of gson, please visit this link
If you have any questions or feedback, please put your thoughts in the comments section below.
Happy learning !!