1

I have become a great fan of the JavaScriptOverlayTypes.

so lets say, I have the followin JSON object:

 {
  "product": {
    "name": "Widget",
    "prices": 
      { "minQty": 1, "price": 12.49 }
  }
}

So I write my class for products and one for prices. Now if somethings wents wrong when analysing the "price JavascriptObject", I want to print it as the following:

{ "minQty": 1, "price": 12.49 }

but I havent found a possibilty yet to confert the "price JavascriptObject" backt to a string.

Is there a possibilty to do this?

Regards, Stefan

2 Answers 2

7

new JSONObject(priceJso).toString()

Beware of performance thugh, as it'll create a JSONValue object for each property of the object (and recursively of course), and I'm not sure the GWT compiler is able to optimize things much.
In your case, as an "error path", it should be OK though.

Sign up to request clarification or add additional context in comments.

2 Comments

cool, exactly what I wanted. Note, that you have to include <inherits name="com.google.gwt.json.JSON" /> into your projects *.gwt.xml file. On other note, is there a possibility to print the JSON file in a nicely formatted manner?
your module (.gwt.xml) has to inherit the JSON module to make it work: <inherits name="com.google.gwt.json.JSON" />
1

JsonUtils has a nice function for it:

String jsonString = JsonUtils.stringify(priceJson);

Which has the native implementation:

public static native String stringify(JavaScriptObject obj) /*-{ JSON.stringify(obj); }-*/;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.