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

Make it possible to allow comments in JSON #212

Closed
GoogleCodeExporter opened this issue Mar 19, 2015 · 6 comments
Closed

Make it possible to allow comments in JSON #212

GoogleCodeExporter opened this issue Mar 19, 2015 · 6 comments

Comments

@GoogleCodeExporter
Copy link

GoogleCodeExporter commented Mar 19, 2015

I am working on a project (http://code.google.com/p/plovr/) that is trying
to use JSON as the format for a config file. The inability to comment
something out is making me consider XML instead :( You can see the
workaround that I'm using (which I believe is what Crockford recommends) here:

http://code.google.com/p/plovr/source/browse/test/org/plovr/integration-test-con
fig.js

This is a pretty horrible option for multi-line comments, or when you are
experimenting and are frequently enabling/disabling various options.

I realize that // and /* comments are no longer part of the JSON spec, but
in practice, it would be great if it were possible to instantiate a
com.google.gson.JsonParser() in such a way that it would tolerate comments.
I believe some of the other JSON parsers offer this mode.

Original issue reported on code.google.com by bolinf...@gmail.com on 2 Jun 2010 at 1:42

@GoogleCodeExporter
Copy link
Author

GoogleCodeExporter commented Mar 19, 2015

Fixed by r584.

Original comment by limpbizkit on 27 Aug 2010 at 7:34

  • Changed state: Fixed
  • Added labels: Type-Enhancement
  • Removed labels: Type-Defect

@tompetrillo
Copy link

tompetrillo commented Oct 20, 2021

Was this issue actually solved? I see the comment "Fixed by r584." but i have no idea how to find release 584. It doesn't seem obvious from gsonbuilder how i would enable c style comments.

@google google deleted a comment from GoogleCodeExporter Oct 20, 2021
@google google deleted a comment from LegendLength Oct 20, 2021
@eamonnmcmanus
Copy link
Member

eamonnmcmanus commented Oct 20, 2021

I think the information about which exact change was r584 was lost when this code was migrated from Google Code to GitHub, but we do still have the commit history including dates. Based on that, I think it was this change. The documentation could be improved, but based on JsonReader.setLenient we might guess that GsonBuilder.setLenient() is supposed to allow comments (as well as a bunch of other things). However as far as I can see comments are always enabled. Also see this test.

@Marcono1234
Copy link
Collaborator

Marcono1234 commented Oct 20, 2021

However as far as I can see comments are always enabled.

The underlying issue is probably that Gson's fromJson methods always make the reader lenient regardless of the setting value, see also #804.

@eamonnmcmanus
Copy link
Member

eamonnmcmanus commented Oct 20, 2021

I don't think we can change this default leniency, but we could modify the documentation to describe the actual behaviour, and we could potentially add GsonBuilder.setLenient(boolean) to allow people to get strict parsing without using JsonReader directly.

@Marcono1234
Copy link
Collaborator

Marcono1234 commented Nov 6, 2021

we could potentially add GsonBuilder.setLenient(boolean)

That sounds like a good idea; it would allow fixing the whole issue that some methods of Gson cannot be made strict. However, the problem is that even in its current form JsonReader is not completely strict, see #1609.

So maybe a better solution would be (compared to introducing multiple strictness levels) to create config classes, e.g. JsonReaderConfig and JsonWriterConfig for which the user can individually enable and disable certain features. There could be two default instances then, STRICT (strictly follows the RFCs; would include #1609) and LENIENT (like the current lenient mode).
This would then for example also allow creating a strict JSONC (JSON with comments) reader.

What do you think?

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

No branches or pull requests

4 participants