Class JsonPatch
- java.lang.Object
-
- com.github.fge.jsonpatch.JsonPatch
-
- All Implemented Interfaces:
com.fasterxml.jackson.databind.JsonSerializable
public class JsonPatch extends Object implements com.fasterxml.jackson.databind.JsonSerializable
Implementation of JSON PatchJSON Patch, as its name implies, is an IETF draft describing a mechanism to apply a patch to any JSON value. This implementation covers all operations according to the specification; however, there are some subtle differences with regards to some operations which are covered in these operations' respective documentation.
An example of a JSON Patch is as follows:
[ { "op": "add", "path": "/-", "value": { "productId": 19, "name": "Duvel", "type": "beer" } } ]This patch contains a single operation which adds an item at the end of an array. A JSON Patch can contain more than one operation; in this case, all operations are applied to the input JSON value in their order of appearance, until all operations are applied or an error condition is encountered.
The main point where this implementation differs from the specification is initial JSON parsing. The draft says:
Operation objects MUST have exactly one "op" memberand:
Additionally, operation objects MUST have exactly one "path" member.However, obeying these to the letter forces constraints on the JSON parser. Here, these constraints are not enforced, which means:
[ { "op": "add", "op": "remove", "path": "/x" } ]is parsed (as a
removeoperation, since it appears last).IMPORTANT NOTE: the JSON Patch is supposed to be VALID when the constructor for this class (
fromJson(JsonNode)is used.
-
-
Field Summary
Fields Modifier and Type Field Description protected static com.github.fge.msgsimple.bundle.MessageBundleBUNDLEprotected static JsonPatchFactoryJSON_PATCH_FACTORYprotected List<JsonPatchOperation>operationsList of operations
-
Constructor Summary
Constructors Constructor Description JsonPatch(List<JsonPatchOperation> operations)Constructor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description com.fasterxml.jackson.databind.JsonNodeapply(com.fasterxml.jackson.databind.JsonNode node)Apply this patch to a JSON valuestatic JsonPatchfromJson(com.fasterxml.jackson.databind.JsonNode node)Deprecated.This uses a static StandardJsonPatchFactory to produce the JsonPatch.List<JsonPatchOperation>getOperations()voidserialize(com.fasterxml.jackson.core.JsonGenerator jgen, com.fasterxml.jackson.databind.SerializerProvider provider)voidserializeWithType(com.fasterxml.jackson.core.JsonGenerator jgen, com.fasterxml.jackson.databind.SerializerProvider provider, com.fasterxml.jackson.databind.jsontype.TypeSerializer typeSer)StringtoString()
-
-
-
Field Detail
-
BUNDLE
protected static final com.github.fge.msgsimple.bundle.MessageBundle BUNDLE
-
JSON_PATCH_FACTORY
protected static final JsonPatchFactory JSON_PATCH_FACTORY
-
operations
protected final List<JsonPatchOperation> operations
List of operations
-
-
Constructor Detail
-
JsonPatch
public JsonPatch(List<JsonPatchOperation> operations)
ConstructorNormally, you should never have to use it.
- Parameters:
operations- the list of operations for this patch- See Also:
JsonPatchOperation
-
-
Method Detail
-
fromJson
@Deprecated public static JsonPatch fromJson(com.fasterxml.jackson.databind.JsonNode node) throws IOException
Deprecated.This uses a static StandardJsonPatchFactory to produce the JsonPatch. Use the appropriate JsonPatchFactory instead.Static factory method to build a JSON Patch out of a JSON representation- Parameters:
node- the JSON representation of the generated JSON Patch- Returns:
- a JSON Patch
- Throws:
IOException- input is not a valid JSON patchNullPointerException- input is null
-
apply
public com.fasterxml.jackson.databind.JsonNode apply(com.fasterxml.jackson.databind.JsonNode node) throws JsonPatchExceptionApply this patch to a JSON value- Parameters:
node- the value to apply the patch to- Returns:
- the patched JSON value
- Throws:
JsonPatchException- failed to apply patchNullPointerException- input is null
-
serialize
public void serialize(com.fasterxml.jackson.core.JsonGenerator jgen, com.fasterxml.jackson.databind.SerializerProvider provider) throws IOException- Specified by:
serializein interfacecom.fasterxml.jackson.databind.JsonSerializable- Throws:
IOException
-
serializeWithType
public void serializeWithType(com.fasterxml.jackson.core.JsonGenerator jgen, com.fasterxml.jackson.databind.SerializerProvider provider, com.fasterxml.jackson.databind.jsontype.TypeSerializer typeSer) throws IOException- Specified by:
serializeWithTypein interfacecom.fasterxml.jackson.databind.JsonSerializable- Throws:
IOException
-
getOperations
public List<JsonPatchOperation> getOperations()
-
-