flipkart-incubator/zjsonpatch

Name: zjsonpatch

Owner: Flipkart Incubator

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

Created: 2014-11-17 17:23:43.0

Updated: 2018-01-19 10:52:17.0

Pushed: 2018-01-07 16:29:55.0

Homepage:

Size: 228

Language: Java

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

CircleCI

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

Description & Use-Cases
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
How to use:
Current Version : 0.4.2

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

upId>com.flipkart.zjsonpatch</groupId>
ifactId>zjsonpatch</artifactId>
sion>{version}</version>
API Usage
Obtaining Json Diff as patch
Node 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 -

Apply Json Patch
Node 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
Set<DiffFlags> flags = DiffFlags.dontNormalizeOpIntoMoveAndCopy().clone()
Node patch = JsonDiff.asJson(JsonNode source, JsonNode target, flags)
Example

First Json

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

Second json ( the json to obtain )

": [1,2,0]}

Following patch will be returned:

p":"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
Patch.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

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.