tags:

views:

249

answers:

1

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.

A: 

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.

Chii
That's really what I wanted to avoid. I have very complex objects left over from a legacy system and don't want to write the JSON conversion by hand. That was the plan if nobody answered, but man it is annoying.
Jack M.