fredsa / forplay

Automatically exported from code.google.com/p/forplay
Apache License 2.0
12 stars 4 forks source link

Java JSON parser is looser than JSON.parse() #9

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
The browser uses JSON.parse(), which is apparently quite strict. Java/Android 
use the json.org parser, which is quite a bit looser. So far I've found the 
following differences:

- JSON.parse() disallows trailing commas in arrays and property lists.
- JSON.parse() disallows unquoted string keys.

We need to update the json.org parser to match this behavior, because it really 
sucks to find out about it the hard way when you convert to HTML!

Original issue reported on code.google.com by jgw@google.com on 13 May 2011 at 2:36

GoogleCodeExporter commented 9 years ago
Just found another case: missing boolean values (.getBoolean("does not exist")) 
return as false for Java but throw an exception in Html.

Original comment by pdr@google.com on 1 Jul 2011 at 1:44

GoogleCodeExporter commented 9 years ago
I just wrote some code that heavily depends on the assumption that 
.getBoolean() returns false, .getNumber() returns 0 etc. (Don't worry, it's not 
committed.)

It makes lots of things simpler if there are default values for the calls, but 
it also causes some issues. So currently there is no easy way to check if the 
boolean key is missing or if it's just false. Yes, you could get the key array 
and iterate through that, but that's far from optimal.

Related problem is the situation where you call .getBoolean() on a key that's 
actually a number. Does anyone have any idea how that works in different 
implementations?

If I may suggest a solution, something like .getKeyType() which would return 
the type or something like Json.UNDEFINED if the key doesn't exist.

But it sounds like getting the defaults working should be a lot less work and 
it's more urgent anyway.

Original comment by aki.jan...@rovio.com on 1 Jul 2011 at 10:46

GoogleCodeExporter commented 9 years ago
Just a random tip for checking that your JSON is valid: You can paste it into 
http://jsonlint.com. JsonLint will flag some of these issues (trailing commas).

Original comment by pdr@google.com on 5 Jul 2011 at 8:02