Skip to content

Fix JsonReader and JsonTreeReader nextLong() not failing on precision loss#1737

Open
Marcono1234 wants to merge 2 commits into
google:mainfrom
Marcono1234:nextLong-parsing
Open

Fix JsonReader and JsonTreeReader nextLong() not failing on precision loss#1737
Marcono1234 wants to merge 2 commits into
google:mainfrom
Marcono1234:nextLong-parsing

Conversation

@Marcono1234
Copy link
Copy Markdown
Contributor

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:

// Last digit is different but values are considered equal
Double.parseDouble("9223372036854775808") == 9223372036854775807L

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 to Double.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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Long upper limit value

2 participants