emanuelteixeira / google-gson

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

Fix interpretation of trailing comma when setLenient() is true. #494

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
The following code exhibits the issue:

    StringReader reader = new StringReader("{foo: ['bar', 'baz',]}");
    JsonReader jsonReader = new JsonReader(reader);
    jsonReader.setLenient(true);
    JsonParser parser = new JsonParser();
    JsonElement root = parser.parse(jsonReader);
    assertEquals(
        "Trailing comma should be ignored like in ECMAScript 5.",
        2,
        root.getAsJsonObject().get("foo").getAsJsonArray().size());

Currently, GSON returns an array that corresponds to:

    ["bar", "baz", null]

Instead of:

    ["bar", "baz"]

This is atrocious. The current behavior is consistent with bad browser behavior 
from the last decade. The expected behavior is consistent with ECMAScript 5, as 
well as any programmer who has ever wanted trailing commas to be ignored so 
that it is easier to write lists of things. Here is more information on the 
badness of the current JSON spec:

    http://bolinfest.com/essays/json.html

Please, please fix this. Anyone who uses JSON for a config language wants to be 
able to write data like this:

   [
     "foo",
     "bar",
   ]

such that it is possible to add something to the list without having to clean 
up the comma from the previous line. The current behavior is incredibly 
counterintuitive.

Also, while we're on the subject, the trailing comma should also be supported 
for map entries:

  {
    "foo": 42,
    "bar": 24,
  }

Again, this makes it considerably easier to maintain JSON data files.

Original issue reported on code.google.com by bolinf...@gmail.com on 6 Jan 2013 at 11:58

GoogleCodeExporter commented 9 years ago
I'm worried about breaking others by making such a change. Our current behavior 
was initially implemented to be consistent with the json.org reference 
implementation.

That said, it would a good idea for a 3.0 release, where we can document that 
we're breaking things in the name of forward progress.

Original comment by limpbizkit on 4 Feb 2013 at 3:59

GoogleCodeExporter commented 9 years ago
Yes please. I just got burned by this. No parse error, but runtime error by the 
thing that consumed my JSON array because there was an unexpected null at the 
end.

Original comment by bolinf...@gmail.com on 21 Mar 2013 at 11:31

GoogleCodeExporter commented 9 years ago
This is the only issue that is tagged 3.0. Is 3.0 actually on the horizon?

Original comment by bolinf...@gmail.com on 19 Aug 2014 at 5:23

GoogleCodeExporter commented 9 years ago
IMHO allowing the trailing comma should be separated from the lenient mode. I'd 
never come to the idea of using lenient mode with features like "Array elements 
separated by ; instead of ," - that's plain awful. But trailing commas is 
something what happens all the time when editing, especially with maps.

Original comment by Maaarti...@gmail.com on 27 Aug 2014 at 3:48