kidager / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

Strict parsing not as strict as it should be #499

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
According to the docs, in strict parsing "Names that are unquoted" should throw 
an error, however the following code parses the json perfectly fine, although 
lenient is false:
new JsonParser().parse("{invalid: 1}");

I am using gson-2.1.

Original issue reported on code.google.com by rum...@google.com on 29 Jan 2013 at 10:45

GoogleCodeExporter commented 9 years ago
For backwards compatibility, JsonParser is always lenient. Use JsonReader for 
strict parsing.

Original comment by limpbizkit on 4 Feb 2013 at 4:06

GoogleCodeExporter commented 9 years ago
With JsonReader, it still works:
JsonReader reader = new JsonReader(new StringReader("{invalid: 1}"));
reader.setLenient(false);
new JsonParser().parse(reader).getAsJsonObject().get("invalid").getAsInt();

Or did you mean something different?

Original comment by rum...@google.com on 5 Feb 2013 at 3:45

GoogleCodeExporter commented 9 years ago
I agree, this issue should be re-opened as the JsonReader isLenient() is 
ignored.

The fromJson(JsonReader reader, Type typeOfT) method has this code:

    boolean oldLenient = reader.isLenient();
    reader.setLenient(true);

Gson is overriding the reader's leniency.

Original comment by craig.es...@gmail.com on 18 Mar 2014 at 9:28