Showing posts with label tojson. Show all posts
Showing posts with label tojson. Show all posts

4/25/2011

Creating JSON String From Bean - 2

I have to say one more thing about JsonConfig class. Suppose you have a bean with not only primitive objects (like String, int etc.), but also some other classes.

Suppose your class structure is as follows. You have a Person class:
public class Person {

    String name;
    String surname;

    // getters and setters here!

}
And you have a family class that has some persons in it:
public class Family {

    Person mother;
    Person father;
    String anniversary;

    // getters and setters here!

}
Now let's create some instances:
Person person1 = new Person();
person1.setName("Münir");
person1.setSurname("Özkul");

Person person2 = new Person();
person2.setName("Adile");
person2.setSurname("Naşit");

Family family = new Family();
family.setFather(person1);
family.setMother(person2);
family.setAnniversary("01/01/1986");
Now we want JSON String of this family class. All we have to do is create a JsonConfig instance and set its classmap.
JsonConfig jsonConfig = new JsonConfig();

jsonConfig.setRootClass(Family.class);

Map<String,Class> itemMap = new HashMap<String,Class>();
itemMap.put("mother",Person.class);
itemMap.put("father",Person.class);

jsonConfig.setClassMap(itemMap);
In this classmap, we will add each non-primitive attribute of the root class (in this case Family) to the map with its corresponding classes. In this way, JSONObject will know how to call the getter and setter of this attribute.
Now we will again pass jsonConfig instance to fromObject method:
String resultJson = JSONObject.fromObject(family,jsonConfig).toString();
resultJson will be:
{
    "anniversary" : "01/01/1986",
    "mother" : {
            "name" : "Adile",
            "surname" : "Naşit"
    },
    "father" : {
            "name" : "Münir",
            "surname" : "Özkul"
    }
}

Ref: http://json-lib.sourceforge.net/

4/22/2011

Creating JSON String From Bean

I will use net.sf.json.* package for JSON processing. You can find the documentation and library files from here: http://json-lib.sourceforge.net/

Suppose you have the following class:
public class UserInfo {               

    String id;
    String userName;
    String birthPlace;

    // getters and setters here!

}
First, let's create an instance:
UserInfo userInfo = new UserInfo();
userInfo.setId("BFASHRGHFG");
userInfo.setUserName("User1");
userInfo.setBirthPlace("Madrid");
You can create JSON formatted string from this class with the following line:
String resultJson = JSONObject.fromObject(userInfo).toString();
resultJson will be:
{ "id" : "BFASHRGHFG", "userName" : "User1", "birthPlace" : "Madrid" }
We may want some fields not to be included in resulting json string, then we will create a JsonConfig instance and set exclude list of this instance:
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setExcludes(new String[] { "id" });
Then we may use this configuration as another parameter to fromObject method:
String resultJson = JSONObject.fromObject(userInfo,jsonConfig).toString();
resultJson will be:
{ "userName" : "User1", "birthPlace" : "Madrid" }