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.

GsonBuilder.setLenient(false)

See original GitHub issue
The 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:open
  • Created 9 years ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
sgbrowncommented, Feb 23, 2016

I think Gson.lenient flag probably does more harm than good in the current state. The comments on the GsonBuilder.setLenient() method say

By default, Gson is strict and only accepts JSON as specified by RFC 4627.

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

    @Override
   protected void setUp() throws Exception {
     super.setUp();
     gson = new GsonBuilder().setLenient().create();
   }

   public void testLenientFromJson() {
     List<String> json = gson.fromJson(""
         + "[ # One!\n"
         + "  'Hi' #Element!\n"
         + "] # Array!", new TypeToken<List<String>>() {}.getType());
     assertEquals(singletonList("Hi"), json);
   }

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:

     List<String> json = new GsonBuilder().create().fromJson(""
         + "[ # One!\n"
         + "  'Hi' #Element!\n"
         + "]", new TypeToken<List<String>>() {}.getType());
     assertEquals(singletonList("Hi"), json);

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.

0reactions
vkopichenkocommented, Jul 5, 2019

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

Read more comments on GitHub >

github_iconTop 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 >

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