Fix JsonReader and JsonTreeReader nextLong() not failing on precision loss #1737
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #1727
JsonReader.nextLong()
tries to parse a value as double in case parsing as long failed. An exception should be thrown if converting the double to long causes precision loss.However, previously if the parsed value could not be represented exactly as double, the precision loss was not detected.
For example:
This pull request changes it so if the number (or number string) represents a value which matches the syntax required by
Long.parseLong(String)
, then the value is not parsed as double.However, if the value does not match that syntax precision loss is still possible. But since the value uses
double
syntax, it is assumed that the source of this data is aware that floating point can cause precision loss. Trying to prevent this as well would require writing an own parsing method similar toDouble.parseDouble
to remain backward compatible and the amount of effort is likely not worth it.This pull request also updates JsonTreeReader to fall back to parsing as double when parsing as int / long fails and to check for precision loss as well.