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.

GSON `fromJson` can't parse JSON without quotes!

See original GitHub issue

For example:

{
   name : Google Company,
   age:19
}

That’s OK following codes, I can get correct value:

JsonObject jsonObject = new JsonParser().parse(jsonSt).getAsJsonObject();
String name = jsonObject.get("name").getAsString();
int age = jsonObject.get("age").getAsInt();

but it can’t parse into a bean class using fromJson

will cause Exception like this:

java.lang.RuntimeException: 
com.google.gson.JsonSyntaxException: 
com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 15 path $.name

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:10

github_iconTop GitHub Comments

5reactions
danorton-cubic-austincommented, Oct 30, 2017

This is correct behavior. Your example is not valid JSON. See https://www.json.org/

4reactions
cowtowncodercommented, Oct 24, 2018

One follow-up question, still: why does JsonParser.parse(...) accept content that does not enclose Object keys in double-quotes? I have seen test code that seems to rely on this behavior, but Javadocs:

https://static.javadoc.io/com.google.code.gson/gson/2.8.5/com/google/gson/JsonParser.html#parse-java.io.Reader-

do not really mention anything about more lenient handling. So it would seem like this SHOULD indicate exception, too. But apparently it does not. Would that part be considered a bug, or perhaps missing documentation if more lenient handling is intentional?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Convert json string without quotes into a map - Stack Overflow
To answer your question: There is no safe way of getting your JSON - which is not JSON by the way, because it's...
Read more >
how can one strip quotes from the resulting JSON strings?
In this method, return the string in the way you want, which will be no quotes, and whatever else you wish to do...
Read more >
How to read a JSON string without field name quotes in Jackson
We often have to read JSON values where the field names are not quoted, i.e., when enclosed with double ( "" ) or...
Read more >
How to convert JSON String to Java Object? Gson ... - Java67
It is very simple, you just need to use the fromJson() method of Gson class and you are done. This is the simplest...
Read more >
SyntaxError: JSON.parse: bad parsing - JavaScript | MDN
You cannot use single-quotes around properties, like 'foo'. JSON.parse("{'foo': 1}"); ...
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