Sunday, April 15, 2012

Json Tutorial: Java to Json with Google Gson

Overview:
This article looks how you can easily transform Java objects into Json representation and vice versa using Google Gson library.

Project Structure:



Step 1: Maven Dependencies (pom.xml)
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  <modelVersion>4.0.0</modelVersion>  
  <groupId>org.fazlan</groupId>  
  <artifactId>org.fazlan.json.gson</artifactId>  
  <packaging>jar</packaging>  
  <version>1.0.0-SNAPSHOT</version>  
  <name>org.fazlan.json.gson</name>  
  <url>http://maven.apache.org</url>  
  <dependencies>  
   <dependency>  
    <groupId>junit</groupId>  
    <artifactId>junit</artifactId>  
    <version>3.8.1</version>  
    <scope>test</scope>  
   </dependency>  
   <dependency>  
    <groupId>com.google.code.gson</groupId>  
    <artifactId>gson</artifactId>  
    <version>2.1</version>  
    <scope>compile</scope>  
   </dependency>  
    <dependency>  
      <groupId>org.apache.ws.commons.schema</groupId>  
      <artifactId>XmlSchema</artifactId>  
      <version>1.4.7-wso2v1</version>  
    </dependency>  
  </dependencies>  
 </project>  

Step 2: The Java Class we wish to transform into Json and vice versa
 package org.fazlan.json;  
 public class Person {  
   private Long id;  
   private String firstName;  
   private String lastName;  
   public Person(Long id, String firstName, String lastName) {  
     this.id = id;  
     this.firstName = firstName;  
     this.lastName = lastName;  
   }  
   public Long getId() {  
     return id;  
   }  
   public void setId(Long id) {  
     this.id = id;  
   }  
   public String getFirstName() {  
     return firstName;  
   }  
   public void setFirstName(String firstName) {  
     this.firstName = firstName;  
   }  
   public String getLastName() {  
     return lastName;  
   }  
   public void setLastName(String lastName) {  
     this.lastName = lastName;  
   }  
   @Override  
   public String toString() {  
     return "Person{" +  
         "id=" + id +  
         ", firstName='" + firstName + '\'' +  
         ", lastName='" + lastName + '\'' +  
         '}';  
   }  
 }  

Step 3: The Util class that helps to do the transformation.
 package org.fazlan.json;  
 import com.google.gson.Gson;  
 public class Json {  
   private final Gson gson;  
   private static Json JSON;  
   private Json() {  
     gson = new Gson();  
   }  
   public static Json getInstance() {  
     if(JSON == null) {  
       JSON = new Json();  
     }  
     return JSON;  
   }  
   public String toJson(Object t) {  
      return gson.toJson(t);  
   }  
   public <T> T fromJson(String jsonString, Class<T> clazz) {  
     return gson.fromJson(jsonString, clazz);  
   }  
 }  

What's important to note is the following two methods in com.google.gson.Gson
 . . .  
   gson.toJson(t);  
 . . .  
   gson.fromJson(jsonString, clazz);  

Step 3: Writing a simple client to test application
 package org.fazlan.json;  
 public class Application {  
   public static void main(String... args) {  
     Json json = Json.getInstance();  
     System.out.println("Java to Json");  
     Person p1 = new Person(1L, "Json", "Json");  
     String jsonString = json.toJson(p1);  
     System.out.println(p1 + "\t-->\t" + jsonString);  
     System.out.println("-----------");  
     System.out.println("Json to Java");  
     String person2JsonString = "{\"id\":2,\"firstName\":\"Gson\",\"lastName\":\"Google\"}";  
     Person p2 = json.fromJson(person2JsonString, Person.class);  
     System.out.println(person2JsonString + "\t-->\t" + p2);  
   }  
 }  

Sample output from this application,
 Java to Json  
 Person{id=1, firstName='Json', lastName='Json'}     -->     {"id":1,"firstName":"Json","lastName":"Json"}  
 -----------  
 Json to Java  
 {"id":2,"firstName":"Gson","lastName":"Google"}     -->     Person{id=2, firstName='Gson', lastName='Google'}  

No comments:

Post a Comment