In the world of dynamic HTML, we always need a fat free version of data exchange between the server and the client.As the web world becomes more dynamic, the use of Javascript have considerably increased. With frameworks like Jquery, DOJO etc, use of javascript have become very easy and less cumbersome. These JS frameworks can create dynamic HTML based on data from the server. Another biggest change on the web world is the use of AJAX. The more interactive the web is, AJAX will be used to a greater extent so that the user gets a seamless feeling. For all the above scenarios, what we need is a data format which is easy and efficient. Here we will discuss about JSON - the fat free data format which is used across the board in web, mobile and even client applications.
JSON stands for JavaScript Object Notation. (JSON is pronounced like the name Jason)
It is a data format. The JSON data format is ideally suited for consumption by a JavaScript program.
JSON is built on two structures:
These are universal data structures. Virtually all modern programming languages support them in one form or another. It makes sense that a data format that is interchangeable with programming languages also be based on these structures.
In JSON, they take on these forms:
Example for JSON Object
Below is the consumer representation in JSON. Look at it, its so simple and easily understandable.
“consumer”: {
“name”:”Deepak”
“age”: 30
“email” : “[email protected]”
}
}
The types represented in JSON are strings, numbers, booleans, object, arrays, and null.
Example
Here is the consumer array.
{
[“name”:”Deepak”,”age”:30,”email”:”[email protected]”],
[“name”:”Dhanu”,”age”:29,”email”:”[email protected]”],
[“name”:”Ajay”,”age”:30,”email”:”[email protected]”],
}
I loved using JSON for transport on my projects. But when i have a complex structure i felt the use of Normal JSON parsers on the server side felt little cumbersome and time taking. I really wanted a way where i feed the JSON structure to a method and get a custom well defined object as a result of it. This thought made me research on such programs and i finally landed GSON. My life became so much more easier with it.
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.
Lets say i have a consumer object with below structure in my project.
public class Consumer { private String name; private int age; private String email; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } } |
i can use the below code to convert the Consumer object to a JSON equivalent.
Consumer consumer = new Consumer(); consumer.setName(“Deepak”); consumer.setAge(30); consumer.setEmail(“[email protected]”); Gson gson = new Gson(); String json = gson.toJson(consumer); |
The above code output will look like
{“name”:”Deepak”,”age”:30,”email”:”[email protected]”}
Here i will show a simple deserialization sample. The gson.fromJson() takes two parameters:
String jsonStr = {“name”:”Deepak”,”age”:30,”email”:”[email protected]”}; Gson gson = new Gson(); Consumer consumer = gson.fromJson(jsonStr, Consumer.class); |
Lets take the same Consumer class as an example. Say that we have a list of consumers and we need to GSON to serialize and deserialize to and from JSON format.
/** Created a consumers list **/ List<Consumer> consumers = new ArrayList<Consumer>(); /**Adding 3 consumer to the list**/ consumers.add(new Consumer(“Deepak”,30,”[email protected]”)); consumers.add(new Consumer(“Dhanu”,29,”[email protected]”)); consumers.add(new Consumer(“Ajay”,30,”[email protected]”)); Gson gson = new Gson(); String jsonStr = gson.toJson(consumers); |
This will create a JSON object as below:
{
[“name”:”Deepak”,”age”:30,”email”:”[email protected]”],
[“name”:”Dhanu”,”age”:29,”email”:”[email protected]”],
[“name”:”Ajay”,”age”:30,”email”:”[email protected]”],
}
When we have typed collections we will need to create a type token and tell GSON that its dealing with this type.
String json = “{ [\“name\”:\”Deepak\”,\”age\”:30,\”email\”:\”[email protected]\”], [\“name\”:\”Dhanu\”,\”age\”:29,\”email\”:\”[email protected]\”], [\“name\”:\”Ajay\”,\”age\”:30,\”email\”:\”[email protected]\”], }”; Gson gson = new Gson() Type newType = new TypeToken<ArrayList<Consumer>>(){}.getType();
List<Consumer> models = gson.fromJson(json,newType); |
This will create a list of consumer objects with the values as in the JSON string.
Google-gson.jar http://code.google.com/p/google-gson/downloads/list