Reputation: 21654
I have been using JSONParser
's parse method without many issues.
Recently, I decided to pay attention to a deprecation notice that I had been seeing. It recommended that I use either parseStrict
or parseLenient
.
So, I decided to try out parseStrict
.
I declared a json string...
String jsonstr = "{value : [12,34],[56,78]]}";
...I confirmed that it worked with good old parse
...
JSONValue jsv = JSONParser.parse(jsonstr);
...and an alert window tells me value of jsv was this:
{"value" : [12,34],[56,78]]}
Then I used parseStrict
on the same string:
JSONValue jsv = JSONParser.parseStrict(jsonstr);
But my GWT app crashed with an exception!
What are the requirements in using parseStrict
(vs parse
)? Wny did it trip on such a simple little json string?
Uncaught exception escaped
com.google.gwt.event.shared.UmbrellaException: One or more exceptions caught, see full set in UmbrellaException#getCauses
at com.google.gwt.event.shared.HandlerManager.fireEvent(HandlerManager.java:129)
......
at com.google.gwt.json.client.JSONParser.evaluate(JSONParser.java)
at com.google.gwt.json.client.JSONParser.parse(JSONParser.java:218)
at com.google.gwt.json.client.JSONParser.parseStrict(JSONParser.java:87)
Upvotes: 1
Views: 3962
Reputation: 15370
In the strictest sense, the JSON you've provided just isn't strictly correct.
JSON key values should be surrounded by double quotes as described in the JSON spec, so your example JSON should be as follows:
String jsonstr = "{\"value\" : [[12,34],[56,78]]}";
Also, it appears that your braces ([]
) don't match up (which I have also corrected).
In summary, it could be either the missing matching brace, or the lack of double quotes. To find out, you can wrap the offending code in a try / catch block and do as the stack trace suggests. Namely, call the getCauses
method on the exception:
try {
JSONValue jsv = JSONParser.parseStrict(jsonstr);
} catch (UmbrellaException e) {
Set causes = e.getCauses();
//actually find out what the problem was
}
Note: JSONParser.parse
just uses eval
under the hood so be careful when using it!
Upvotes: 5