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.
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);