Skip to content

Commit bf8acf8

Browse files
committed
JsonNumEquals: fix equivalence for integer nodes
1 parent 549007e commit bf8acf8

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

Diff for: src/main/java/com/github/fge/jackson/JsonNumEquals.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,12 @@ protected int doHash(final JsonNode t)
167167
private static boolean numEquals(final JsonNode a, final JsonNode b)
168168
{
169169
/*
170-
* If both numbers are integers, delegate to JsonNode.
170+
* If both numbers are integers, compare integer values
171171
*/
172172
if (a.isIntegralNumber() && b.isIntegralNumber())
173-
return a.equals(b);
173+
return (a.canConvertToLong() && b.canConvertToLong())
174+
? a.longValue() == b.longValue()
175+
: a.bigIntegerValue().equals(b.bigIntegerValue());
174176

175177
/*
176178
* Otherwise, compare decimal values.

Diff for: src/test/java/com/github/fge/jackson/JsonNumEqualsTest.java

+18
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@
2121

2222
import com.fasterxml.jackson.databind.JsonNode;
2323
import com.fasterxml.jackson.databind.node.ArrayNode;
24+
import com.fasterxml.jackson.databind.node.BigIntegerNode;
25+
import com.fasterxml.jackson.databind.node.IntNode;
2426
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
27+
import com.fasterxml.jackson.databind.node.LongNode;
2528
import com.fasterxml.jackson.databind.node.ObjectNode;
29+
import com.fasterxml.jackson.databind.node.ShortNode;
2630
import org.testng.annotations.BeforeClass;
2731
import org.testng.annotations.DataProvider;
2832
import org.testng.annotations.Test;
2933

3034
import java.io.IOException;
35+
import java.math.BigInteger;
3136
import java.util.ArrayList;
3237
import java.util.Iterator;
3338
import java.util.List;
@@ -59,6 +64,19 @@ public Iterator<Object[]> getInputs()
5964
list.add(new Object[]{reference, node});
6065
}
6166

67+
list.add(new Object[]{new ShortNode((short) 1), new IntNode(1)});
68+
list.add(new Object[]{new ShortNode((short) 1), new LongNode(1)});
69+
list.add(new Object[]{new ShortNode((short) 1), new BigIntegerNode(BigInteger.ONE)});
70+
list.add(new Object[]{new IntNode(1), new ShortNode((short) 1)});
71+
list.add(new Object[]{new IntNode(1), new LongNode(1)});
72+
list.add(new Object[]{new IntNode(1), new BigIntegerNode(BigInteger.ONE)});
73+
list.add(new Object[]{new LongNode(1), new ShortNode((short) 1)});
74+
list.add(new Object[]{new LongNode(1), new IntNode(1)});
75+
list.add(new Object[]{new LongNode(1), new BigIntegerNode(BigInteger.ONE)});
76+
list.add(new Object[]{new BigIntegerNode(BigInteger.ONE), new ShortNode((short) 1)});
77+
list.add(new Object[]{new BigIntegerNode(BigInteger.ONE), new IntNode(1)});
78+
list.add(new Object[]{new BigIntegerNode(BigInteger.ONE), new LongNode(1)});
79+
6280
return list.iterator();
6381
}
6482

0 commit comments

Comments
 (0)