question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue
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

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:4
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

10reactions
inder123commented, Oct 10, 2018

We will not support this behavior change by default. Write better JSON. We could consider a GsonBuilder property ignoreTrailingCommas() but that looks hidious.

6reactions
BoDcommented, Oct 10, 2018

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found