fix: Include location info in NumberFormatException from JsonReader#3000
Open
daguimu wants to merge 1 commit intogoogle:mainfrom
Open
fix: Include location info in NumberFormatException from JsonReader#3000daguimu wants to merge 1 commit intogoogle:mainfrom
daguimu wants to merge 1 commit intogoogle:mainfrom
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
JsonReader.nextDouble(),nextInt(), andnextLong()callDouble.parseDouble()on peeked strings. When parsing fails (e.g., for empty strings like""), theNumberFormatExceptionthrown byDouble.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 producesNumberFormatException: empty Stringwith 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 fromDouble.parseDouble()propagate directly. OtherNumberFormatExceptionthrows in the same methods already include location information vialocationString().Fix
Catch the
NumberFormatExceptionfromDouble.parseDouble()and rethrow with a descriptive message that includes the location string, consistent with other error messages inJsonReader. 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$.xtestNextLongNumberFormatExceptionContainsLocation— verifies location in path$.yImpact
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