Skip to content

Commit

Permalink
Merge pull request #2 from rob-glas/feature/compare-all-values-as-big…
Browse files Browse the repository at this point in the history
…-decimal

Feature/compare all values as big decimal
  • Loading branch information
DamianChlodGD authored Jun 1, 2021
2 parents 9a71df6 + 6cb2709 commit 576a882
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/flipkart/zjsonpatch/DiffFlags.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ public enum DiffFlags {
* as sets. This means that the order does not matter and the object is considered updated only if
* there is another object with the same "id", "_id" or "uuid".
*/
TREAT_ARRAYS_AS_SETS;
TREAT_ARRAYS_AS_SETS,

COMPARE_ALL_NUMBERS_AS_BIG_DECIMAL;

public static EnumSet<DiffFlags> defaults() {
return EnumSet.of(OMIT_VALUE_ON_REMOVE);
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/com/flipkart/zjsonpatch/JsonDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.collections4.ListUtils;

import java.math.BigDecimal;
import java.util.*;

/**
Expand Down Expand Up @@ -356,9 +357,19 @@ private static ObjectNode getJsonNode(JsonNodeFactory FACTORY, Diff diff, EnumSe

private void generateDiffs(JsonPointer path, JsonNode source, JsonNode target) {
if (!source.equals(target)) {

final NodeType sourceType = NodeType.getNodeType(source);
final NodeType targetType = NodeType.getNodeType(target);

if (flags.contains(DiffFlags.COMPARE_ALL_NUMBERS_AS_BIG_DECIMAL) &&
sourceType == NodeType.NUMBER && targetType == NodeType.NUMBER){
BigDecimal sourceVal = BigDecimal.valueOf(source.asDouble());
BigDecimal targetVal = BigDecimal.valueOf(target.asDouble());
if (sourceVal.equals(targetVal)) {
return;
}
}

if (sourceType == NodeType.ARRAY && targetType == NodeType.ARRAY) {
//both are arrays

Expand Down Expand Up @@ -486,7 +497,7 @@ private void compareObjects(JsonPointer path, JsonNode source, JsonNode target)
if (flags.contains(DiffFlags.TREAT_ARRAYS_AS_SETS) && sourceIndexById.containsKey(key)) {
JsonPointer currPath = path.append(sourceIndexById.get(key));
sourceIndexById.remove(key);
if (flags.contains(DiffFlags.TREAT_ARRAYS_AS_SETS) && flags.contains(DiffFlags.EMIT_TEST_OPERATIONS))
if (flags.contains(DiffFlags.EMIT_TEST_OPERATIONS))
diffs.add(new Diff(Operation.TEST, currPath, source.get(key)));
diffs.add(Diff.generateDiff(Operation.REMOVE, currPath, source.get(key)));
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/flipkart/zjsonpatch/NodeType.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ enum NodeType {
TOKEN_MAP.put(JsonToken.START_ARRAY, ARRAY);
TOKEN_MAP.put(JsonToken.VALUE_TRUE, BOOLEAN);
TOKEN_MAP.put(JsonToken.VALUE_FALSE, BOOLEAN);
TOKEN_MAP.put(JsonToken.VALUE_NUMBER_INT, INTEGER);
TOKEN_MAP.put(JsonToken.VALUE_NUMBER_INT, NUMBER);
TOKEN_MAP.put(JsonToken.VALUE_NUMBER_FLOAT, NUMBER);
TOKEN_MAP.put(JsonToken.VALUE_NULL, NULL);
TOKEN_MAP.put(JsonToken.START_OBJECT, OBJECT);
Expand Down
14 changes: 12 additions & 2 deletions src/test/java/com/flipkart/zjsonpatch/JsonDiffTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,12 @@ public void testJsonDiffWithArrayOfObjectWithId() throws Exception {
JsonNode sourceNode = objectMapper.readTree(source);
JsonNode targetNode = objectMapper.readTree(target);

EnumSet<DiffFlags> flags = EnumSet.of(DiffFlags.ADD_ORIGINAL_VALUE_ON_REPLACE_AS_VALUE, DiffFlags.OMIT_MOVE_OPERATION, DiffFlags.OMIT_COPY_OPERATION, DiffFlags.TREAT_ARRAYS_AS_SETS);
EnumSet<DiffFlags> flags = EnumSet.of(
DiffFlags.ADD_ORIGINAL_VALUE_ON_REPLACE_AS_VALUE,
DiffFlags.OMIT_MOVE_OPERATION,
DiffFlags.OMIT_COPY_OPERATION,
DiffFlags.TREAT_ARRAYS_AS_SETS,
DiffFlags.COMPARE_ALL_NUMBERS_AS_BIG_DECIMAL);
JsonNode patchNode = JsonDiff.asJson(sourceNode, targetNode, flags);

String diff = patchNode.toString();
Expand Down Expand Up @@ -252,7 +257,12 @@ public static Object[][] provideVariousDocuments() {
"{\"_id\":\"_id\",\"fields\":[{\"dataFormat\":\"quantity\",\"name\":\"soundPeak3\",\"uuid\":\"6d4aa503-4d7f-4373-a563-417edd8681f3\"},{\"dataFormat\":\"quantity\",\"name\":\"soundPeak\",\"uuid\":\"6d4aa503-4d7f-4373-a563-417edd8681f1\"},{\"dataFormat\":\"quantity\",\"name\":\"soundPeak2\",\"uuid\":\"6d4aa503-4d7f-4373-a563-417edd8681f2\"},{\"dataFormat\":\"quantity\",\"name\":\"soundPeak7\",\"uuid\":\"6d4aa503-4d7f-4373-a563-417edd8681f7\"}]}",
"{\"_id\":\"_id\",\"fields\":[{\"dataFormat\":\"quantity\",\"name\":\"soundPeak2\",\"uuid\":\"6d4aa503-4d7f-4373-a563-417edd8681f2\"},{\"dataFormat\":\"quantity\",\"name\":\"soundPeak\",\"uuid\":\"6d4aa503-4d7f-4373-a563-417edd8681f1\"},{\"dataFormat\":\"quantity\",\"name\":\"soundPeak8\",\"uuid\":\"6d4aa503-4d7f-4373-a563-417edd8681f3\"},{\"dataFormat\":\"quantity\",\"name\":\"soundPeak20\",\"uuid\":\"6d4aa503-4d7f-4373-a563-417edd8681f20\"}]}",
"[{\"op\":\"remove\",\"path\":\"/fields/3\",\"value\":{\"dataFormat\":\"quantity\",\"name\":\"soundPeak7\",\"uuid\":\"6d4aa503-4d7f-4373-a563-417edd8681f7\"}},{\"op\":\"replace\",\"value\":\"soundPeak3\",\"path\":\"/fields/0/name\"},{\"op\":\"add\",\"path\":\"/fields/3\",\"value\":{\"dataFormat\":\"quantity\",\"name\":\"soundPeak20\",\"uuid\":\"6d4aa503-4d7f-4373-a563-417edd8681f20\"}}]"
}
},
{
"{\"values\":{\"344aa235-4d7f-4373-a563-417edd8685d2\":20.0,\"6d4aa503-4d7f-4373-a563-417edd8681f1\":65.4215557891}}",
"{\"values\":{\"6d4aa503-4d7f-4373-a563-417edd8681f1\":65.4215557891,\"344aa235-4d7f-4373-a563-417edd8685d2\":20}}",
"[]"
},
};
}
}

0 comments on commit 576a882

Please sign in to comment.