return BigInteger for json integers beyond long range#2619
Open
netliomax25-code wants to merge 1 commit into
Open
return BigInteger for json integers beyond long range#2619netliomax25-code wants to merge 1 commit into
netliomax25-code wants to merge 1 commit into
Conversation
✅ All tests passed ✅Test SummaryBuild and test / lts (17, windows-latest, 1) > :test
🏷️ Commit: 71ffc3b Learn more about TestLens at testlens.app. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR aligns JsonSlurper’s integer type-selection behavior across its parser implementations so that JSON integers exceeding the long range are returned as BigInteger (instead of overflowing/wrapping or returning null), matching the existing JsonToken contract.
Changes:
- Extend integer decoding in the CHAR_BUFFER parser (
CharScanner.parseJsonNumber) to promote beyond-longintegers toBigInteger. - Extend integer decoding in overlay parsers (
NumberValue.doToValue) to promote beyond-longintegers toBigInteger. - Extend integer decoding in the CHARACTER_SOURCE parser (
JsonParserUsingCharacterSource.decodeNumber) to returnBigIntegerfor beyond-longintegers, and add a regression test.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| subprojects/groovy-json/src/test/groovy/groovy/json/JsonSlurperTest.groovy | Adds a regression test for parsing an integer beyond Long.MAX_VALUE (note: as written, it only exercises the default parser type). |
| subprojects/groovy-json/src/main/java/org/apache/groovy/json/internal/NumberValue.java | Updates overlay-backed integer conversion to select BigInteger when the value doesn’t fit in int or long. |
| subprojects/groovy-json/src/main/java/org/apache/groovy/json/internal/JsonParserUsingCharacterSource.java | Updates character-source number decoding to construct a BigInteger when the value doesn’t fit in int or long. |
| subprojects/groovy-json/src/main/java/org/apache/groovy/json/internal/CharScanner.java | Updates CHAR_BUFFER number parsing to construct a BigInteger for integer tokens that exceed long range. |
Comment on lines
+744
to
+752
| @Test | ||
| void testParseIntegerBeyondLongRange() { | ||
| // 9999999999999999999 exceeds Long.MAX_VALUE; it must round-trip as a | ||
| // BigInteger rather than silently overflowing to a wrong (negative) long. | ||
| def raw = parser.parseText('9999999999999999999') | ||
| def value = raw instanceof Value ? raw.toValue() : raw | ||
| assertTrue(value instanceof BigInteger) | ||
| assertEquals(new BigInteger('9999999999999999999'), value) | ||
| } |
Contributor
|
Thanks again for another contribution. Did you want to create a Jira and address the Copilot comment? |
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.
Added the missing BigInteger branch at the three decode sites so every parser type matches the JsonToken contract. The new regression test runs across all four parser types and fails before the change.