Class JsonMergePatch
- java.lang.Object
-
- com.gravity9.jsonpatch.mergepatch.JsonMergePatch
-
- All Implemented Interfaces:
com.fasterxml.jackson.databind.JsonSerializable,Patch
@ParametersAreNonnullByDefault public abstract class JsonMergePatch extends java.lang.Object implements com.fasterxml.jackson.databind.JsonSerializable, Patch
Implementation of JSON Merge Patch (RFC 7386)JSON Merge Patch is a "toned down" version of JSON Patch. However, it covers a very large number of use cases for JSON value modifications; its focus is mostly on patching JSON Objects, which are by far the most common type of JSON texts exchanged on the Internet.
Applying a JSON Merge Patch is defined by a single, pseudo code function as follows (quoted from the RFC; indentation fixed):
define MergePatch(Target, Patch): if Patch is an Object: if Target is not an Object: Target = {} # Ignore the contents and set it to an empty Object for each Name/Value pair in Patch: if Value is null: if Name exists in Target: remove the Name/Value pair from Target else: Target[Name] = MergePatch(Target[Name], Value) return Target else: return Patch
-
-
Field Summary
Fields Modifier and Type Field Description protected static MessageBundleBUNDLE
-
Constructor Summary
Constructors Constructor Description JsonMergePatch()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract com.fasterxml.jackson.databind.JsonNodeapply(com.fasterxml.jackson.databind.JsonNode input)Apply the patch to a given JSON valuestatic JsonMergePatchfromJson(com.fasterxml.jackson.databind.JsonNode node)Build an instance from a JSON inputstatic JsonMergePatchfromJson(com.fasterxml.jackson.databind.JsonNode node, com.fasterxml.jackson.databind.ObjectMapper mapper)Build an instance from a JSON input with a custom ObjectMapper.
-
-
-
Field Detail
-
BUNDLE
protected static final MessageBundle BUNDLE
-
-
Method Detail
-
fromJson
public static JsonMergePatch fromJson(com.fasterxml.jackson.databind.JsonNode node) throws JsonPatchException
Build an instance from a JSON input- Parameters:
node- the input- Returns:
- a JSON Merge Patch instance
- Throws:
JsonPatchException- failed to deserializejava.lang.NullPointerException- node is null
-
fromJson
public static JsonMergePatch fromJson(com.fasterxml.jackson.databind.JsonNode node, com.fasterxml.jackson.databind.ObjectMapper mapper) throws JsonPatchException
Build an instance from a JSON input with a custom ObjectMapper. This allows to customize the mapper used in deserialization of nodes.- Parameters:
node- the inputmapper- custom ObjectMapper- Returns:
- a JSON Merge Patch instance
- Throws:
JsonPatchException- failed to deserializejava.lang.NullPointerException- node or mapper is null
-
apply
public abstract com.fasterxml.jackson.databind.JsonNode apply(com.fasterxml.jackson.databind.JsonNode input) throws JsonPatchExceptionApply the patch to a given JSON value- Specified by:
applyin interfacePatch- Parameters:
input- the value to patch- Returns:
- the patched value
- Throws:
JsonPatchException- never thrown; only for consistency withJsonPatchjava.lang.NullPointerException- value is null
-
-