tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 JSON > FLEX JSON > How to Deep Serialize Complex Object

How to Deep Serialize Complex Object 

Flexjson is a lightweight java library for serializing and de-serializing java beans, maps, arrays and collections in JSON format. Get the latest binaries from https://sourceforge.net/projects/flexjson/. This requires the library flexjson-2.1.jar to be in classpath. The following example shows how to deep serialize the given Java Object which by default includes the Collections, Maps and Array fields.

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

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

public class StudentClass {

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

  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public List<MyStudent> getStudents() {
    return students;
  }

  public MyStudent getStudentLeader() {
    return studentLeader;
  }

  public void setStudentLeader(MyStudent studentLeader) {
    this.studentLeader = studentLeader;
  }
  
}
   

File Name  :  
com/bethecoder/tutorials/flexjson/common/MyStudent.java 
   
package com.bethecoder.tutorials.flexjson.common;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


public class MyStudent extends Student {

  private List<String> phones = new ArrayList<String>();
  
  public MyStudent(String firstName, String lastName, 
      int age, String hobby, Date dob) {
    super(firstName, lastName, age, hobby, dob);
  }

  public MyStudent addPhones(String ... phoneNums) {
    phones.addAll(Arrays.asList(phoneNums));
    return this;
  }
  
  public List<String> getPhones() {
    return phones;
  }
  
}
   

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

import java.util.Date;

import com.bethecoder.tutorials.flexjson.common.MyStudent;
import com.bethecoder.tutorials.flexjson.common.StudentClass;

import flexjson.JSONSerializer;

public class DeepSerializeTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    StudentClass stdClass = new StudentClass("Maths Class");
    MyStudent spl = new MyStudent("Sudhakar""Kasireddi"29"Painting"new Date());
    spl.addPhones("1111""2222");
    stdClass.setStudentLeader(spl);
    
    stdClass.getStudents().add(
        new MyStudent("Sriram""Kasireddi"2
          "Singing"new Date()).addPhones("3333""4444"));
    
    stdClass.getStudents().add(spl);
    
    stdClass.getStudents().add(
        new MyStudent("Anu""Kasireddi"28,
          "Cooking"new Date()).addPhones("5555""6666"));

    /**
     * This performs a deep serialization of the target instance. 
     * It will include all collections, maps, and arrays by default.
     */
    JSONSerializer serializer = new JSONSerializer().prettyPrint(true)
    String jsonStr = serializer.deepSerialize(stdClass);
    System.out.println(jsonStr);
  }

}
   

It gives the following output,
{
    "class": "com.bethecoder.tutorials.flexjson.common.StudentClass",
    "name": "Maths Class",
    "studentLeader": {
        "age": 29,
        "class": "com.bethecoder.tutorials.flexjson.common.MyStudent",
        "dob": 1342088027893,
        "firstName": "Sudhakar",
        "hobby": "Painting",
        "lastName": "Kasireddi",
        "phones": [
            "1111",
            "2222"
        ]
    },
    "students": [
        {
            "age": 2,
            "class": "com.bethecoder.tutorials.flexjson.common.MyStudent",
            "dob": 1342088027893,
            "firstName": "Sriram",
            "hobby": "Singing",
            "lastName": "Kasireddi",
            "phones": [
                "3333",
                "4444"
            ]
        },
        {
            "age": 29,
            "class": "com.bethecoder.tutorials.flexjson.common.MyStudent",
            "dob": 1342088027893,
            "firstName": "Sudhakar",
            "hobby": "Painting",
            "lastName": "Kasireddi",
            "phones": [
                "1111",
                "2222"
            ]
        },
        {
            "age": 28,
            "class": "com.bethecoder.tutorials.flexjson.common.MyStudent",
            "dob": 1342088027893,
            "firstName": "Anu",
            "hobby": "Cooking",
            "lastName": "Kasireddi",
            "phones": [
                "5555",
                "6666"
            ]
        }
    ]
}



 
  


  
bl  br