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?
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("");
}-*/;