tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 JSON > GOOGLE GSON > Custom Instance Creator

Custom Instance Creator 

Google-gson is a java library from Google for encoding and decoding JSON text. Get the latest binaries from http://code.google.com/p/google-gson/. The following example shows registering a custom instance creator by providing an implementation of InstanceCreator interface.

File Name  :  
com/bethecoder/tutorials/google_gson/common/Student3.java 
   
package com.bethecoder.tutorials.google_gson.common;

import java.util.Date;

public class Student3 {
  private String firstName;
  private String lastName;
  private int age;
  private String hobby;
  private Date dob;
  private Password password;

  public Student3(String firstName, String lastName, int age, String hobby,
      Date dob) {
    super();
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
    this.hobby = hobby;
    this.dob = dob;
  }
  
  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;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public String getHobby() {
    return hobby;
  }

  public void setHobby(String hobby) {
    this.hobby = hobby;
  }
  public Date getDob() {
    return dob;
  }
  public void setDob(Date dob) {
    this.dob = dob;
  }

  public Password getPassword() {
    return password;
  }

  public void setPassword(Password password) {
    this.password = password;
  }
  
  public String toString() {
    return "Student[ " +
      "firstName = " + firstName +
      ", lastName = " + lastName +
      ", age = " + age +
      ", hobby = " + hobby +
      ", dob = " + dob +
      " ]";
  }
}
   

File Name  :  
com/bethecoder/tutorials/google_gson/tests/CustomInstanceCreatorTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.google_gson.tests;

import java.lang.reflect.Type;

import com.bethecoder.tutorials.google_gson.common.Password;
import com.bethecoder.tutorials.google_gson.common.Student3;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator;

public class CustomInstanceCreatorTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    Gson gson = new GsonBuilder()
      .registerTypeAdapter(Password.class,
            new PasswordInstanceCreator()) //Register custom instance creator
      .setPrettyPrinting().create();

    String jsonStr = 
      "{" +
          "\"firstName\": \"Sriram\"," +
          "\"lastName\": \"Kasireddi\"," +
          "\"age\": 2," +
          "\"hobby\": \"Singing\"," +
          "\"dob\": \"May 6, 2010 12:00:00 AM\"," +
          "\"password\": {}" +
      "}";
    
    Student3 stud = gson.fromJson(jsonStr, Student3.class);
    System.out.println("password : " + stud.getPassword().getPassword());          
  }

}

class PasswordInstanceCreator implements InstanceCreator<Password> {
  @Override
  public Password createInstance(Type type) {
    return new Password("default_password");
  }
}
   

It gives the following output,
password : default_password



 
  


  
bl  br