ralfstx / minimal-json

A fast and small JSON parser and writer for Java
MIT License
732 stars 186 forks source link

quoting field names is optional, JsonParser doesn't handle it #86

Closed pstanton closed 6 years ago

pstanton commented 6 years ago

[{fieldName:"value"}] is valid json, however JsonParser throws:

com.eclipsesource.json.ParseException: Expected name at 2:1
        at com.eclipsesource.json.JsonParser.error(JsonParser.java:379)
        at com.eclipsesource.json.JsonParser.expected(JsonParser.java:372)
        at com.eclipsesource.json.JsonParser.readName(JsonParser.java:156)
        at com.eclipsesource.json.JsonParser.readObject(JsonParser.java:139)
        at com.eclipsesource.json.JsonParser.readValue(JsonParser.java:94)
        at com.eclipsesource.json.JsonParser.readArray(JsonParser.java:121)
        at com.eclipsesource.json.JsonParser.readValue(JsonParser.java:92)
        at com.eclipsesource.json.JsonParser.parse(JsonParser.java:73)

version: 0.9.4

arcticicestudio commented 6 years ago

You mistake JSON with JavaScript. In JavaScript the name must only be quoted if it contains illegal characters like e.g. a dash (-) or is a reserved keyword like e.g. null or typeof.

On the other hand the JSON specification explicitly says that

A string is a sequence of zero or more Unicode characters, wrapped in double quotes [...]

Your example code must be changed to

[{"fieldName":"value"}]

to be valid JSON.

You can validate JSON using tools like e.g. a online JSON validator.

pstanton commented 6 years ago

fairnuf