Home  > Resources  > Blog

JSONObject No Longer Breaks Encapsulation

July 1, 2010 by Matthew Silver
Category: Ajax & Web 2.0

JSON is a string representation of a JavaScript object.  It’s been around since the dawn of JavaScript, but only recently gained popularity due to Ajax.  It’s commonly used as a data format for transmitting information from the server to the client.  The appeal of JSON is that the client doesn’t have to write a lot of code to update the page.  All he has to do is convert the JSON back to a JavaScript object (one line of code).  Then he can access the properties of the object and potentially update several portions of the page.

For example, below is the equivalent JSON for a JavaScript object representing a customer:

{"id":10,"name":"J. Buchman","active":true}

Below is the client code that is used to convert the JSON to a JavaScript object and update two div elements in the web page.

var customer = eval( "(" + replyString + ")" );

document.getElementById(“name”).innerHTML = customer.name;

document.getElementById(“active”).innerHTML = customer.active;

<div id="name"></div>
<div id="active"></div>

On the server side, a utility library is typically used to convert Java objects into JSON strings.  www.json.org, which is responsible for monitoring all JSON related activities, provides such a library for Java.  The org.json.JSONObject class represents a JavaScript object.  Its toString method is to used to produce a JSON string.  When creating a  JSONObject, you typically do so based on some existing Java object.  Unfortunately, you need to declare all of its fields as public, which breaks encapsulation.

For example:

public class Customer {
   public int id; 
   public String name; 
   public boolean active; 

To create the JSON string, you would:

1.  Construct an instance of the Customer class and initialize it.

2.  Construct an array of Strings specifying the class’s public field names.

3.  Construct a JSONObject passing in the Customer instance and array of Strings.

4.  Call the JSONObject’s toString method.

For example:

Customer c = new Customer(); 
c.id = 10; 
c.name = "John G. Reeves"; 
c.active = true;

String propList[] = {“name”, “id”, “active”}; //List of public field names

JSONObject jo = new JSONObject(c, propList);

String replyData = jo.toString();

However, things have changed.  A new version of the JSONObject class was recently released, which no longer forces you to break encapsulation.  Instead, you can now pass in a JavaBean instance to the JSONObject constructor.

JSONObject(Object bean)

The new constructor uses reflection to obtain the property names from the public methods that start with “get” or “is” followed by a capital letter.  Consequently, you can keep your fields private and have public methods for accessing them.  This way, you don’t break encapsulation.

Hence, your Customer class and JSON code are simplified and change to the following:

public class Customer {
   private int id;
   private String name;
   private boolean active;

public int getId() {
return id;
public void setId(int id) {
this.id = id;

   // remaining getters and setters
Customer c = new Customer(); 
c.setName("John G. Reeves"); 

JSONObject jo = new JSONObject(c);

String replyData = jo.toString();

For more information on JSON and other related Ajax topics, we recommend this training course:

WA1503 Comprehensive AJAX

Follow Us

Blog Categories