From 2f0c617d8df2ad38439c43c06b7c066ba646a627 Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Fri, 9 Sep 2011 08:01:51 +0000 Subject: [PATCH] Use floating point comparison for all non-integral Number types (such as LazilyParsedNumber) --- .../main/java/com/google/gson/JsonPrimitive.java | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/gson/src/main/java/com/google/gson/JsonPrimitive.java b/gson/src/main/java/com/google/gson/JsonPrimitive.java index 32ff6502..58040635 100644 --- a/gson/src/main/java/com/google/gson/JsonPrimitive.java +++ b/gson/src/main/java/com/google/gson/JsonPrimitive.java @@ -318,7 +318,7 @@ public final class JsonPrimitive extends JsonElement { long value = getAsNumber().longValue(); return (int) (value ^ (value >>> 32)); } - if (isFloatingPoint(this)) { + if (value instanceof Number) { long value = Double.doubleToLongBits(getAsNumber().doubleValue()); return (int) (value ^ (value >>> 32)); } @@ -340,7 +340,7 @@ public final class JsonPrimitive extends JsonElement { if (isIntegral(this) && isIntegral(other)) { return getAsNumber().longValue() == other.getAsNumber().longValue(); } - if (isFloatingPoint(this) && isFloatingPoint(other)) { + if (value instanceof Number && other.value instanceof Number) { double a = getAsNumber().doubleValue(); // Java standard types other than double return true for two NaN. So, need // special handling for double. @@ -362,15 +362,4 @@ public final class JsonPrimitive extends JsonElement { } return false; } - - /** - * Returns true if the specified number is a floating point type (BigDecimal, double, float) - */ - private static boolean isFloatingPoint(JsonPrimitive primitive) { - if (primitive.value instanceof Number) { - Number number = (Number) primitive.value; - return number instanceof BigDecimal || number instanceof Double || number instanceof Float; - } - return false; - } }