tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 JSON > FLEX JSON > How to explicitly include properties during Serialization

How to explicitly include properties during Serialization 

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. Flexjson by default serializes the immediate fields but not the Collection Objects. The following example shows how to explicitly include Collections while serializing a Complex Object.

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/IncludePropertiesTest.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
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 IncludePropertiesTest {

  /**
   @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"));
    

    //By default Flexjson serializes the immediate fields of that object. 
    //It's just a shallow representation of the object. 
    //All collections are not serialized by default.
    JSONSerializer serializer = new JSONSerializer().prettyPrint(true)
    String jsonStr = serializer.serialize(stdClass);
    System.out.println(jsonStr);
    
    jsonStr = serializer.include("students").serialize(stdClass);
    System.out.println(jsonStr);
    
    jsonStr = serializer.include("students.phones").serialize(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": 1342078990079,
        "firstName": "Sudhakar",
        "hobby": "Painting",
        "lastName": "Kasireddi"
    }
}



{
    "class": "com.bethecoder.tutorials.flexjson.common.StudentClass",
    "name": "Maths Class",
    "studentLeader": {
        "age": 29,
        "class": "com.bethecoder.tutorials.flexjson.common.MyStudent",
        "dob": 1342078990079,
        "firstName": "Sudhakar",
        "hobby": "Painting",
        "lastName": "Kasireddi"
    },
    "students": [
        {
            "age": 2,
            "class": "com.bethecoder.tutorials.flexjson.common.MyStudent",
            "dob": 1342078990079,
            "firstName": "Sriram",
            "hobby": "Singing",
            "lastName": "Kasireddi"
        },
        {
            "age": 29,
            "class": "com.bethecoder.tutorials.flexjson.common.MyStudent",
            "dob": 1342078990079,
            "firstName": "Sudhakar",
            "hobby": "Painting",
            "lastName": "Kasireddi"
        },
        {
            "age": 28,
            "class": "com.bethecoder.tutorials.flexjson.common.MyStudent",
            "dob": 1342078990079,
            "firstName": "Anu",
            "hobby": "Cooking",
            "lastName": "Kasireddi"
        }
    ]
}



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


                    Print

   



Books Recommendation

 
 
bl  br