tags:

views:

1227

answers:

1

In GWT, what is the best way to convert a JavaScriptObject overlay type into a JSON string?

I currently have

public final String toJSON() {  
 return new JSONObject(this).toString();
}

Which seems to work fine. I would like to know if there are any better approaches.

+1  A: 

I've never actually tried that (only consumed JSON so far, never needed to produce it). This seems to be native browser/javascript functionality.

You could write it as:

public native String toJSON() /*-{
  return this.toString();
}-*/;

They essentially just do the exact same thing and likely result in identical JavaScript output. The optimizing compiler is really amazing.

Mark Renouf