tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 JSON > GOOGLE GSON > Complex Bean DeSerialization

Complex Bean DeSerialization 

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 converting a Complex bean to JSON string and vice versa.

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

import java.util.Date;

public class Student {
  private String firstName;
  private String lastName;
  private int age;
  private String hobby;
  private Date dob;

  public Student(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 String toString() {
    return "Student[ " +
      "firstName = " + firstName +
      ", lastName = " + lastName +
      ", age = " + age +
      ", hobby = " + hobby +
      ", dob = " + dob +
      " ]";
  }
}
   

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

import java.util.ArrayList;
import java.util.List;

public class StudentClass {

  private String name;
  private List<String> subjects = new ArrayList<String>();
  private List<Student> students = new ArrayList<Student>();
  
  public StudentClass(String name) {
    super();
    this.name = name;
  }

  public String getName() {
    return name;
  }
  
  public void setName(String name) {
    this.name = name;
  }
  
  public List<String> getSubjects() {
    return subjects;
  }
  
  public void setSubjects(List<String> subjects) {
    this.subjects = subjects;
  }
  
  public List<Student> getStudents() {
    return students;
  }
  
  public void setStudents(List<Student> students) {
    this.students = students;
  }
  
}
   

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

import java.util.Arrays;
import java.util.Date;

import com.bethecoder.tutorials.google_gson.common.Student;
import com.bethecoder.tutorials.google_gson.common.StudentClass;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class ComplexBeanDeSerializationTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    Gson gson = new GsonBuilder().setPrettyPrinting().create();

    StudentClass stdCls = new StudentClass("MPC");
    stdCls.getSubjects().addAll(Arrays.asList("Maths""Physics""Chemistry"));
    stdCls.getStudents().add(
        new Student("Sriram""Kasireddi"2"Singing"new Date(11046)));  
    stdCls.getStudents().add(
        new Student("Sudhakar""Kasireddi"29"Singing"new Date(8286)));  
    
    String jsonStr = gson.toJson(stdCls)//Serialize
    System.out.println(jsonStr);
    
    stdCls = gson.fromJson(jsonStr, StudentClass.class)//Deserialize
    System.out.println("Name : " + stdCls.getName());
    System.out.println("Subjects : " + stdCls.getSubjects());
    System.out.println("Students : " + stdCls.getStudents());
  }

}
   

It gives the following output,
{
  "name": "MPC",
  "subjects": [
    "Maths",
    "Physics",
    "Chemistry"
  ],
  "students": [
    {
      "firstName": "Sriram",
      "lastName": "Kasireddi",
      "age": 2,
      "hobby": "Singing",
      "dob": "May 6, 2010 12:00:00 AM"
    },
    {
      "firstName": "Sudhakar",
      "lastName": "Kasireddi",
      "age": 29,
      "hobby": "Singing",
      "dob": "Sep 6, 1982 12:00:00 AM"
    }
  ]
}


Name : MPC
Subjects : [Maths, Physics, Chemistry]
Students : [Student[ firstName = Sriram, lastName = Kasireddi, age = 2, 
	hobby = Singing, dob = Thu May 06 00:00:00 IST 2010 ], 
	Student[ firstName = Sudhakar, lastName = Kasireddi, age = 29, 
	hobby = Singing, dob = Mon Sep 06 00:00:00 IST 1982 ]]



 
  


  
bl  br