Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature to allow trailing commas (non-compliant JSON) #118

Closed
cburroughs opened this issue Nov 14, 2013 · 11 comments
Closed

feature to allow trailing commas (non-compliant JSON) #118

cburroughs opened this issue Nov 14, 2013 · 11 comments

Comments

@cburroughs
Copy link

Splitting off from #52 and originally at http://jira.codehaus.org/browse/JACKSON-618

Presumably a feature named: ALLOW_TRAILING_COMMA

If enabled the following would be valid and yield the same result:

  • [1,2,3,] [1,2,3]
  • {1:1,} {1:1}
@UnquietCode
Copy link

+1

Even though this is getting a little far afield for acceptable representations, Jackson is pretty magical about handling other nonsense (like comments), and this would be handy.

@cakoose
Copy link

cakoose commented Mar 18, 2014

Datapoint: Google's "gyp" build tool uses a config file that is JSON with "#" comments and trailing commas: https://code.google.com/p/gyp/wiki/GypUserDocumentation

(It's strange that they went with "#" instead of "//". Looks more like Python than JSON.)

@cowtowncoder
Copy link
Member

@cakoose FWIW, "script comments" are supported by Jackson 2.3 with JsonParser.Feature.ALLOW_YAML_COMMENTS. I added it for better inter-operability between YAML -- YAML, alas, chose that comment style instead of c/c++ comments.

I think I am actually supportive of a single trailing comma (more so than with "missing entries", that is, possible in-sequence commas), but there's still question of efficient implementation -- I do not want any measurable performance impact for the common case of no trailing commas.

@cowtowncoder cowtowncoder changed the title feature to allow trailing commas feature to allow trailing commas (non-compliant JSON) Sep 30, 2014
@lkleen
Copy link

lkleen commented Dec 10, 2014

+1 for gyp it would be really great if it would be possible to parse a trailing comma

@yonik
Copy link

yonik commented Dec 20, 2014

Here's what I decided for Noggit:

  /** ALLOW_EXTRA_COMMAS causes any number of extra commas in arrays and objects to be ignored
   * Note that a trailing comma in [] would be [,] (hence calling the feature "trailing" commas
   * is either limiting or misleading.  Since trailing commas is fundamentally incompatible with any future
   * "fill-in-missing-values-with-null", it was decided to extend this feature to handle any
   * number of extra commas.
   */
  public static final int ALLOW_EXTRA_COMMAS                     = 1 << 5;

Allowing multiple extraneous commas makes things like templating JSON easier.
Examples:
http://heliosearch.org/noggit-json-parser/#Comma

@cowtowncoder
Copy link
Member

@yonik Thanks! This is useful to know wrt actual existing usage.

I am still bit unsure about multiple commas, since even in templating it'd seem like two commas would suggest a missing value, which would seem more natural as null? But at the same time, allowing ignoring any number of commas would probably be easier to implement actually.
It'd of course be easy to add separate features to enable slightly differing variations but that might be an overkill.

@yonik
Copy link

yonik commented Dec 20, 2014

I am still bit unsure about multiple commas, since even in templating it'd seem like two commas would > suggest a missing value,

So can a trailing comma ;-)

I was coming from the point of view that it's not sane to enable "missing values" and "allow trailing comma" at the same time. Given that, if one is to allow trailing commas, one may as well allow any number of extra commas.

I guess it depends on the context. For something like Solr filter lists, position does not matter, only values. If one is dealing with parallel arrays (two different arrays where the values line up), then one would want to retain the "missing" values to maintain the relative positions. But for these use-cases it seems like one would want to completely turn off trailing commas and turn on missing values.

@cowtowncoder
Copy link
Member

Yes, good point. So I think it actually would make sense to have orthogonal feature, if choice between missing value / bogus comma is important. I was thinking that a single trailing comma is most commony used at end of lists, when appending new entries, but that is probably not the only case.

@cakoose
Copy link

cakoose commented Dec 22, 2014

Just to record my preference here: it would be nice to have a "trailing commas" option that didn't also allow extra commas in other parts of a list. For example, if I were using Jackson on Gyp files, I'd like to be able to parse them strictly.

@albertocavalcante
Copy link

I'm in a situation where a third party is sending a leading comma:

  • {,1:1} {1:1}

Any chance already having a feature flag similar to the ALLOW_TRAILING COMMA? Or any tips on implementing this custom deserialization?

Thanks

@cowtowncoder
Copy link
Member

@albertocavalcante There is JsonReadFeature.ALLOW_MISSING_VALUES (and deprecated JsonParser.Feature.ALLOW_MISSING_VALUES) which might work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants