Class JsonPatchDocument
- All Implemented Interfaces:
com.azure.json.JsonSerializable<JsonPatchDocument>
Represents a JSON Patch document.
This class encapsulates a list of JsonPatchOperation instances that form a JSON Patch document.
It provides methods to add various types of operations (add, replace, copy, move, remove, test) to the document.
Each operation in the document is represented by a JsonPatchOperation instance, which encapsulates the
operation kind, path, and optional from and value.
This class also provides a toJson(JsonWriter) method to serialize the JSON Patch document to JSON,
and a fromJson(JsonReader) method to deserialize a JSON Patch document from JSON.
This class is useful when you want to create a JSON Patch document to express a sequence of operations to apply to a JSON document.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new JSON Patch document.JsonPatchDocument(JsonSerializer serializer) Creates a new JSON Patch document. -
Method Summary
Modifier and TypeMethodDescriptionAppends an "add" operation to this JSON Patch document.appendAddRaw(String path, String rawJson) Appends an "add" operation to this JSON Patch document.appendCopy(String from, String path) Appends a "copy" operation to this JSON Patch document.appendMove(String from, String path) Appends a "move" operation to this JSON Patch document.appendRemove(String path) Appends a "remove" operation to this JSON Patch document.appendReplace(String path, Object value) Appends a "replace" operation to this JSON Patch document.appendReplaceRaw(String path, String rawJson) Appends a "replace" operation to this JSON Patch document.appendTest(String path, Object value) Appends a "test" operation to this JSON Patch document.appendTestRaw(String path, String rawJson) Appends a "test" operation to this JSON Patch document.static JsonPatchDocumentfromJson(com.azure.json.JsonReader jsonReader) Reads a JSON stream into aJsonPatchDocument.com.azure.json.JsonWritertoJson(com.azure.json.JsonWriter jsonWriter) toString()Gets a formatted JSON string representation of this JSON Patch document.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface com.azure.json.JsonSerializable
toJson, toJson, toJsonBytes, toJsonString
-
Constructor Details
-
JsonPatchDocument
public JsonPatchDocument()Creates a new JSON Patch document. -
JsonPatchDocument
Creates a new JSON Patch document.If
serializerisn't specifiedJacksonAdapterwill be used.- Parameters:
serializer- TheJsonSerializerthat will be used to serialize patch operation values.
-
-
Method Details
-
appendAdd
Appends an "add" operation to this JSON Patch document.If the
pathdoesn't exist a new member is added to the object. If thepathdoes exist the previous value is replaced. If thepathspecifies an array index the value is inserted at the specified.See JSON Patch Add for more information.
Code Samples
/* * Add an object member to the JSON document { "foo" : "bar" } to get the JSON document * { "bar": "foo", "foo": "bar" }. */ jsonPatchDocument.appendAdd("/bar", "foo"); /* * Add an array element to the JSON document { "foo": [ "fizz", "fizzbuzz" ] } to get the JSON document * { "foo": [ "fizz", "buzz", "fizzbuzz" ] }. */ jsonPatchDocument.appendAdd("/foo/1", "buzz"); /* * Add a nested member to the JSON document { "foo": "bar" } to get the JSON document * { "foo": "bar", "child": { "grandchild": { } } }. */ jsonPatchDocument.appendAdd("/child", Collections.singletonMap("grandchild", Collections.emptyMap())); /* * Add an array element to the JSON document { "foo": [ "fizz", "buzz" ] } to get the JSON document * { "foo": [ "fizz", "buzz", "fizzbuzz" ] }. */ jsonPatchDocument.appendAdd("/foo/-", "fizzbuzz");- Parameters:
path- The path to apply the addition.value- The value that will be serialized and added to the path.- Returns:
- The updated JsonPatchDocument object.
- Throws:
NullPointerException- Ifpathis null.
-
appendAddRaw
Appends an "add" operation to this JSON Patch document.If the
pathdoesn't exist a new member is added to the object. If thepathdoes exist the previous value is replaced. If thepathspecifies an array index the value is inserted at the specified.See JSON Patch Add for more information.
Code Samples
/* * Add an object member to the JSON document { "foo" : "bar" } to get the JSON document * { "bar": "foo", "foo": "bar" }. */ jsonPatchDocument.appendAddRaw("/bar", "\"foo\""); /* * Add an array element to the JSON document { "foo": [ "fizz", "fizzbuzz" ] } to get the JSON document * { "foo": [ "fizz", "buzz", "fizzbuzz" ] }. */ jsonPatchDocument.appendAddRaw("/foo/1", "\"buzz\""); /* * Add a nested member to the JSON document { "foo": "bar" } to get the JSON document * { "foo": "bar", "child": { "grandchild": { } } }. */ jsonPatchDocument.appendAddRaw("/child", "\"child\": { \"grandchild\": { } }"); /* * Add an array element to the JSON document { "foo": [ "fizz", "buzz" ] } to get the JSON document * { "foo": [ "fizz", "buzz", "fizzbuzz" ] }. */ jsonPatchDocument.appendAddRaw("/foo/-", "\"fizzbuzz\"");- Parameters:
path- The path to apply the addition.rawJson- The raw JSON value that will be added to the path.- Returns:
- The updated JsonPatchDocument object.
- Throws:
NullPointerException- Ifpathis null.
-
appendReplace
Appends a "replace" operation to this JSON Patch document.See JSON Patch replace for more information.
Code Samples
/* * Replace an object member in the JSON document { "bar": "qux", "foo": "bar" } to get the JSON document * { "bar": "foo", "foo": "bar" }. */ jsonPatchDocument.appendReplace("/bar", "foo"); /* * Replace an object member in the JSON document { "foo": "fizz" } to get the JSON document * { "foo": [ "fizz", "buzz", "fizzbuzz" ] }. */ jsonPatchDocument.appendReplace("/foo", new String[] {"fizz", "buzz", "fizzbuzz"}); /* * Given the JSON document { "foo": "bar" } the following is an example of an invalid replace operation as the * target path doesn't exist in the document. */ jsonPatchDocument.appendReplace("/baz", "foo");- Parameters:
path- The path to replace.value- The value will be serialized and used as the replacement.- Returns:
- The updated JsonPatchDocument object.
- Throws:
NullPointerException- Ifpathis null.
-
appendReplaceRaw
Appends a "replace" operation to this JSON Patch document.See JSON Patch replace for more information.
Code Samples
/* * Replace an object member in the JSON document { "bar": "qux", "foo": "bar" } to get the JSON document * { "bar": "foo", "foo": "bar" }. */ jsonPatchDocument.appendReplaceRaw("/bar", "\"foo\""); /* * Replace an object member in the JSON document { "foo": "fizz" } to get the JSON document * { "foo": [ "fizz", "buzz", "fizzbuzz" ] }. */ jsonPatchDocument.appendReplaceRaw("/foo", "[ \"fizz\", \"buzz\", \"fizzbuzz\" ]"); /* * Given the JSON document { "foo": "bar" } the following is an example of an invalid replace operation as the * target path doesn't exist in the document. */ jsonPatchDocument.appendReplaceRaw("/baz", "\"foo\"");- Parameters:
path- The path to replace.rawJson- The raw JSON value that will be used as the replacement.- Returns:
- The updated JsonPatchDocument object.
- Throws:
NullPointerException- Ifpathis null.
-
appendCopy
Appends a "copy" operation to this JSON Patch document.See JSON Patch copy for more information.
Code Samples
/* * Copy an object member in the JSON document { "foo": "bar" } to get the JSON document * { "foo": "bar", "copy": "bar" }. */ jsonPatchDocument.appendCopy("/foo", "/copy"); /* * Copy an object member in the JSON document { "foo": { "bar": "baz" } } to get the JSON document * { "foo": { "bar": "baz" }, "bar": "baz" }. */ jsonPatchDocument.appendCopy("/foo/bar", "/bar"); /* * Given the JSON document { "foo": "bar" } the following is an example of an invalid copy operation as the * target from doesn't exist in the document. */ jsonPatchDocument.appendCopy("/baz", "/fizz");- Parameters:
from- The path to copy from.path- The path to copy to.- Returns:
- The updated JsonPatchDocument object.
- Throws:
NullPointerException- Iffromorpathis null.
-
appendMove
Appends a "move" operation to this JSON Patch document.For the operation to be successful
pathcannot be a child node offrom.See JSON Patch move for more information.
Code Samples
/* * Move an object member in the JSON document { "foo": "bar", "bar": "foo" } to get the JSON document * { "bar": "bar" }. */ jsonPatchDocument.appendMove("/foo", "/bar"); /* * Move an object member in the JSON document { "foo": { "bar": "baz" } } to get the JSON document * { "foo": "baz" }. */ jsonPatchDocument.appendMove("/foo/bar", "/foo"); /* * Given the JSON document { "foo": { "bar": "baz" } } the following is an example of an invalid move operation * as the target path is a child of the target from. */ jsonPatchDocument.appendMove("/foo", "/foo/bar"); /* * Given the JSON document { "foo": "bar" } the following is an example of an invalid move operation as the * target from doesn't exist in the document. */ jsonPatchDocument.appendMove("/baz", "/fizz");- Parameters:
from- The path to move from.path- The path to move to.- Returns:
- The updated JsonPatchDocument object.
- Throws:
NullPointerException- Iffromorpathis null.
-
appendRemove
Appends a "remove" operation to this JSON Patch document.See JSON Patch remove for more information.
Code Samples
/* * Remove an object member in the JSON document { "foo": "bar", "bar": "foo" } to get the JSON document * { "foo": "bar" }. */ jsonPatchDocument.appendRemove("/bar"); /* * Remove an object member in the JSON document { "foo": { "bar": "baz" } } to get the JSON document * { "foo": { } }. */ jsonPatchDocument.appendRemove("/foo/bar"); /* * Given the JSON document { "foo": "bar" } the following is an example of an invalid remove operation as the * target from doesn't exist in the document. */ jsonPatchDocument.appendRemove("/baz");- Parameters:
path- The path to remove.- Returns:
- The updated JsonPatchDocument object.
- Throws:
NullPointerException- Ifpathis null.
-
appendTest
Appends a "test" operation to this JSON Patch document.See JSON Patch test for more information.
Code Samples
/* * Test an object member in the JSON document { "foo": "bar" } to get a successful operation. */ jsonPatchDocument.appendTest("/foo", "bar"); /* * Test an object member in the JSON document { "foo": "bar" } to get a unsuccessful operation. */ jsonPatchDocument.appendTest("/foo", 42); /* * Given the JSON document { "foo": "bar" } the following is an example of an unsuccessful test operation as * the target path doesn't exist in the document. */ jsonPatchDocument.appendTest("/baz", "bar");- Parameters:
path- The path to test.value- The value that will be serialized and used to test against.- Returns:
- The updated JsonPatchDocument object.
- Throws:
NullPointerException- Ifpathis null.
-
appendTestRaw
Appends a "test" operation to this JSON Patch document.See JSON Patch test for more information.
Code Samples
/* * Test an object member in the JSON document { "foo": "bar" } to get a successful operation. */ jsonPatchDocument.appendTestRaw("/foo", "\"bar\""); /* * Test an object member in the JSON document { "foo": "bar" } to get a unsuccessful operation. */ jsonPatchDocument.appendTestRaw("/foo", "42"); /* * Given the JSON document { "foo": "bar" } the following is an example of an unsuccessful test operation as * the target path doesn't exist in the document. */ jsonPatchDocument.appendTestRaw("/baz", "\"bar\"");- Parameters:
path- The path to test.rawJson- The raw JSON value that will be used to test against.- Returns:
- The updated JsonPatchDocument object.
- Throws:
NullPointerException- Ifpathis null.
-
toString
Gets a formatted JSON string representation of this JSON Patch document. -
toJson
- Specified by:
toJsonin interfacecom.azure.json.JsonSerializable<JsonPatchDocument>- Throws:
IOException
-
fromJson
Reads a JSON stream into aJsonPatchDocument.- Parameters:
jsonReader- TheJsonReaderbeing read.- Returns:
- The
JsonPatchDocumentthat the JSON stream represented, or null if it pointed to JSON null. - Throws:
IllegalStateException- If the deserialized JSON object was missing any required properties.IOException- If aJsonPatchDocumentfails to be read from thejsonReader.
-