Skip to content

Commit

Permalink
Implements BigDecimal check in the JsonPrimitive.equals()
Browse files Browse the repository at this point in the history
  • Loading branch information
MaicolAntali committed Oct 29, 2023
1 parent f91c2dc commit 98bec86
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions gson/src/main/java/com/google/gson/JsonPrimitive.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,15 @@ public boolean equals(Object obj) {
: this.getAsNumber().longValue() == other.getAsNumber().longValue();
}
if (value instanceof Number && other.value instanceof Number) {
if (value instanceof BigDecimal && other.value instanceof BigDecimal) {
// Uses compareTo to ignore scale of values, e.g. `0` and `0.00` should be considered equal
return this.getAsBigDecimal().compareTo(other.getAsBigDecimal()) == 0;
}

double thisAsDouble = this.getAsDouble();
double otherAsDouble = other.getAsDouble();

return (this.value instanceof BigDecimal && other.value instanceof BigDecimal)
// Uses compareTo to ignore scale of values, e.g. `0` and `0.00` should be considered equal
? this.getAsBigDecimal().compareTo(other.getAsBigDecimal()) == 0
: thisAsDouble == otherAsDouble
|| (Double.isNaN(thisAsDouble) && Double.isNaN(otherAsDouble));
// Don't use Double.compare(double, double) because that considers -0.0 and +0.0 not equal
return (thisAsDouble == otherAsDouble) || (Double.isNaN(thisAsDouble) && Double.isNaN(otherAsDouble));
}
return value.equals(other.value);
}
Expand Down

0 comments on commit 98bec86

Please sign in to comment.