Skip to content

fix: Include location info in NumberFormatException from JsonReader#3000

Merged
eamonnmcmanus merged 5 commits intogoogle:mainfrom
daguimu:fix/numberformatexception-location-info-issue1564
Apr 23, 2026
Merged

fix: Include location info in NumberFormatException from JsonReader#3000
eamonnmcmanus merged 5 commits intogoogle:mainfrom
daguimu:fix/numberformatexception-location-info-issue1564

Conversation

@daguimu
Copy link
Copy Markdown
Contributor

@daguimu daguimu commented Mar 26, 2026

Problem

JsonReader.nextDouble(), nextInt(), and nextLong() call Double.parseDouble() on peeked strings. When parsing fails (e.g., for empty strings like ""), the NumberFormatException thrown by Double.parseDouble() contains no JSON location information (line, column, path). This makes it difficult to locate the problematic value in the input JSON.

For example, parsing { x: ''} as an int produces NumberFormatException: empty String with no indication of where in the JSON the error occurred.

Root Cause

The three methods had a comment // don't catch this NumberFormatException. and let the exception from Double.parseDouble() propagate directly. Other NumberFormatException throws in the same methods already include location information via locationString().

Fix

Catch the NumberFormatException from Double.parseDouble() and rethrow with a descriptive message that includes the location string, consistent with other error messages in JsonReader. The message format follows the existing pattern: "Expected a <type> but was <value> at line X column Y path $.<path>".

Tests Added

  • testNextDoubleNumberFormatExceptionContainsLocation — verifies location in path $[0]
  • testNextIntNumberFormatExceptionContainsLocation — verifies location in path $.x
  • testNextLongNumberFormatExceptionContainsLocation — verifies location in path $.y

Impact

Only affects the error path when Double.parseDouble() fails on an invalid numeric string. The exception type (NumberFormatException) is unchanged. All 4589 existing tests pass.

Fixes #1564

daguimu and others added 5 commits March 26, 2026 10:02
JsonReader.nextDouble(), nextInt(), and nextLong() call
Double.parseDouble() on peeked strings, but the NumberFormatException
thrown by parseDouble() contains no JSON location information (line,
column, path), making it difficult to locate the problematic value in
the input.

Catch the NumberFormatException and rethrow with a message that
includes the location string, consistent with other error messages
in JsonReader.

Fixes google#1564
Address the ErrorProne UnusedException warning (-Werror on JDK 25 /
Build Gson subset CI jobs) introduced when the three number-parsing
catch blocks in JsonReader were added: set the caught exception as
the cause of the rethrown one so debuggers still see the original
parse failure.
Copy link
Copy Markdown
Member

@eamonnmcmanus eamonnmcmanus left a comment

Choose a reason for hiding this comment

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

Thanks, I think this will be helpful for users!

@eamonnmcmanus eamonnmcmanus merged commit 6bf8bf6 into google:main Apr 23, 2026
16 checks passed
@daguimu daguimu deleted the fix/numberformatexception-location-info-issue1564 branch April 24, 2026 01:57
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.

JsonReader throws NumberFormatException which does not contain location info

2 participants