diff --git a/src/main/java/com/github/fge/jackson/JsonNumEquals.java b/src/main/java/com/github/fge/jackson/JsonNumEquals.java index 2bb4bc7..e7de4e6 100644 --- a/src/main/java/com/github/fge/jackson/JsonNumEquals.java +++ b/src/main/java/com/github/fge/jackson/JsonNumEquals.java @@ -167,10 +167,12 @@ protected int doHash(final JsonNode t) private static boolean numEquals(final JsonNode a, final JsonNode b) { /* - * If both numbers are integers, delegate to JsonNode. + * If both numbers are integers, compare integer values */ if (a.isIntegralNumber() && b.isIntegralNumber()) - return a.equals(b); + return (a.canConvertToLong() && b.canConvertToLong()) + ? a.longValue() == b.longValue() + : a.bigIntegerValue().equals(b.bigIntegerValue()); /* * Otherwise, compare decimal values. diff --git a/src/test/java/com/github/fge/jackson/JsonNumEqualsTest.java b/src/test/java/com/github/fge/jackson/JsonNumEqualsTest.java index af4d068..66c48ba 100644 --- a/src/test/java/com/github/fge/jackson/JsonNumEqualsTest.java +++ b/src/test/java/com/github/fge/jackson/JsonNumEqualsTest.java @@ -21,13 +21,18 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.BigIntegerNode; +import com.fasterxml.jackson.databind.node.IntNode; import com.fasterxml.jackson.databind.node.JsonNodeFactory; +import com.fasterxml.jackson.databind.node.LongNode; import com.fasterxml.jackson.databind.node.ObjectNode; +import com.fasterxml.jackson.databind.node.ShortNode; import org.testng.annotations.BeforeClass; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import java.io.IOException; +import java.math.BigInteger; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -59,6 +64,19 @@ public Iterator getInputs() list.add(new Object[]{reference, node}); } + list.add(new Object[]{new ShortNode((short) 1), new IntNode(1)}); + list.add(new Object[]{new ShortNode((short) 1), new LongNode(1)}); + list.add(new Object[]{new ShortNode((short) 1), new BigIntegerNode(BigInteger.ONE)}); + list.add(new Object[]{new IntNode(1), new ShortNode((short) 1)}); + list.add(new Object[]{new IntNode(1), new LongNode(1)}); + list.add(new Object[]{new IntNode(1), new BigIntegerNode(BigInteger.ONE)}); + list.add(new Object[]{new LongNode(1), new ShortNode((short) 1)}); + list.add(new Object[]{new LongNode(1), new IntNode(1)}); + list.add(new Object[]{new LongNode(1), new BigIntegerNode(BigInteger.ONE)}); + list.add(new Object[]{new BigIntegerNode(BigInteger.ONE), new ShortNode((short) 1)}); + list.add(new Object[]{new BigIntegerNode(BigInteger.ONE), new IntNode(1)}); + list.add(new Object[]{new BigIntegerNode(BigInteger.ONE), new LongNode(1)}); + return list.iterator(); }