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

why gson can parse some string not stand json such as "[a,b,c]" ? #763

Closed
somewon opened this issue Dec 31, 2015 · 6 comments
Closed

why gson can parse some string not stand json such as "[a,b,c]" ? #763

somewon opened this issue Dec 31, 2015 · 6 comments

Comments

@somewon
Copy link

somewon commented Dec 31, 2015

        JsonParser parser = new JsonParser();
        JsonElement el = parser.parse("[a,b,c]");
        JsonArray array = new JsonArray();
        array = el.getAsJsonArray();

? why?

@JakeWharton
Copy link
Contributor

It's not valid JSON. Try "[\"a\", \"b\", \"c\"]" instead.

@somewon
Copy link
Author

somewon commented Jan 3, 2016

Yes. I know that. It's not valid JSON. But gson could parse this.
I'm curious about why gson have this feature.

@JakeWharton
Copy link
Contributor

Oh you're asking why it can parse it? For historical reasons Gson is lenient by default and unfortunately there's no sane API to disable it for JsonParser that I can see. Even if you create your own JsonReader and call setLenient(false) Gson will ignore that setting.

Disabling the lenient behavior can be done when using the object binding API (via the Gson type), but there's hoops to jump through. Are you able to use the binding API instead of the DOM API?

@pretizy
Copy link

pretizy commented Jan 4, 2016

I haven't reviewed the code, but can you explain why the lenient behavior
affects the json standard format
On Jan 4, 2016 6:12 AM, "Jake Wharton" notifications@github.com wrote:

Oh you're asking why it can parse it? For historical reasons Gson is
lenient by default and unfortunately there's no sane API to disable it for
JsonParser that I can see. Even if you create your own JsonReader and
call setLenient(false) Gson will ignore that setting.

Disabling the lenient behavior can be done when using the object binding
API (via the Gson type), but there's hoops to jump through. Are you able
to use the binding API instead of the DOM API?


Reply to this email directly or view it on GitHub
#763 (comment).

@Marcono1234
Copy link
Collaborator

Probably resolved (but not really fixed) by the documentation improvements of #2122.

@eamonnmcmanus
Copy link
Member

I think those documentation improvements are probably the best we can do given compatibility requirements and the maintenance-mode status of this project.

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

5 participants