Giter Site home page Giter Site logo

zjsonpatch's Introduction

CircleCI

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

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 6 / 7 / 8

Code Coverage

Package Class, % Method, % Line, %
all classes 100% (6/ 6) 93.6% (44/ 47) 96.2% (332/ 345)

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.4.1

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

<groupId>com.flipkart.zjsonpatch</groupId>
<artifactId>zjsonpatch</artifactId>
<version>{version}</version>
  • Available on maven cental repository

API Usage

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
  • COPY
  • TEST

Apply Json Patch

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

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

To turn off MOVE & COPY Operations

EnumSet<DiffFlags> flags = DiffFlags.dontNormalizeOpIntoMoveAndCopy().clone()
JsonNode patch = JsonDiff.asJson(JsonNode source, JsonNode 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","value":0}]

here 'op' represents Operation, 'from' represent path from where value should be moved, 'path' represents where value should be moved and value represents value to move.

Apply Json Patch In-Place

JsonPatch.applyInPlace(JsonNode patch, JsonNode source);

Given a patch, it will apply it to the source JSON mutating the instance, opposed to JsonPatch.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 ***

zjsonpatch's People

Contributors

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

Watchers

 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.