Fix interpretation of trailing comma when setLenient() is true.
See original GitHub issueThe 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
Issue Analytics
- State:
- Created 9 years ago
- Reactions:4
- Comments:11 (1 by maintainers)
Top Results From Across the Web
Python - Remove comma of last object in a string for valid JSON
If your JSON is valid except for the trailing commas, you can try using more relaxed parsers like the other solution ...
Read more >Fixing JSON - Hacker News
You are assuming that trailing commas are ignored as valid, which is true of JavaScript but not of JSON or of C (the...
Read more >java.util.GregorianCalendar: incorrect validation in non-lenient
setLenient() (see java.util.Calendar.setLenient() ) states: ----- Specify whether or not date/time interpretation is to be lenient.
Read more >Editing REAL Data (D, E, F, G) (FORTRAN 77 Language ...
Commas in Formatted Input. If you are entering numeric data that is controlled by a fixed-column format, then you can use commas to...
Read more >DateFormat setLenient() Method in Java with Examples
The boolean value true turns on the leniency mode and false turns off ... Below programs illustrate the working of setLenient() Method of ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We will not support this behavior change by default. Write better JSON. We could consider a GsonBuilder property ignoreTrailingCommas() but that looks hidious.
You mean more hideous than the current “happily parse invalid JSON without a warning and result in a very surprising results that nobody should reasonably expect” behavior?