Skip to content
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

Merged
merged 2 commits into from
Feb 6, 2023

Conversation

MaicolAntali
Copy link
Contributor

@MaicolAntali MaicolAntali commented Feb 6, 2023

This PR should fix the issue #2144.

The getAsNumber().longValue() caused an overflow because the BigInteger was converted to a long.

The idea here is: create a BigInteger (that's the biggest int we could rappresent) using the string value and then compare it.

@MaicolAntali MaicolAntali changed the title Fix the equals method of JsonPrimitive to work with BigInteger Fix the equals method of JsonPrimitive to work withBigInteger Feb 6, 2023
@MaicolAntali MaicolAntali changed the title Fix the equals method of JsonPrimitive to work withBigInteger Fix the equals method of JsonPrimitive to work with BigInteger Feb 6, 2023
@MaicolAntali
Copy link
Contributor Author

We could also create a method called like: getNumberAsString. It should be:

public String getNumberAsString() {
    return getAsNumber().toString();
}

But, I don't know if it's worth

Copy link
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 for taking this on!

gson/src/main/java/com/google/gson/JsonPrimitive.java Outdated Show resolved Hide resolved
@@ -300,10 +300,8 @@ public void testEqualsAcrossTypes() {
@Test
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
Copy link
Member

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();
Copy link
Member

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?

Copy link
Contributor Author

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

@eamonnmcmanus eamonnmcmanus merged commit af21798 into google:master Feb 6, 2023
@MaicolAntali MaicolAntali deleted the JsonPrimitive-equal-fix branch February 10, 2023 14:04
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`
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.

2 participants