GsonBuilder.setLenient(false)
See original GitHub issueThe gson JsonParser is final and it's #parse(JsonReader json) does this:
json.setLenient(true);
In my unit tests I validate the output of my json rest api and I would like to
have a parser with strict parsing. Creating my own StrictJsonReader also is not
possible unless I put it in the com.google.gson package.
Maybe you could add a setter or constructor param for lenient or strict parsing?
Original issue reported on code.google.com by franci...@gmail.com
on 26 Oct 2011 at 2:11
Issue Analytics
- State:
- Created 9 years ago
- Comments:11 (1 by maintainers)
Top Results From Across the Web
Why do I get Gson builder error when starting a Spring Boot ...
When I try to run it as spring boot application. I get the following error. java.lang.NoSuchMethodError: com.google.gson.GsonBuilder.setLenient ...
Read more >com.google.gson.GsonBuilder.setLenient java code examples
Gson gson = new GsonBuilder() .setLenient()
Read more >Example usage for com.google.gson GsonBuilder setLenient
List of usage examples for com.google.gson GsonBuilder setLenient ... getType())) { return true; } } } return false; } @Override public boolean ...
Read more >com.google.gson.GsonBuilder Maven / Gradle / Ivy
GsonBuilder maven / gradle build tool code. ... private boolean lenient = DEFAULT_LENIENT; /** * Creates a GsonBuilder instance that can be used...
Read more >Search - appsloveworld.com
Best coding solution for query java.lang.NoSuchMethodError: com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder;
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think Gson.lenient flag probably does more harm than good in the current state. The comments on the GsonBuilder.setLenient() method say
The comments on Gson.setLenient() also references the JsonReader.setLenient(boolean) method which has a great explanation of what should and shouldn’t be considered when using the lenient flag.
The unfortunate fact here is that due to the Gson.fromJson(JsonReader, Type) method always setting the lenient flag to true when parsing, almost none of what is commented in GsonBuilder about the default behavior of Gson is true. The only affect that setting the lenient flag on Gson will do is to allow comments to be at the end of your json buffer when calling Gson.fromJson(Reader, Type) since the assertFullConsumption(Object, JsonReader) method will be called from here and only checks for data at the end of the buffer having not been consumed (e.g. comments at the end).
Consider, for example, the test in com.google.gson.functional.LeniencyTest
If you were to remove the comment at the end of the String (“# Array!”), then it would not make any difference at all whether or not the lenient flag had been set. The following would also pass:
It seems counterintuitive that with the lenient flag unset, comments in the middle of the JSON data would be ignored but comments at the end would cause a failure when the Gson parser is supposed to be “strict”.
If it is considered undesirable to change the default behavior (to the behavior that is documented) by actually respecting a default lenient flag being set to false (i.e. default to strict), then perhaps the correct answer is to respect the lenient flag, default Gson to have the lenient flag set to true, and update the javadocs to reflect that Gson by default is lenient.
Here is a workaround from #1208 for Gson strict parsing with many details and extensive test case: https://stackoverflow.com/questions/43233898/how-to-check-if-json-is-valid-in-java-using-gson/47890960#47890960