Skip to content

JsonNumEquals: fix equivalence for integer nodes #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/main/java/com/github/fge/jackson/JsonNumEquals.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/com/github/fge/jackson/JsonNumEqualsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,6 +64,19 @@ public Iterator<Object[]> 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();
}

Expand Down