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

GsonBuilder.setLenient(false) #372

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

GsonBuilder.setLenient(false) #372

GoogleCodeExporter opened this issue Mar 19, 2015 · 13 comments

Comments

@GoogleCodeExporter
Copy link

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 [email protected] on 26 Oct 2011 at 2:11

sgbrown added a commit to sgbrown/gson that referenced this issue Feb 23, 2016
also updated JsonReader.nextDouble() method to deserialize infinite values, following the comments
in GsonBuilder.serializeSpecialFloatingPointValues() method which claims:
"Gson always accepts these special values during deserialization"

These fixes should also address issue google#372
@sgbrown
Copy link
Contributor

sgbrown commented 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.

@danorton-cubic-austin
Copy link

This also affects JsonWriter. See #1093 and #372 .

@vkopichenko
Copy link

vkopichenko commented 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

@Marcono1234
Copy link
Collaborator

In the next Gson version a new Strictness mode (#2437) will be added. While Gson will then still by default be lenient, you can however use

gsonBuilder.setStrictness(Strictness.STRICT)

and the Gson instance will respect that for all parsing it does.

@eamonnmcmanus
Copy link
Member

I think the situation described by @Marcono1234 is enough to address the concerns here, and as much as we can do while maintaining compatibility.

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

6 participants