Skip to content

Include location in NumberFormatException thrown by JsonTreeReader - #3062

Merged
eamonnmcmanus merged 2 commits into
google:mainfrom
dong0713:fix/jsontreereader-nfe-location
Jul 28, 2026
Merged

Include location in NumberFormatException thrown by JsonTreeReader#3062
eamonnmcmanus merged 2 commits into
google:mainfrom
dong0713:fix/jsontreereader-nfe-location

Conversation

@dong0713

Copy link
Copy Markdown
Contributor

Problem

JsonReader.nextDouble(), nextLong() and nextInt() already include the JSON path in the NumberFormatException thrown when a value cannot be parsed. This was added in #3000 as a fix for #1564.

However, the corresponding methods in JsonTreeReader were missing this handling. As a result, the two public deserialization entry points behave inconsistently:

class Model { int x; }

// Streaming path — already includes location (since #3000)
new Gson().fromJson("{\"x\":\"\"}", Model.class);
// -> JsonSyntaxException: ... Expected an int but was  at line 1 column 8 path $.x

// Tree path — no location information
JsonObject obj = new JsonObject();
obj.add("x", new JsonPrimitive(""));
new Gson().fromJson(obj, Model.class);
// -> JsonSyntaxException: ... NumberFormatException: For input string: ""

Both paths consume untrusted JSON and report parse failures to the caller, so they should report the same location information.

Fix

Mirror the existing JsonReader handling: wrap the JsonPrimitive conversion in nextDouble(), nextLong() and nextInt() with a try-catch, and rethrow a NumberFormatException containing the offending value and locationString(), preserving the original exception as the cause (via initCause(e)), exactly as the streaming JsonReader does.

Tests

Added three regression tests in JsonTreeReaderTest (testNextDouble/NextInt/NextLongNumberFormatExceptionContainsLocation), asserting the message contains the JSON path (path $.x / path $.y), paralleling the existing tests added for JsonReader in JsonReaderTest.

Full test suite: Tests run: 4607, Failures: 0, Errors: 0, Skipped: 20 — no regressions.

Notes

  • This is the tree-path counterpart of fix: Include location info in NumberFormatException from JsonReader #3000; JsonTreeReader overrides the same JsonReader methods, so the omission is a direct symmetric gap.
  • The outer Gson.fromJson only catches EOFException/IllegalStateException/IOException, so the bare NumberFormatException from the tree path reaches the caller unwrapped, making the missing location directly user-visible.

@google-cla

google-cla Bot commented Jul 21, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@eamonnmcmanus eamonnmcmanus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for helping improve consistency here!

try {
result = primitive.getAsDouble();
} catch (NumberFormatException e) {
NumberFormatException rethrown =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's annoying that NumberFormatException doesn't have a constructor with a cause parameter!

Given that the code pattern here occurs three times, I think it would make sense to introduce a helper method, so you could write this:

throw numberFormatException("Expected a double " ..., e);

@dong0713

Copy link
Copy Markdown
Contributor Author

Done — introduced a numberFormatException helper as suggested. Thanks!

@dong0713
dong0713 force-pushed the fix/jsontreereader-nfe-location branch 2 times, most recently from 289b4a8 to 1be033b Compare July 25, 2026 09:33
@dong0713
dong0713 force-pushed the fix/jsontreereader-nfe-location branch from 8974bff to 6cce44d Compare July 25, 2026 09:47
Signed-off-by: Bowang <417851790@qq.com>
@dong0713
dong0713 requested a review from eamonnmcmanus July 28, 2026 01:23

@eamonnmcmanus eamonnmcmanus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks again!

@eamonnmcmanus
eamonnmcmanus merged commit 138377c into google:main Jul 28, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants