Fix the equals method of JsonPrimitive to work with BigInteger#2311
Merged
eamonnmcmanus merged 2 commits intogoogle:masterfrom Feb 6, 2023
Merged
Fix the equals method of JsonPrimitive to work with BigInteger#2311eamonnmcmanus merged 2 commits intogoogle:masterfrom
equals method of JsonPrimitive to work with BigInteger#2311eamonnmcmanus merged 2 commits intogoogle:masterfrom
Conversation
equals method of JsonPrimitive to work with BigIntegerequals method of JsonPrimitive to work withBigInteger
equals method of JsonPrimitive to work withBigIntegerequals method of JsonPrimitive to work with BigInteger
Contributor
Author
|
We could also create a method called like: public String getNumberAsString() {
return getAsNumber().toString();
}But, I don't know if it's worth |
eamonnmcmanus
requested changes
Feb 6, 2023
Member
eamonnmcmanus
left a comment
There was a problem hiding this comment.
Thanks for taking this on!
| public void testEqualsIntegerAndBigInteger() { | ||
| JsonPrimitive a = new JsonPrimitive(5L); | ||
| JsonPrimitive b = new JsonPrimitive(new BigInteger("18446744073709551621")); // 2^64 + 5 | ||
| // Ideally, the following assertion should have failed but the price is too much to pay |
Member
There was a problem hiding this comment.
FWIW I vigorously disagree with this assessment. Saying values are equal when they aren't is pretty bad, and the fix is neither difficult nor expensive.
| // assertFalse(a + " equals " + b, a.equals(b)); | ||
| assertWithMessage("%s equals %s", a, b).that(a.equals(b)).isTrue(); | ||
| JsonPrimitive b = new JsonPrimitive(new BigInteger("18446744073709551621")); | ||
| assertWithMessage("%s not equals %s", a, b).that(a.equals(b)).isFalse(); |
Member
There was a problem hiding this comment.
This may become irrelevant if we start using EqualsTester, but for now could you also check that b.equals(a) is false?
Contributor
Author
There was a problem hiding this comment.
We could leave it until we implement EqualsTester
eamonnmcmanus
approved these changes
Feb 6, 2023
4 tasks
Closed
tibor-universe
pushed a commit
to getuniverse/gson
that referenced
this pull request
Sep 14, 2024
…oogle#2311) * Fix the `equals` method of `JsonPrimitive` to work with BigInteger * Improve the `equals` & `getAsBigInteger` methods in `JsonPrimitive`
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.
This PR should fix the issue #2144.
The
getAsNumber().longValue()caused an overflow because theBigIntegerwas converted to along.The idea here is: create a
BigInteger(that's the biggest int we could rappresent) using the string value and then compare it.