The increased use of JSON for data exchange has resulted in the emergence of several libraries to handle it. GSON, google's API, allows the production of JSON strings from Objects and Objects from JSON strings quite easily. However, additional steps must be performed when the Object implements Collections of items that are not primitive.
In this post we will have a simple example with no Collections. We'll convert Object to JSON and then JSON to Object.
GSON can be downloaded from:
It's a .jar file wich you can add to your project. GSON uses the Java Refelction API in order to generate the JSON Strings in just two lines of code! two lines!
Note: Due to the use of the Java Reflection API, GSON can't be used in Java ME, but it is a very good choice if you want to produce JSON Strings of an Object at server side and then send it to a mobile client.
The Java Classes we will use during the example are:
import java.util.Date;
public class Book
{
/** Name of the book*/
private String name;
/** number of pages*/
private Integer numPages;
/** How much the book costs*/
private Double cost;
/**authors's names*/
private String[] authors;
/** Publication Date*/
private Date published;
//Constructor, Getters and Setters...
}
import com.google.gson.Gson;
import java.util.Date;
public class Control
{
public static void main(String[] args)
{
//create a book
Book myBook = new Book();
myBook.setName("Java and JSON");
myBook.setNumPages(2);
myBook.setCost(1.5);
myBook.setAuthors(new String[]{"Alexis López", "Duke"});
myBook.setPublished(new Date());
//Convert myBook to JSON using GSON
Gson gson = new Gson();
String json = gson.toJson(myBook);
//print the result
System.out.println("*** MyBook as JSON ***");
System.out.println(json);
//Map JSON text to a new book
Book newBook = gson.fromJson(json, Book.class);
//print some attributes of the new book
System.out.println("*** NewBook from JSON ***");
System.out.println("Name = " + newBook.getName());
System.out.println("Authors = " + Arrays.toString(newBook.getAuthors()));
System.out.println("Number of pages = " + newBook.getNumPages());
System.out.println("Publication date = " + newBook.getPublished());
//...
}
}
Once the Control class has been run, you will notice in the console the JSON String of the Object myBook:
Also, you will notice the information of the new book (newBook) mapped from the MyBook JSON String:
As I mentioned at the beginning of the post, when the object in question contains among its attributes a Collection, you should consider this:
- Producing the JSON string is the same as we saw in the example above.
- Mapping Objects from the JSON String requires additional steps, especially because it is not possible to recognize the type of Generics used for collection from the plain text (JSON).
In a future post, we will extend the example to clarify this.
See you soon!
{"name":"Java and JSON","numPages":2,"cost":1.5,"authors":["Alexis López","Duke"],"published":"3/11/2010 09:42:06 AM"}
Also, you will notice the information of the new book (newBook) mapped from the MyBook JSON String:
Name = Java and JSON
Authors = [Alexis López, Duke]
Number of pages = 2
Publication date = Wed Nov 03 09:42:06 GMT-05:00 2010
As I mentioned at the beginning of the post, when the object in question contains among its attributes a Collection, you should consider this:
- Producing the JSON string is the same as we saw in the example above.
- Mapping Objects from the JSON String requires additional steps, especially because it is not possible to recognize the type of Generics used for collection from the plain text (JSON).
In a future post, we will extend the example to clarify this.
See you soon!
References:
google-gson. August 19 de 2010. Google Code [online]. Available from Internet:http://code.google.com/p/google-gson/ [accessed on October the 26th 2010].
Excellent Post! :-)
ReplyDeleteBut can someone please give me the link to the "future post"?
Hi, try this:
Deletehttp://www.java-n-me.com/2010/11/gson-json-in-just-two-lines-of-code_07.html
And this is a more elaborate example:
http://www.java-n-me.com/2010/11/servlet-gson-vs-jsonme-javame.html
Regards
Thanks Sir! That post is also awesome!
Delete