GSON – Google JSON library for Java

In this tutorial lets see how to convert a Java object to/from JSON using GSON in Eclipse.

Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson is an open-source project hosted at http://code.google.com/p/google-gson.

What Jar files are needed ?

We need only one jar file; gson.jar.

For this tutorial i am downloading

1. gson-2.5.jar

Follow the download url from the page:http://code.google.com/p/google-gson. It will take us to the jar file in maven central.
Right click on the jar file and click “Save Target As..” to save the jar to your local.
An example image is provided for downloading gson-2.5.jar

gson

In GSON, the main methods are “new GSON().toJson” and “new GSON().fromJson”. As the names imply, toJson will create a Json String from a Java Object, and fromJson method will create a Java Object from a Json String.

How to convert a java object to JSON ?

To start with, create a java project in eclipse and add the above mentioned gson-2.5.jar to the project classpath.

Then lets create a simple java object (UserDO) with three properties say name, id and date for birth and populate them with values as shown below. We are using the same objects for our Jackson – JSON tutorial too.

Create 2 userDO objects and add them to an ArrayList – userDOList.

//Creating UserDO1.
UserDO userDO1 = new UserDO();
userDO1.setId(111);
userDO1.setName("Dinosaur");
userDO1.setDob("200 million BCE");

//Creating UserDO2.
UserDO userDO2 = new UserDO();
userDO2.setId(222);
userDO2.setName("Tyrannosaurus");
userDO2.setDob("68 million BCE");

//Setting both UserDO1 and UserDO2 into an ArrayList.
ArrayList<UserDO> userDOList = new ArrayList();
userDOList.add(userDO1);
userDOList.add(userDO2);

Now lets convert the userDO1 and the ArrayList object to a JSON String. First we need to create a GSON object. Then call toJson method by passing the object (example: userDO1) that needs to be converted to JSON.

//Creating gson object.
Gson gson = new Gson();
//Converting userDO to JSON String and printing.
String userDO1_Json = gson.toJson(userDO1);
System.out.println("JSON String of UserDO1 :\n" + userDO1_Json);

//Converting userDO arrayList to JSON String.
String userDOArrayList_Json = gson.toJson(userDOList);
System.out.println("JSON String of ArrayList :\n" + userDOArrayList_Json);

The toJson method from Gson class at line 4 & 8 does the trick. The output is provided below.

JSON String of UserDO1 :
{"name":"Dinosaur","id":111,"dob":"200 million BCE"}
JSON String of ArrayList :
[{"name":"Dinosaur","id":111,"dob":"200 million BCE"},{"name":"Tyrannosaurus","id":222,"dob":"68 million BCE"}]

How to convert a JSON into a java object?

First we need to create a GSON object and call the fromJson method on it.
We need to pass the JSON string and specify the type of java object which needs to be created from JSON;

For example UserDO.class needs to be passed into fromJson for the first JSON: userDO1_Json

userDO1_Json = {"name":"Dinosaur","id":111,"dob":"200 million BCE"}
Gson gson = new Gson();
//Converting UserDO1 JSON back to UserDO
UserDO UserDO1_FromJSON = gson.fromJson(userDO1_Json, UserDO.class);
System.out.println("\n\n"+ UserDO1_FromJSON);

Output:

Printing UserDO: 
name = Dinosaur
id = 111
dob = 200 million BCE

A new type object needs to be passed into fromJson method for the Second JSON userDOArrayList_Json to convert it into an ArrayList.

userDOArrayList_Json=[{"name":"Dinosaur","id":111,"dob":"200 million BCE"},{"name":"Tyrannosaurus","id":222,"dob":"68 million BCE"}]
Gson gson = new Gson();
//Converting userDOArrayList JSON back to Arraylist of UserDOs.
Type type = new TypeToken<ArrayList<UserDO>>(){}.getType();
ArrayList<UserDO> userDOArrayList_fromJSON = gson.fromJson(userDOArrayList_Json, type);
System.out.println("\nPrint UserDOArrayList_FromJSON :\n" + userDOArrayList_fromJSON);

Output:

Print UserDOArrayList_FromJSON :
[Printing UserDO: 
name = Dinosaur
id = 111
dob = 200 million BCE, Printing UserDO: 
name = Tyrannosaurus
id = 222
dob = 68 million BCE]

The full code used in this example is provided below.
1. UserDO.java

package com.jsession4d;

import java.util.ArrayList;

public class UserDO {

private String name;
private long id;
private String dob;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}

public String toString(){
return "Printing UserDO: \nname = " + name + "\n" + "id = " + id + "\n" + "dob = " + dob;
}

}

2. RunGson.java

package com.jsession4d;

import java.lang.reflect.Type;
import java.util.ArrayList;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jsession4d.UserDO;

public class RunGson {

public static void main(String[] args) {
try{			
//Creating UserDO1.
UserDO userDO1 = new UserDO();
userDO1.setId(111);
userDO1.setName("Dinosaur");
userDO1.setDob("200 million BCE");

//Creating UserDO2.
UserDO userDO2 = new UserDO();
userDO2.setId(222);
userDO2.setName("Tyrannosaurus");
userDO2.setDob("68 million BCE");

//Setting both UserDO1 and UserDO2 into an ArrayList.
ArrayList<UserDO> userDOList = new ArrayList();
userDOList.add(userDO1);
userDOList.add(userDO2);

//Creating gson object.
Gson gson = new Gson();
//Converting userDO to JSON String and printing.
String userDO1_Json = gson.toJson(userDO1);
System.out.println("JSON String of UserDO1 :\n" + userDO1_Json);

//Converting userDO arrayList to JSON String.
String userDOArrayList_Json = gson.toJson(userDOList);
System.out.println("JSON String of ArrayList :\n" + userDOArrayList_Json);

//Converting UserDO1 JSON back to UserDO
UserDO UserDO1_FromJSON = gson.fromJson(userDO1_Json, UserDO.class);
System.out.println("\n\n"+ UserDO1_FromJSON);

//Converting userDOArrayList JSON back to Arraylist of UserDOs.
Type type = new TypeToken<ArrayList<UserDO>>(){}.getType();
ArrayList<UserDO> userDOArrayList_fromJSON = gson.fromJson(userDOArrayList_Json, type);
System.out.println("\nPrint UserDOArrayList_FromJSON :\n" + userDOArrayList_fromJSON);

}catch(Exception e){
e.printStackTrace();
}

}

}

3. Output

JSON String of UserDO1 :
{"name":"Dinosaur","id":111,"dob":"200 million BCE"}
JSON String of ArrayList :
[{"name":"Dinosaur","id":111,"dob":"200 million BCE"},{"name":"Tyrannosaurus","id":222,"dob":"68 million BCE"}]


Printing UserDO: 
name = Dinosaur
id = 111
dob = 200 million BCE

Print UserDOArrayList_FromJSON :
[Printing UserDO: 
name = Dinosaur
id = 111
dob = 200 million BCE, Printing UserDO: 
name = Tyrannosaurus
id = 222
dob = 68 million BCE]

The same code, works perfectly well for complex java objects too.
To test it, lets make our UserDO java object little more complex by adding a nested object or an object inside the UserDO object and an ArrayList.
Navigate to next page to see it in action.

%d bloggers like this: