Package com.gravity9.jsonpatch.diff
Class JsonDiff
- java.lang.Object
-
- com.gravity9.jsonpatch.diff.JsonDiff
-
@ParametersAreNonnullByDefault public final class JsonDiff extends java.lang.Object
JSON "diff" implementationThis class generates a JSON Patch (as in, an RFC 6902 JSON Patch) given two JSON values as inputs. The patch can be obtained directly as a
JsonPatchor as aJsonNode.Note: there is no guarantee about the usability of the generated patch for any other source/target combination than the one used to generate the patch.
This class always performs operations in the following order: removals, additions and replacements. It then factors removal/addition pairs into move operations, or copy operations if a common element exists, at the same
pointer, in both the source and destination.You can obtain a diff either as a
JsonPatchdirectly or, for backwards compatibility, as aJsonNode.- Since:
- 1.2
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static com.fasterxml.jackson.databind.JsonNodeasJson(com.fasterxml.jackson.databind.JsonNode source, com.fasterxml.jackson.databind.JsonNode target)Generate a JSON patch for transforming the source node into the target nodestatic com.fasterxml.jackson.databind.JsonNodeasJsonIgnoringFields(com.fasterxml.jackson.databind.JsonNode source, com.fasterxml.jackson.databind.JsonNode target, java.util.List<java.lang.String> fieldsToIgnore)Generate a JSON patch for transforming the source node into the target node ignoring given fieldsstatic JsonPatchasJsonPatch(com.fasterxml.jackson.databind.JsonNode source, com.fasterxml.jackson.databind.JsonNode target)Generate a JSON patch for transforming the source node into the target nodestatic JsonPatchasJsonPatchIgnoringFields(com.fasterxml.jackson.databind.JsonNode source, com.fasterxml.jackson.databind.JsonNode target, java.util.List<java.lang.String> fieldsToIgnore)Generate a JSON patch for transforming the source node into the target node ignoring given fields
-
-
-
Method Detail
-
asJsonPatch
public static JsonPatch asJsonPatch(com.fasterxml.jackson.databind.JsonNode source, com.fasterxml.jackson.databind.JsonNode target)
Generate a JSON patch for transforming the source node into the target node- Parameters:
source- the node to be patchedtarget- the expected result after applying the patch- Returns:
- the patch as a
JsonPatch - Since:
- 1.9
-
asJsonPatchIgnoringFields
public static JsonPatch asJsonPatchIgnoringFields(com.fasterxml.jackson.databind.JsonNode source, com.fasterxml.jackson.databind.JsonNode target, java.util.List<java.lang.String> fieldsToIgnore) throws JsonPatchException
Generate a JSON patch for transforming the source node into the target node ignoring given fields- Parameters:
source- the node to be patchedtarget- the expected result after applying the patchfieldsToIgnore- list of JsonPath or JsonPointer paths which should be ignored when generating diff. Non-existing fields are ignored.- Returns:
- the patch as a
JsonPatch - Throws:
JsonPatchException- if fieldsToIgnored not in valid JsonPath or JsonPointer format- Since:
- 2.0.0
-
asJson
public static com.fasterxml.jackson.databind.JsonNode asJson(com.fasterxml.jackson.databind.JsonNode source, com.fasterxml.jackson.databind.JsonNode target) throws JsonPatchExceptionGenerate a JSON patch for transforming the source node into the target node- Parameters:
source- the node to be patchedtarget- the expected result after applying the patch- Returns:
- the patch as a
JsonNode - Throws:
JsonPatchException- when cannot generate JSON diff
-
asJsonIgnoringFields
public static com.fasterxml.jackson.databind.JsonNode asJsonIgnoringFields(com.fasterxml.jackson.databind.JsonNode source, com.fasterxml.jackson.databind.JsonNode target, java.util.List<java.lang.String> fieldsToIgnore) throws JsonPatchExceptionGenerate a JSON patch for transforming the source node into the target node ignoring given fields- Parameters:
source- the node to be patchedtarget- the expected result after applying the patchfieldsToIgnore- list of JsonPath or JsonPointer paths which should be ignored when generating diff. Non-existing fields are ignored.- Returns:
- the patch as a
JsonNode - Throws:
JsonPatchException- if fieldsToIgnored not in valid JsonPath or JsonPointer format- Since:
- 2.0.0
-
-