Builds are now verified by Confluxsys
This is an implementation of RFC 6902 (JSON Patch) and RFC 7386 (JSON Merge Patch) written in Java, which uses Jackson (2.2.x) at its core. There are a few customization for more accuracy but it still supports all the original standards of the implementations mentioned.
Its features are:
- {de,}serialization of JSON Patch and JSON Merge Patch instances with Jackson;
- full support for RFC 6902 operations, including
test; - JSON "diff" (RFC 6902 and custom) with custom operations factorization.
The current version is 1.0. See file RELEASE-NOTES.md for details.
In-progress
When computing the difference between two JSON texts (in the form of JsonNode instances), the diff
will factorize value removals and additions as moves and copies.
For instance, given this node to patch:
{ "a": "b" }in order to obtain:
{ "c": "b" }the implementation will return the following patch:
[ { "op": "move", "from": "/a", "path": "/c" } ]The Custom diff only compute difference in form of add, remove and replace. The add, remove and replace operation is same as specified in rfc6901 but only in case of object,there is change in case of array diff operations the remove and replace operations have attached extra {Key: value} of original_value: value which helps us to keep track of the state before the difference was calculated.
In case of Absense of Key the Algorithm treats whole object as Key and cant be used for calculation of fine-grained difference between the JsonNode
This Library Supports Custom Operation in Case of Array as ...(In-progress)
It is able to do even more than that. See the test files in the project.
RFC 6902 mandates that when testing for numeric values, however deeply nested in the tested value, a test is successful if the numeric values are mathematically equal. That is, JSON texts:
1and:
1.00must be considered equal.
This implementation obeys the RFC; for this, it uses the numeric equivalence of jackson-coreutils.
You have to choices to build a JsonPatch instance: use Jackson deserialization, or initialize one
directly from a JsonNode. Examples:
// Using Jackson
final ObjectMapper mapper = new ObjectMapper();
final InputStream in = ...;
final JsonPatch patch = mapper.readValue(in, JsonPatch.class);
// From a JsonNode
final JsonPatch patch = JsonPatch.fromJson(node);
You can then apply the patch to your data:
// orig is also a JsonNode
final JsonNode patched = patch.apply(orig);The main class is JsonDiff. It returns the patch as a JsonPatch or as a JsonNode. Sample usage:
final JsonPatch patch = JsonDiff.asJsonPatch(source, target);
final JsonNode patchNode = JsonDiff.asJson(source, target);As for JsonPatch, you may use either Jackson or "direct" initialization:
// With Jackson
final JsonMergePatch patch = mapper.readValue(in, JsonMergePatch.class);
// With a JsonNode
final JsonMergePatch patch = JsonMergePatch.fromJson(node);Applying a patch also uses an .apply() method:
// orig is also a JsonNode
final JsonNode patched = patch.apply(orig);The main class is JsonDiff. It returns the patch as a JsonPatch or as a JsonNode. Sample usage:
final JsonPatch patch = JsonDiff.asJsonPatch(source, target, attributeKeyFieldMap);
final JsonNode patchNode = JsonDiff.asJson(source, target, attributeKeyFieldMap);attributeKeyFieldMap is the Map of <JsonPointer,String>, read RFC6902 for JsonPointer. JsonPointer contains all the Key Fields Inside an Array object which you want to treat as primary Key.
The API offers no guarantee at all about patch "reuse"; that is, the generated patch is only guaranteed to safely transform the given source to the given target. Do not expect it to give the result you expect on another source/target pair!