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.
Note the "L" in "LGPL". LGPL AND GPL ARE QUITE DIFFERENT!
Versions before 1.10 are available at groupIdcom.github.fge.
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 two choices to build a JsonPatch instance: use Jackson deserialization, or initialize one
directly from a JsonNode. Examples:
// Using JacksonfinalObjectMappermapper = newObjectMapper();
finalInputStreamin = ...;
finalJsonPatchpatch = mapper.readValue(in, JsonPatch.class);
// From a JsonNodefinalJsonPatchpatch = JsonPatch.fromJson(node);
You can then apply the patch to your data:
// orig is also a JsonNodefinalJsonNodepatched = patch.apply(orig);
JSON diff
The main class is JsonDiff. It returns the patch as a JsonPatch or as a JsonNode. Sample usage:
Important note: the API offers no guarantee at all about patch "reuse";
that is, the generated patch is only guaranteed to safely transform the given
source to the given target. Do not expect it to give the result you expect on
another source/target pair!
JSON Merge Patch
As for JsonPatch, you may use either Jackson or "direct" initialization:
// With JacksonfinalJsonMergePatchpatch = mapper.readValue(in, JsonMergePatch.class);
// With a JsonNodefinalJsonMergePatchpatch = JsonMergePatch.fromJson(node);
Applying a patch also uses an .apply() method:
// orig is also a JsonNodefinalJsonNodepatched = patch.apply(orig);
请发表评论