Skip to content

Commit e185296

Browse files
committed
Fix for issues google#904 and google#2144
1 parent cbc0af8 commit e185296

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

gson/src/main/java/com/google/gson/JsonPrimitive.java

+18
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,12 @@ public boolean equals(Object obj) {
267267
if (value == null) {
268268
return other.value == null;
269269
}
270+
if (isBigNumber(this) || isBigNumber(other)) {
271+
// As a BigDecimal is essentially a wrapper around a BigInteger that
272+
// "remembers where the decimal point is", casting BigInteger to BigDecimal
273+
// makes more sense, avoiding max long/double values comparison.
274+
return getAsBigDecimal().equals(other.getAsBigDecimal());
275+
}
270276
if (isIntegral(this) && isIntegral(other)) {
271277
return getAsNumber().longValue() == other.getAsNumber().longValue();
272278
}
@@ -292,4 +298,16 @@ private static boolean isIntegral(JsonPrimitive primitive) {
292298
}
293299
return false;
294300
}
301+
302+
private static boolean isBigInteger(JsonPrimitive primitive) {
303+
return primitive.value instanceof BigDecimal;
304+
}
305+
306+
private static boolean isBigDecimal(JsonPrimitive primitive) {
307+
return primitive.value instanceof BigDecimal;
308+
}
309+
310+
private static boolean isBigNumber(JsonPrimitive primitive) {
311+
return isBigDecimal(primitive) || isBigInteger(primitive);
312+
}
295313
}

0 commit comments

Comments
 (0)