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

Fix JsonReader.nextDouble() throwing wrong exception type for NaN and Infinity #1767

Conversation

Marcono1234
Copy link
Collaborator

The documentation says that a NumberFormatException will be thrown in case the value is not finite (i.e. NaN or Infinity). However, the JsonReader implementation actually throws a MalformedJsonException.

Note that JsonTreeReader currently throws a NumberFormatException, as expected.

… Infinity

The documentation says that a NumberFormatException will be thrown in case
the value is not finite (i.e. NaN or Infinity). However, the JsonReader
implementation actually throws a MalformedJsonException.
Note that JsonTreeReader currently throws a NumberFormatException, as
expected.
@google-cla google-cla bot added the cla: yes label Aug 25, 2020
@@ -907,7 +907,7 @@ public double nextDouble() throws IOException {
peeked = PEEKED_BUFFERED;
double result = Double.parseDouble(peekedString); // don't catch this NumberFormatException.
if (!lenient && (Double.isNaN(result) || Double.isInfinite(result))) {
throw new MalformedJsonException(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like it is indeed bad JSON since JSON forbids NaN and infinities.
The number may actually be in correct format.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though then the question is why NumberFormatException is thrown in the first place when the number cannot be parsed as double, int or long since it is malformed JSON in these cases as well.

@inder123
Copy link
Collaborator

When in doubt, I prefer to preserve the current behavior.

@inder123 inder123 closed this Aug 26, 2020
@Marcono1234
Copy link
Collaborator Author

So should the documentation then be updated to state that a MalformedJsonException is thrown and JsonTreeReader be adjusted accordingly? Because the current behavior is inconsistent.

@Marcono1234 Marcono1234 deleted the marcono1234/JsonReader-nan-infinity-exception-type branch September 13, 2020 14:13
Marcono1234 added a commit to Marcono1234/gson that referenced this pull request Sep 13, 2020
eamonnmcmanus pushed a commit that referenced this pull request Aug 8, 2022
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.

2 participants