-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Comments
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
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 |
In the next Gson version a new gsonBuilder.setStrictness(Strictness.STRICT) and the |
I think the situation described by @Marcono1234 is enough to address the concerns here, and as much as we can do while maintaining compatibility. |
Original issue reported on code.google.com by
[email protected]
on 26 Oct 2011 at 2:11The text was updated successfully, but these errors were encountered: