JSONArray not properly working with gwt-test-utils

138 Views Asked by At

The following code

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArrayMixed;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONString;

[...]

JsArrayMixed jsArray = JavaScriptObject.createArray().cast();
jsArray.push("something");    
JSONArray jsonArray = new JSONArray(jsArray);    
System.out.println(jsonArray.size());

will print "0" instead of the expected "1" when run with gwt-test-utils. Looking at the JSONArrayPatcher in gwt-test-utils it seems that the value given to the constructor is completely ignored.

Is there a way to make those lines print "1"? Maybe there is a way to patch the constructor of JSONArray?

1

There are 1 best solutions below

2
On

JSONArray has nothing to do with JsArrayMixed. JSONArray is org.json.JSONArray and JsArrayMixed is gwt client object of type com.google.gwt.core.client.JsArrayMixed. These are so completedly from different class hierarchy, that they don't just understand each other.

JSONArray maybe works if you construct it with JSONObject instance or another JSONArray.

You have to invent another way to construct your mixed object.


My sources:

[1] http://www.json.org/javadoc/org/json/JSONArray.html
[2] http://srcrr.com/java/google/gwt/2.2/reference/com/google/gwt/core/client/JsArrayMixed.html