JsonPatchDocument Class

  • java.lang.Object
    • com.azure.core.models.JsonPatchDocument

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
JsonPatchDocument appendAdd(String path, Object value)

Appends an "add" operation to this JSON Patch document.

JsonPatchDocument appendAddRaw(String path, String rawJson)

Appends an "add" operation to this JSON Patch document.

JsonPatchDocument appendCopy(String from, String path)

Appends a "copy" operation to this JSON Patch document.

JsonPatchDocument appendMove(String from, String path)

Appends a "move" operation to this JSON Patch document.

JsonPatchDocument appendRemove(String path)

Appends a "remove" operation to this JSON Patch document.

JsonPatchDocument appendReplace(String path, Object value)

Appends a "replace" operation to this JSON Patch document.

JsonPatchDocument appendReplaceRaw(String path, String rawJson)

Appends a "replace" operation to this JSON Patch document.

JsonPatchDocument appendTest(String path, Object value)

Appends a "test" operation to this JSON Patch document.

JsonPatchDocument appendTestRaw(String path, String rawJson)

Appends a "test" operation to this JSON Patch document.

static JsonPatchDocument fromJson(JsonReader jsonReader)

Reads a JSON stream into a JsonPatchDocument.

JsonWriter 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:

serializer - The JsonSerializer that will be used to serialize patch operation values.

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:

path - The path to apply the addition.
value - The value that will be serialized and added to the path.

Returns:

The updated JsonPatchDocument object.

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:

path - The path to apply the addition.
rawJson - The raw JSON value that will be added to the path.

Returns:

The updated JsonPatchDocument object.

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:

from - The path to copy from.
path - The path to copy to.

Returns:

The updated JsonPatchDocument object.

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:

from - The path to move from.
path - The path to move to.

Returns:

The updated JsonPatchDocument object.

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:

path - The path to remove.

Returns:

The updated JsonPatchDocument object.

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:

path - The path to replace.
value - The value will be serialized and used as the replacement.

Returns:

The updated JsonPatchDocument object.

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:

path - The path to replace.
rawJson - The raw JSON value that will be used as the replacement.

Returns:

The updated JsonPatchDocument object.

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:

path - The path to test.
value - The value that will be serialized and used to test against.

Returns:

The updated JsonPatchDocument object.

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:

path - The path to test.
rawJson - The raw JSON value that will be used to test against.

Returns:

The updated JsonPatchDocument object.

fromJson

public static JsonPatchDocument fromJson(JsonReader jsonReader)

Reads a JSON stream into a JsonPatchDocument.

Parameters:

jsonReader - The JsonReader being read.

Returns:

The JsonPatchDocument that the JSON stream represented, or null if it pointed to JSON null.

Throws:

IOException

- If the deserialized JSON object was missing any required properties.

toJson

public JsonWriter toJson(JsonWriter jsonWriter)

Parameters:

jsonWriter

Throws:

toString

public String toString()

Gets a formatted JSON string representation of this JSON Patch document.

Overrides:

JsonPatchDocument.toString()

Returns:

The formatted JSON String representing this JSON Patch document.

Applies to