Thursday, June 10, 2010

A Simple Json Utility Class Using Gson

In my current project we planned to go with Gson(Google's json manipulating library) to handle out json manipulation within our application. Hence i wrote this small utility class to ease the use of Gson within the project.



import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
* This class is used as a Json utility. The base functionality comes from the Gson<br>
* package from Google. It is made generic due to the fact that many clients may use<br>
* this class at any given moment.
*
* @author dinuka
* @param <T>
*/
public class Jsonutil {

/**
* Null serialize is used because else Gson will ignore all null fields.
*/
private static Gson gson = new GsonBuilder().serializeNulls().create();

/**
* Made private because all methods are static and hence do not need
* object instantiation
*/
private Jsonutil() {}
/**
* To Json Converter using Goolge's Gson Package<br>
* this method converts a simple object to a json string<br>
*
* @param obj
* @return a json string
*/
public static <T> String toJsonObj(T obj) {
return gson.toJson(obj);
}

/**
* Converts a collection of objects using Google's Gson Package
*
* @param objCol
* @return a json string array
*/
public static <T> String toJsonList(List<T> objCol) {
return gson.toJson(objCol);
}

/**
* Returns the specific object given the Json String
* @param <T>
* @param jsonString
* @param obj
* @return a specific object as defined by the user calling the method
*/
public static <T> T fromJsonToObj(String jsonString,Class<T>obj) {
return gson.fromJson(jsonString, obj);
}

/**
* Returns a list of specified object from the given json array
* @param <T>
* @param jsonString
* @param t the type defined by the user
* @return a list of specified objects as given in the json array
*/
public static <T> List<T> fromJsonToList(String jsonString,Type t){
return gson.fromJson(jsonString, t);
}

}


Typical usage of the given utility class is as follow;


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

import com.google.gson.reflect.TypeToken;


public class Test {


public static void main(String[] args) {
Person p = new Person();
p.setFirstName("xxx");
p.setLastName("yyy");

List<Person>pList = new ArrayList<Person>();
pList.add(p);

String jsonStr = JsonUtil.toJsonList(pList);

System.out.println("Person List as Json Array = "+jsonStr);

Type collectionType = new TypeToken<List<Person>>(){}.getType();
List<Person>personList = JsonUtil.fromJsonToList(jsonStr,collectionType);
System.out.println("Person List from json String array = "+personList);

String personJsonStr = JsonUtil.toJsonObj(p);
System.out.println("Json String from person = "+personJsonStr);

Person p1 = JsonUtil.fromJsonToObj(personJsonStr, Person.class);
System.out.println("Person from json String = "+p1);

}



}

class Person{
private String firstName;
private String lastName;
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Person [firstName=" + firstName + ", lastName=" + lastName + "]";
}


}



Thats it guys. If you have any queries or possible enhancements you can see, please do leave a comment.

2 comments:

andry said...

very good tutorial and can hopefully help me in building json in the application that I created for this lecture. thank you

Dinuka Arseculeratne said...

@Andry,

Appreciate you taking the time to leave a comment. And thank you for the positive feedback. Glad to be able to help you out.

Cheers

Dinuka

Post a Comment

 
;