Skip to content
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

Deeper Numerical Value Comparison Support #138

Open
wants to merge 2 commits 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
8 changes: 8 additions & 0 deletions src/main/java/com/flipkart/zjsonpatch/JsonDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.NumericNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.collections4.ListUtils;

Expand Down Expand Up @@ -362,6 +363,9 @@ private void generateDiffs(JsonPointer path, JsonNode source, JsonNode target) {
//both are json
compareObjects(path, source, target);
} else {
if (isSameNumericValue(source, target, sourceType, targetType)) {
return;
}
//can be replaced
if (flags.contains(DiffFlags.EMIT_TEST_OPERATIONS))
diffs.add(new Diff(Operation.TEST, path, source));
Expand All @@ -370,6 +374,10 @@ private void generateDiffs(JsonPointer path, JsonNode source, JsonNode target) {
}
}

private boolean isSameNumericValue(JsonNode source, JsonNode target, NodeType sourceType, NodeType targetType) {
return source instanceof NumericNode && target instanceof NumericNode && source.asDouble() == target.asDouble();
}

private void compareArray(JsonPointer path, JsonNode source, JsonNode target) {
List<JsonNode> lcs = getLCS(source, target);
int srcIdx = 0;
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/com/flipkart/zjsonpatch/JsonDiffTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.EnumSet;
Expand Down Expand Up @@ -156,4 +157,35 @@ public void testJsonDiffShowsDiffWhenTargetNodeIsNullWithFlags() throws JsonProc
assertEquals(JsonPointer.ROOT.toString(), diff.get(0).get("path").textValue());
assertEquals("V1", diff.get(0).get("value").get("K1").textValue());
}

@Test
public void testJsonDiffWithSameNumericValueAfterSerialization() throws IOException {
ObjectNode numeric = objectMapper.createObjectNode()
.put("integer", Long.MAX_VALUE)
.put("number", Double.MAX_VALUE)
.put("decimal", 9223372036854775807D)
.put("long", Long.MAX_VALUE);
File numericFile = new File("target/numeric.json");
objectMapper.writeValue(numericFile, numeric);
ObjectNode deserialized = (ObjectNode) objectMapper.readTree(numericFile);
deserialized.put("decimal", new Double(Long.MAX_VALUE));
deserialized.put("long", 9223372036854775807D);
JsonNode diff = JsonDiff.asJson(numeric, deserialized);
assertEquals(0, diff.size());
}


@Test
public void testJsonDiffWithSameNumericValueAfterSerializationButDifferentType() throws IOException {
ObjectNode numeric = objectMapper.createObjectNode()
.put("integer", 1L)
.put("number", 1.0);
File numericFile = new File("target/numeric.json");
objectMapper.writeValue(numericFile, numeric);
ObjectNode deserialized = (ObjectNode) objectMapper.readTree(numericFile);
deserialized.put("integer", "1");
deserialized.put("number", "1.0");
JsonNode diff = JsonDiff.asJson(numeric, deserialized);
assertEquals(2, diff.size());
}
}