bsonpatch
bsonpatch copied to clipboard
A BSON implementation of RFC 6902 to compute the difference between two BSON documents
This is an implementation of RFC 6902 JSON Patch written in Java.
This JSON Patch implementation works directly with BSON documents using the MongoDB Java driver implementation of BSON.
The code here was ported (copied, renamed, repackaged, modified) from the zjsonpatch project.
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.
- This library compares two BsonValue inputs and produces a BsonArray of the changes.
Compatible with : Java 8 and above all versions
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 JSON Array 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.4.12
Add following to <dependencies/>
section of your pom.xml -
<dependency>
<groupId>com.ebay.bsonpatch</groupId>
<artifactId>bsonpatch</artifactId>
<version>0.4.12</version>
</dependency>
API Usage
Obtaining Json Diff as patch
BsonArray patch = BsonDiff.asBson(BsonValue source, BsonValue target)
Computes and returns a JSON patch
(as a BsonArray) from source
to target
,
Both source
and target
must be either valid BSON 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
-
copy
Apply Json Patch
BsonValue target = BsonPatch.apply(BsonArray patch, BsonValue source);
Given a patch
, apply it to source
Bson and return a target
Bson which can be ( Bson object or array or value ). This operation performed on a clone of source
Bson ( thus, source
Bson is untouched and can be used further).
To turn off MOVE & COPY Operations
EnumSet<DiffFlags> flags = DiffFlags.dontNormalizeOpIntoMoveAndCopy().clone()
BsonArray patch = BsonDiff.asJson(BsonValue source, BsonValue target, flags)
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"}]
here "op"
represents the operation ("move"
), "from"
represent path from where value should be moved, "path"
represents where value should be moved. The value that is moved is taken as the content at the "from"
path.
Apply Json Patch In-Place
BsonPatch.applyInPlace(BsonArray patch, BsonValue source);
Given a patch
, it will apply it to the source
BSON mutating the instance, opposed to BsonPatch.apply
which returns
a new instance with the patch applied, leaving the source
unchanged.
Tests:
- 100+ selective hardcoded different input JSONs , with their driver test classes present under /test directory.
- Apart from selective input, a deterministic random JSON generator is present under ( TestDataGenerator.java ), and its driver test class method is JsonDiffTest.testGeneratedJsonDiff().
*** Tests can only show presence of bugs and not their absence ***
Get Involved
-
Contributing: Pull requests are welcome!
- Read
CONTRIBUTING.md
- Submit github issues for any feature enhancements, bugs or documentation problems
- Read
-
Support: Questions/comments can posted as github issues