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 for issues #904 and #2144 #2145

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions gson/src/main/java/com/google/gson/JsonPrimitive.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ public boolean equals(Object obj) {
if (value == null) {
return other.value == null;
}
if (isBigNumber(this) || isBigNumber(other)) {
// As a BigDecimal is essentially a wrapper around a BigInteger that
// "remembers where the decimal point is", casting BigInteger to BigDecimal
// makes more sense, avoiding max long/double values comparison.
return getAsBigDecimal().equals(other.getAsBigDecimal());
}
if (isIntegral(this) && isIntegral(other)) {
return getAsNumber().longValue() == other.getAsNumber().longValue();
}
Expand All @@ -292,4 +298,16 @@ private static boolean isIntegral(JsonPrimitive primitive) {
}
return false;
}

private static boolean isBigInteger(JsonPrimitive primitive) {
return primitive.value instanceof BigDecimal;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be BigInteger? Which in turn makes me think that there should be a test that would have caught this.

Speaking of tests, in general if you are proposing a fix for an issue, you should include a test that fails without the fix and passes with it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eamonnmcmanus
Yes that should be a BigInteger, as for the testing part, thank you for the tip, I will make sure to provide tests with any future commits.

}

private static boolean isBigDecimal(JsonPrimitive primitive) {
return primitive.value instanceof BigDecimal;
}

private static boolean isBigNumber(JsonPrimitive primitive) {
return isBigDecimal(primitive) || isBigInteger(primitive);
}
}