Flexjson -Opensource Java api for JSON

In this tutorial lets see how to convert a Java object to/from JSON using Flexjson in Eclipse.

Flexjson is a opensource project hosted in sourceforge.net by charlie hubbard. It is a library for serializing and deserializing Java objects into and from JSON. The main speciality of Flexjson is its control over what gets converted into JSON.
In this tutorial we are just going to see how Flexjson suites for our example scenarios provided below. Flexjson has many additional features. All of them are not covered in this tutorial.
Scenario 1. Converting a simple Java object (with no external references) to and from JSON.
Scenario 2. Converting a little complex java object (which contains another child object and an ArrayList) to and from JSON.

What Jar files are needed ?

We need only one jar file; flexjson.jar.

For this tutorial i am downloading

1. flexjson-3.3.jar

The release versions are available in the url: http://repo2.maven.org/maven2/net/sf/flexjson/flexjson/

In flexjson, the main methods are “new JSONSerializer().serialize” and “new JSONDeserializer.deserialize”. As the names imply, serialize will create a Json String from a Java Object, and deserialize method will create a Java Object from a Json String.

How to convert a java object to JSON ?

To start with, create a java project in eclipse and add the above mentioned flexjson-3.3.jar to the project classpath.

Then lets create a simple java object (UserDO) with three properties say name, id and date for birth and populate them with values as shown below. We are using the same objects for our Jackson – JSON tutorial too.

Create 2 userDO objects and add them to an ArrayList – userDOList.

//Creating UserDO1.
UserDO userDO1 = new UserDO();
userDO1.setId(111);
userDO1.setName("Dinosaur");
userDO1.setDob("200 million BCE");

//Creating UserDO2.
UserDO userDO2 = new UserDO();
userDO2.setId(222);
userDO2.setName("Tyrannosaurus");
userDO2.setDob("68 million BCE");

//Setting both UserDO1 and UserDO2 into an ArrayList.
ArrayList<UserDO> userDOList = new ArrayList();
userDOList.add(userDO1);
userDOList.add(userDO2);

Now lets convert the userDO1 object to a JSON String. Here we are passing the userDO1 object into the new JSONSerializer().serialize method.

//Converting userDO to JSON String and printing.
String userDO1_Json_with_classname = new JSONSerializer().serialize(userDO1);
System.out.println("JSON String of UserDO1 :\n" + userDO1_Json_with_classname);

The output is provided below. As you can see from the output, the generated JSON also contains the class name from which it got serialized.

JSON String of UserDO1 :
{"class":"com.jsession4d.UserDO","dob":"200 million BCE","id":111,"name":"Dinosaur"}

Our intended output is not to have the class name inside the generated JSON. We can easily get that by adding a exclude method as provided below.

//Converting userDO to JSON String and printing.
String userDO1_Json = new JSONSerializer().exclude("*.class").serialize(userDO1);
System.out.println("JSON String of UserDO1 :\n" + userDO1_Json);

Now we got our intended output.

JSON String of UserDO1 :
{"dob":"200 million BCE","id":111,"name":"Dinosaur"}

The same way we can generate the JSON for the ArrayList of UserDOs.

//Converting userDO arrayList to JSON String.
String userDOArrayList_Json = new JSONSerializer().exclude("*.class").serialize(userDOList);
System.out.println("JSON String of ArrayList :\n" + userDOArrayList_Json);

Output:

JSON String of ArrayList :
[{"dob":"200 million BCE","id":111,"name":"Dinosaur"},{"dob":"68 million BCE","id":222,"name":"Tyrannosaurus"}]

How to convert a JSON into a java object?

We need to create JSONDeserializer object based on the return type. If we want the JSON to be deserialized as a UserDO object, we need to create new JSONDeserializer object and then call deserialize method on it. We have to pass both the JSON string and the class to which the JSON string needs to be converted.

To convert userDO1_Json into UserDO object.

userDO1_Json = {"name":"Dinosaur","id":111,"dob":"200 million BCE"}
//Converting UserDO1 JSON back to UserDO
JSONDeserializer<UserDO> jsonDeserializer = new JSONDeserializer<UserDO>();
UserDO UserDO1_FromJSON = jsonDeserializer.deserialize(userDO1_Json,UserDO.class);
System.out.println("\n\n"+ UserDO1_FromJSON);

Output:

Printing UserDO: 
name = Dinosaur
id = 111
dob = 200 million BCE

For converting the Json string Array into an ArrayList we have to create the JSONDeserializer object. Here we have to use the “use” method to mention the values inside the ArrayList are UserDOs as provided in the code below.

JSON String of ArrayList :
[{"dob":"200 million BCE","id":111,"name":"Dinosaur"},{"dob":"68 million BCE","id":222,"name":"Tyrannosaurus"}]
//Converting userDOArrayList JSON back to Arraylist of UserDOs.
JSONDeserializer<ArrayList> jsonDeserializer_list = new JSONDeserializer<ArrayList>().use("values", UserDO.class);
ArrayList<UserDO> userDOArrayList_fromJSON = jsonDeserializer_list.deserialize(userDOArrayList_Json);
System.out.println("\nPrint UserDOArrayList_FromJSON :\n" + userDOArrayList_fromJSON);

Output:

Print UserDOArrayList_FromJSON :
[Printing UserDO: 
name = Dinosaur
id = 111
dob = 200 million BCE, Printing UserDO: 
name = Tyrannosaurus
id = 222
dob = 68 million BCE]

The full code used in this example is provided below.
1. UserDO.java

package com.jsession4d;

import java.util.ArrayList;

public class UserDO {

private String name;
private long id;
private String dob;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}

public String toString(){
return "Printing UserDO: \nname = " + name + "\n" + "id = " + id + "\n" + "dob = " + dob;
}

}

2. RunFlexJson.java

package com.jsession4d;

import java.lang.reflect.Type;
import java.util.ArrayList;

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;

public class RunFlexJson {

public static void main(String[] args) {
try{			
//Creating UserDO1.
UserDO userDO1 = new UserDO();
userDO1.setId(111);
userDO1.setName("Dinosaur");
userDO1.setDob("200 million BCE");

//Creating UserDO2.
UserDO userDO2 = new UserDO();
userDO2.setId(222);
userDO2.setName("Tyrannosaurus");
userDO2.setDob("68 million BCE");

//Setting both UserDO1 and UserDO2 into an ArrayList.
ArrayList<UserDO> userDOList = new ArrayList<UserDO>();
userDOList.add(userDO1);
userDOList.add(userDO2);


//Converting userDO to JSON String and printing.
String userDO1_Json_with_classname = new JSONSerializer().serialize(userDO1);
System.out.println("JSON String of UserDO1 :\n" + userDO1_Json_with_classname);

//Converting userDO to JSON String and printing.
String userDO1_Json = new JSONSerializer().exclude("*.class").serialize(userDO1);
System.out.println("JSON String of UserDO1 :\n" + userDO1_Json);

//Converting userDO arrayList to JSON String.
String userDOArrayList_Json = new JSONSerializer().exclude("*.class").serialize(userDOList);
System.out.println("JSON String of ArrayList :\n" + userDOArrayList_Json);

//Converting UserDO1 JSON back to UserDO
JSONDeserializer<UserDO> jsonDeserializer = new JSONDeserializer<UserDO>();
UserDO UserDO1_FromJSON = jsonDeserializer.deserialize(userDO1_Json,UserDO.class);
System.out.println("\n\n"+ UserDO1_FromJSON);

//Converting userDOArrayList JSON back to Arraylist of UserDOs.
JSONDeserializer<ArrayList> jsonDeserializer_list = new JSONDeserializer<ArrayList>().use("values", UserDO.class);
ArrayList<UserDO> userDOArrayList_fromJSON = jsonDeserializer_list.deserialize(userDOArrayList_Json);
System.out.println("\nPrint UserDOArrayList_FromJSON :\n" + userDOArrayList_fromJSON);

}catch(Exception e){
e.printStackTrace();
}

}

}

3. Output

JSON String of UserDO1 :
{"class":"com.jsession4d.UserDO","dob":"200 million BCE","id":111,"name":"Dinosaur"}
JSON String of UserDO1 :
{"dob":"200 million BCE","id":111,"name":"Dinosaur"}
JSON String of ArrayList :
[{"dob":"200 million BCE","id":111,"name":"Dinosaur"},{"dob":"68 million BCE","id":222,"name":"Tyrannosaurus"}]


Printing UserDO: 
name = Dinosaur
id = 111
dob = 200 million BCE

Print UserDOArrayList_FromJSON :
[Printing UserDO: 
name = Dinosaur
id = 111
dob = 200 million BCE, Printing UserDO: 
name = Tyrannosaurus
id = 222
dob = 68 million BCE]

We need to alter the code a little bit for serializing complex java objects in Flexjson if we use serialize method.

To test that, lets make our UserDO java object little more complex by adding a nested object or an object inside the UserDO object and an ArrayList.
Navigate to next page to see it in action.

%d bloggers like this: