-
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
Make default adapters stricter; improve exception messages #2000
Make default adapters stricter; improve exception messages #2000
Conversation
case STRING: | ||
int intValue = in.nextInt(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JsonReader.nextInt()
can also parse JSON strings as int so I simplified this instead of handling JSON strings separately (as it was the case before). However, this has the following disadvantages:
- The exception thrown for a JSON string which is not an int (e.g.
"abc"
) is not as descriptive anymore because it will now be thrown byJsonReader
- It now allows parsing JSON strings which have a floating point value (but whose value is integral), previously it only allowed ints due to usage of
Integer.parseInt
So maybe it would still be worth handling JSON strings here manually, as it was done before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I don't think it's likely that people would be affected by these particular changes in behaviour. Only recognizing 0 or 1 seems more likely to affect people, but even that seems unlikely. (I'm not sure how often people are serializing BitSet
instances anyway, especially given how verbose the encoding is for sparse sets.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this looks great! Particularly the thorough tests.
case STRING: | ||
int intValue = in.nextInt(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I don't think it's likely that people would be affected by these particular changes in behaviour. Only recognizing 0 or 1 seems more likely to affect people, but even that seems unlikely. (I'm not sure how often people are serializing BitSet
instances anyway, especially given how verbose the encoding is for sparse sets.)
Makes the default type adapters stricter:
byte
andshort
adapter detect numeric overflow (but both allow signed and unsigned value range, e.g. byte allows -128 to 255).For
float
no such check has been added even though conversion from small != 0double
can become 0float
and large finitedouble
values can become Infinityfloat
, because there the 'meaning' is preserved. I have also not adjusted the lenient check to detectfloat
Infinity after conversion from finitedouble
, because the JSON contains finite numbers and therefore should be allowed in non-lenient mode.BitSet
only allows 0 and 1 as numeric valuesImproves exception messages:
JsonReader.getPreviousPath()
, this allows getting the JSON path after a value has been consumed. The existinggetPath()
would for array elements already point to the next element.JsonSyntaxException
s thrown by adapters. Note that the implementation proposed here allows exception message injection (when the JSON comes from an untrusted source), but I am not sure if this is something which Gson should consider; the existingJsonReader
implementation is already vulnerable to this anyway when it includes the JSON path in exception messages for malformed JSON.Improves synchronization of classes using
DateFormat
. Reduces thesynchronized
blocks to the minimum, excluding JSON reading and writing calls to only hold the lock as short as possible.