diff --git a/src/main/java/com/flipkart/zjsonpatch/JsonDiff.java b/src/main/java/com/flipkart/zjsonpatch/JsonDiff.java index effc963..c8908c3 100644 --- a/src/main/java/com/flipkart/zjsonpatch/JsonDiff.java +++ b/src/main/java/com/flipkart/zjsonpatch/JsonDiff.java @@ -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; @@ -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)); @@ -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 lcs = getLCS(source, target); int srcIdx = 0; diff --git a/src/test/java/com/flipkart/zjsonpatch/JsonDiffTest.java b/src/test/java/com/flipkart/zjsonpatch/JsonDiffTest.java index 41e7b6b..d98ac45 100644 --- a/src/test/java/com/flipkart/zjsonpatch/JsonDiffTest.java +++ b/src/test/java/com/flipkart/zjsonpatch/JsonDiffTest.java @@ -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; @@ -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()); + } }