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.
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.
we have a JSNI method like that, but use douglas crockfords JSON library (in case the browser doesn't supply one natively):
https://github.com/douglascrockford/JSON-js
public native String stringify() /*-{
return JSON.stringify();
}-*/;
whats nice is that stringify can take parameters to pretty-print the output with a specified indentation... among other things
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With