-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Fix the equals method of JsonPrimitive to work with BigInteger
#2311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix the equals method of JsonPrimitive to work with BigInteger
#2311
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
|
We could also create a method called like: public String getNumberAsString() {
return getAsNumber().toString();
}But, I don't know if it's worth |
eamonnmcmanus
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may become irrelevant if we start using EqualsTester, but for now could you also check that b.equals(a) is false?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could leave it until we implement EqualsTester
…oogle#2311) * Fix the `equals` method of `JsonPrimitive` to work with BigInteger * Improve the `equals` & `getAsBigInteger` methods in `JsonPrimitive`
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.