/zjsonpatch

This is an implementation of RFC 6902 JSON Patch written in Java

Primary LanguageJavaApache License 2.0Apache-2.0

Implementation of RFC 6902 JSON Patch in Java.

Fork from original to use Java 8 instead of Guava library. Note: there is a dependency of JsonDiff on Apache commons-collections4 to be "provided"!

##Description & Use-Cases

  • Java Library to find / apply JSON Patches according to RFC 6902.
  • JSON Patch defines a JSON document structure for representing changes to a JSON document.
  • It can be used to avoid sending a whole document when only a part has changed, thus reducing network bandwidth requirements if data (in json format) is required to send across multiple systems over network or in case of multi DC transfer.
  • When used in combination with the HTTP PATCH method as per RFC 5789 HTTP PATCH, it will do partial updates for HTTP APIs in a standard way.

###Compatible with : Java 8

##Complexity

  • To find JsonPatch : Ω(N+M) ,N and M represents number of keys in first and second json respectively / O(summation of la*lb) where la , lb represents jsonArray of length la / lb of against same key in first and second json ,since LCS is used to find difference between 2 json arrays there of order of quadratic.
  • To Optimize Diffs ( compact move and remove into Move ) : Ω(D) / O(D*D) where D represents number of diffs obtained before compaction into Move operation.
  • To Apply Diff : O(D) where D represents number of diffs

How to use:

###Current Version : 0.2.3

Add following to <dependencies/> section of your pom.xml -

<groupId>com.github.vkovalchuk</groupId>
<artifactId>zjsonpatch</artifactId>
<version>{version}</version>
  • Avaibale on maven cental repository
  • Available on Clojars repository, access by adding following to your <repositories/> section of pom.xml -
<repository>
  <id>clojars</id>
  <name>Clojars repository</name>
  <url>https://clojars.org/repo</url>
</repository>

Clojars Project

API Usage

Apply Json Patch

JsonNode target = JsonPatch.apply(JsonNode patch, JsonNode source);

Given a Patch, it applies it to source Json and returns a target json which can be (json object or array or value). This operation is performed on a clone of source json (thus, source json is untouched and can be used further).

Obtaining Json Diff as patch

JsonNode patch = JsonDiff.asJson(JsonNode source, JsonNode target)

Computes and returns a JSON Patch from source to target, Both source and target must be either valid JSON objects or arrays or values. Further, if resultant patch is applied to source, it will yield target.

The algorithm which computes this JsonPatch currently generates following operations as per rfc 6902 -

  • ADD
  • REMOVE
  • REPLACE
  • MOVE

Example

First Json

{"a": 0,"b": [1,2]}

Second json ( the json to obtain )

 {"b": [1,2,0]}

Following patch will be returned:

[{"op":"MOVE","from":"/a","path":"/b/2","value":0}]

here o represents Operation, p represent fromPath from where value should be moved, tp represents toPath where value should be moved and v represents value to move.

Tests:

  1. 100+ selective hardcoded different input jsons, with their driver test classes present under /test directory.
  2. Apart from selective input, a deterministic random json generator is present under ( TestDataGenerator.java ), and its driver test class method is JsonDiffTest.testGeneratedJsonDiff().