-
Notifications
You must be signed in to change notification settings - Fork 158
Add smart equals for arrays #214
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
HerrDerb
wants to merge
2
commits into
flipkart-incubator:master
Choose a base branch
from
HerrDerb:smart-equals
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+312
−14
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/com/flipkart/zjsonpatch/JsonNodeEqualsFunction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.flipkart.zjsonpatch; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
|
||
/** | ||
* Custom equality function for JsonNode objects. | ||
* Allows clients to define custom equality semantics for JSON node comparison. | ||
*/ | ||
@FunctionalInterface | ||
public interface JsonNodeEqualsFunction { | ||
JsonNodeEqualsFunction REF_IDENTITY = (jsonNode1, jsonNode2) -> jsonNode1.equals(jsonNode2); | ||
/** | ||
* Compares two JsonNode objects for equality based on custom logic. | ||
* @param jsonNode1 the first JsonNode to compare | ||
* @param jsonNode2 the second JsonNode to compare | ||
* @return true if the nodes are considered equal, false otherwise | ||
*/ | ||
boolean equals(JsonNode jsonNode1, JsonNode jsonNode2); | ||
} |
74 changes: 74 additions & 0 deletions
74
src/test/java/com/flipkart/zjsonpatch/JsonSmartArrayDiffTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2016 flipkart.com zjsonpatch. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.flipkart.zjsonpatch; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
import org.junit.Assert; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
|
||
/** | ||
* @author ctranxuan (streamdata.io). | ||
*/ | ||
public class JsonSmartArrayDiffTest { | ||
|
||
private static final int START_INDEX = 0; | ||
private static ObjectMapper objectMapper = new ObjectMapper(); | ||
private static ArrayNode jsonNode; | ||
|
||
@BeforeClass | ||
public static void beforeClass() throws IOException { | ||
String path = "/testdata/smart-array-move.json"; | ||
InputStream resourceAsStream = JsonDiffTest.class.getResourceAsStream(path); | ||
String testData = IOUtils.toString(resourceAsStream, "UTF-8"); | ||
jsonNode = (ArrayNode) objectMapper.readTree(testData); | ||
} | ||
|
||
@Test | ||
public void testSampleJsonDiff() { | ||
for (int i = START_INDEX; i < jsonNode.size(); i++) { | ||
JsonNode first = jsonNode.get(i).get("first"); | ||
JsonNode second = jsonNode.get(i).get("second"); | ||
JsonNode patch = jsonNode.get(i).get("patch"); | ||
JsonNodeEqualsFunction jsonNodeEqualFunction = new JsonNodeEqualsFunction() { | ||
@Override | ||
public boolean equals(JsonNode jsonNode1, JsonNode jsonNode2) { | ||
if (jsonNode1 == null || jsonNode2 == null) { | ||
return false; | ||
} | ||
if (jsonNode1.has("id") && jsonNode2.has("id")) { | ||
return jsonNode1.get("id").asInt() == jsonNode2.get("id").asInt(); | ||
} | ||
return jsonNode1.equals(jsonNode2); | ||
} | ||
|
||
}; | ||
JsonNode actualPatch = JsonDiff.asJson(first, second, DiffFlags.defaults(), jsonNodeEqualFunction); | ||
Assert.assertEquals("JSON Patch not equal [index=" + i + ", first=" + first + "]", patch, actualPatch); | ||
JsonNode secondPrime = JsonPatch.apply(actualPatch, first); | ||
Assert.assertEquals("JSON Patch applies not symmetrical [index=" + i + ", first=" + first + "]", second, | ||
secondPrime); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.