Convert JavaScript Overlays back to JSON?
-
03-07-2019 - |
Question
I'm having trouble converting a an object from JSON into a JavaScript Overlay object, and back again. I have the following in the class now:
public class Aff extends JavaScriptObject {
protected Aff() {};
public static native Aff fromJSONString(String jsonString) /*-{
return eval('(' + jsonString + ')');
}-*/;
public final native String toJSON() /*-{
return this.toString();
}-*/;
// followed by get/seters and a bunch of TODO.
}
I am able to create and work with objects, but the obj.toJSON()
returns [object Object]
. I can't seem to find any way around this without doing a manual convert back into JSON.
Solution
this.toString();
doesnt actually produce json code. It prints the object's string representation. You will need either custom code to write out the json, or better, use a library.
Two ways to use libraries - use gwt's built in json libraries. Not as nice, but dont need to write jsni code. Or, add a script resource to your module xml for a library such as one javascript json library here , and use that in jsni. Or, find another library, there are literally hundreds out there.