GithubHelp home page GithubHelp logo

royclarkson / json-patch Goto Github PK

View Code? Open in Web Editor NEW

This project forked from java-json-tools/json-patch

0.0 1.0 0.0 469 KB

An RFC 6902 (JSON Patch) and reverse, plus JSON Merge Patch, implementation in Java using Jackson (2.2.x)

License: Other

Groovy 6.48% Shell 0.11% Java 93.41%

json-patch's Introduction

Read me first

This project, as of version 1.4, is licensed under both LGPLv3 and ASL 2.0. See file LICENSE for more details. Versions 1.3 and lower are licensed under LGPLv3 only.

What this is

This is an implementation of RFC 6902 (JSON Patch) and JSON Merge Patch written in Java, which uses Jackson (2.2.x) at its core.

Its features are:

  • {de,}serialization of JSON Patch and JSON merge patch instances with Jackson;
  • full support for RFC 6902 operations, including test;
  • JSON "diff" (RFC 6902 only) with operation factorization.

The JSON diff implementation is courtesy of Randy Watler.

Versions

The current version is 1.6. See file RELEASE-NOTES.md for details.

Using it in your project

With Gradle:

dependencies {
    compile(group: "com.github.fge", name: "json-patch", version: "yourVersionHere");
}

With Maven:

<dependency>
    <groupId>com.github.fge</groupId>
    <artifactId>json-patch</artifactId>
    <version>yourVersionHere</version>
</dependency>

JSON "diff" factorization

When computing the difference between two JSON texts (in the form of JsonNode instances), the diff will factorize value removals and additions as moves and copies.

For instance, given this node to patch:

{ "a": "b" }

in order to obtain:

{ "c": "b" }

the implementation will return the following patch:

[ { "op": "move", "from": "/a", "path": "/c" } ]

It is able to do even more than that. See the test files in the project.

Note about the test operation and numeric value equivalence

RFC 6902 mandates that when testing for numeric values, however deeply nested in the tested value, a test is successful if the numeric values are mathematically equal. That is, JSON texts:

1

and:

1.00

must be considered equal.

This implementation obeys the RFC; for this, it uses the numeric equivalence of jackson-coreutils.

Sample usage

JSON Patch

You have to choices to build a JsonPatch instance: use Jackson deserialization, or initialize one directly from a JsonNode. Examples:

// Using Jackson
final ObjectMapper mapper = new ObjectMapper();
final InputStream in = ...;
final JsonPatch patch = mapper.readValue(in, JsonPatch.class);
// From a JsonNode
final JsonPatch patch = JsonPatch.fromJson(node);

You can then apply the patch to your data:

// orig is also a JsonNode
final JsonNode patched = patch.apply(orig);

JSON diff

The main class is JsonDiff. It returns the patch as a JsonNode. Sample usage:

final JsonNode patchNode = JsonDiff.asJson(firstNode, secondNode);

You can then use the generated JsonNode to build a patch using the code sample above.

JSON Merge Patch

As for JsonPatch, you may use either Jackson or "direct" initialization:

// With Jackson
final JsonMergePatch patch = mapper.readValue(in, JsonMergePatch.class);
// With a JsonNode
final JsonMergePatch patch = JsonMergePatch.fromJson(node);

Applying a patch also uses an .apply() method:

// orig is also a JsonNode
final JsonNode patched = patch.apply(orig);

json-patch's People

Contributors

fge avatar joeltucci 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.