JSONObject bug

62 views
Skip to first unread message

vsmak

unread,
Aug 16, 2006, 5:37:36 PM8/16/06
to Google Web Toolkit
Hello,

I've run into a bug with putting JSONObjects inside of JSONObjects.
Here's some sample code.

public void onModuleLoad()
{
JSONObject j1 = new JSONObject();
j1.put("test1", new JSONString(""));

JSONObject j2 = new JSONObject();
j2.put("test1", new JSONString(""));

JSONObject j3 = new JSONObject();
j3.put("j1", j1);
j3.put("j2", j2);

Window.alert(j3.toString());
}

The expected output is:
{"j1":{"test1":""}, "j2":{"test1":""}}

But instead it returns:
{"test1":{"test1":""}, "test1":{"test1":""}}


Thanks. Is there a workaround until the next patch is released?

Henry

unread,
Aug 17, 2006, 11:32:59 PM8/17/06
to Google Web Toolkit
That is a bug -- thanks for finding it. You can work around it by
calling the alternative toString() routine below. Just put this static
function in a class (e.g. JSONPatch), and replace
xxxx.yyyy.zzzz.JSONPatch with the package name you've chosen.

The bug has already been fixed and will be included in the next GWT
release.

Thanks again,

Henry

/**
* Converts a JSONObject into a JSON representation that can be used
to
* communicate with a JSON service.
*
* @return a JSON string representation of this JSONObject instance
*/
public static native String toStringJSONObject(JSONObject obj) /*-{
for(var x in
this.@com.google.gwt.json.client.JSONObject::backStore) {
// we are wrapping everything in backStore so that frontStore is
canonical

this.@com.google.gwt.json.client.JSONObject::get(Ljava/lang/String;)(x
);
}
var out = [];
out.push("{");
var first = true;
for(var x in obj.@com.google.gwt.json.client.JSONObject::frontStore
) {
if(first) {
first = false;
} else {
out.push(", ");
}
var subItem =
(obj.@com.google.gwt.json.client.JSONObject::frontStore[x ]);
var subObj=null;

if(!(subItem.@com.google.gwt.json.client.JSONValue::isObject()())) {

subObj =
(subItem).@com.google.gwt.json.client.JSONValue::toString ()();
} else {
subObj =

@xxxx.yyyy.zzzz.JSONPatch::toStringJSONObject(Lcom/google/gwt/json/client/JSONObject;)(subItem);
}
out.push("\"");
out.push (x);
out.push("\":");
out.push(subObj);
}
out.push("}")
return out.join("");
}-*/;

Reply all
Reply to author
Forward
0 new messages