Currently attempting to stringify a Java ArrayList object in GWT using an interop method to call the native JSON.stringify(ArrayListobj). This results in a perfect JSON representation of the underlying array list contents in the form of an array. This even works for seemingly more complicated Java class objects, but in this instance I'm going to use Strings to demonstrate. First on my creation side:
test = new ArrayList<>();
test.add("first");
test.add("second");
String jsonstr = JSON.stringify(test);
Now based on my JsInterop code, I'm returning an Object (or object array as is the case in the following code shown here):
@JsType(isNative=true, namespace=GLOBAL)
public class JSON {
public native static String stringify(Object obj);
public native static Object[] parse(String obj);
}
So far so good, all of this works perfectly, and the results I get are stringified JSON representation of the contents of the ArrayList
"{"array_0":["first","second"]}"
Then run this bit through the parser:
ArrayList<String> returned = new ArrayList<>();
Object[] var = JSON.parse(jsonstr);
And var IS a proper reprsentation (when looking at the web browsers execution paused) of the underlying data from the first ArrayList. The problem is getting the Object[] of the JSON array converted back to the Java ArrayList object.
I have tried using JSNI code to extract the array elements, which actually in the console tray on the web browser works perfectly, but the compliler tries to outsmart me and renames the array element so my JSNI code can't touch it.
If my code is as above, and I write JSNI something like:
public static native ArrayList objToList(ArrayList add,Object inVal) /*-{
var length = inVal.array.length;
for(var i = 0;i < length; i++){
add.array[i] = inVal.array[i];
}
return add;
}-*/;
Then the compiler will rename the array array_0 so my code that says inVal.array no longer ties in with the data I'm trying to access.
From all the testing I've done this is by far the fastest method of getting the same ArrayList object (its guaranteed to be defined the same way in both places) from one place in the client software to another place in client software (no server involved here) through stringification.
But the information about how to manipulate the JavaScript on a low level in GWT is, lacking at best.
And I've tried every variation on GWT-RPC mechanisms, GWT-Jackson, AutoBeans (if only they supported objects with multiple primitive types!) requestbuilder, you name it.
And NO, before you suggest it I'm not interested in doing a full GWT-JSON parse of the string again, I already did that when originally pulling the thousands of records off the server and pushed them into the Java ArrayList. It takes 200+mS to parse the JSON in GWT, while the browsers JSON parse function processes this string in around 3mS.