JsonPatchDocument Class
- java.
lang. Object - com.
azure. core. models. JsonPatchDocument
- com.
Implements
public final class JsonPatchDocument
implements 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 jsonWriter) method to serialize the JSON Patch document to JSON, and a fromJson(JsonReader 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.
Constructor Summary
Constructor | Description |
---|---|
JsonPatchDocument() |
Creates a new JSON Patch document. |
JsonPatchDocument(JsonSerializer serializer) |
Creates a new JSON Patch document. |
Method Summary
Modifier and Type | Method and Description |
---|---|
Json |
appendAdd(String path, Object value)
Appends an "add" operation to this JSON Patch document. |
Json |
appendAddRaw(String path, String rawJson)
Appends an "add" operation to this JSON Patch document. |
Json |
appendCopy(String from, String path)
Appends a "copy" operation to this JSON Patch document. |
Json |
appendMove(String from, String path)
Appends a "move" operation to this JSON Patch document. |
Json |
appendRemove(String path)
Appends a "remove" operation to this JSON Patch document. |
Json |
appendReplace(String path, Object value)
Appends a "replace" operation to this JSON Patch document. |
Json |
appendReplaceRaw(String path, String rawJson)
Appends a "replace" operation to this JSON Patch document. |
Json |
appendTest(String path, Object value)
Appends a "test" operation to this JSON Patch document. |
Json |
appendTestRaw(String path, String rawJson)
Appends a "test" operation to this JSON Patch document. |
static
Json |
fromJson(JsonReader jsonReader)
Reads a JSON stream into a JsonPatchDocument. |
Json |
toJson(JsonWriter jsonWriter) |
String |
toString()
Gets a formatted JSON string representation of this JSON Patch document. |
Methods inherited from java.lang.Object
Constructor Details
JsonPatchDocument
public JsonPatchDocument()
Creates a new JSON Patch document.
JsonPatchDocument
public JsonPatchDocument(JsonSerializer serializer)
Creates a new JSON Patch document.
If serializer
isn't specified JacksonAdapter will be used.
Parameters:
Method Details
appendAdd
public JsonPatchDocument appendAdd(String path, Object value)
Appends an "add" operation to this JSON Patch document.
If the path
doesn't exist a new member is added to the object. If the path
does exist the previous value is replaced. If the path
specifies 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:
Returns:
appendAddRaw
public JsonPatchDocument appendAddRaw(String path, String rawJson)
Appends an "add" operation to this JSON Patch document.
If the path
doesn't exist a new member is added to the object. If the path
does exist the previous value is replaced. If the path
specifies 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:
Returns:
appendCopy
public JsonPatchDocument appendCopy(String from, String path)
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:
Returns:
appendMove
public JsonPatchDocument appendMove(String from, String path)
Appends a "move" operation to this JSON Patch document.
For the operation to be successful path
cannot be a child node of from
.
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:
Returns:
appendRemove
public JsonPatchDocument appendRemove(String path)
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:
Returns:
appendReplace
public JsonPatchDocument appendReplace(String path, Object value)
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:
Returns:
appendReplaceRaw
public JsonPatchDocument appendReplaceRaw(String path, String rawJson)
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:
Returns:
appendTest
public JsonPatchDocument appendTest(String path, Object value)
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:
Returns:
appendTestRaw
public JsonPatchDocument appendTestRaw(String path, String rawJson)
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:
Returns:
fromJson
public static JsonPatchDocument fromJson(JsonReader jsonReader)
Reads a JSON stream into a JsonPatchDocument.
Parameters:
Returns:
Throws:
toJson
toString
public String toString()
Gets a formatted JSON string representation of this JSON Patch document.
Overrides:
JsonPatchDocument.toString()Returns:
Applies to
Azure SDK for Java
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for