Giter Club home page Giter Club logo

bsonpatch's Introduction

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.
  • 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.9

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

<dependency>
  <groupId>com.ebay.bsonpatch</groupId>
  <artifactId>bsonpatch</artifactId>
  <version>0.4.9</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:

  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().

*** Tests can only show presence of bugs and not their absence ***

Get Involved

Maintainers

bsonpatch's People

Contributors

vishwakarma avatar dandoug avatar holograph avatar ctranxuan avatar hugebdu avatar alexanderyastrebov avatar hansjoachim avatar ysangkok avatar vermapratyush avatar bryant1410 avatar wojciechbulaty avatar puntogil avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.