public final class JsonPatchDocument extends Object
Constructor and Description |
---|
JsonPatchDocument()
Creates a new JSON Patch document.
|
JsonPatchDocument(JsonSerializer serializer)
Creates a new JSON Patch document.
|
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.
|
String |
toString()
Gets a formatted JSON string representation of this JSON Patch document.
|
public JsonPatchDocument()
public JsonPatchDocument(JsonSerializer serializer)
If serializer
isn't specified JacksonAdapter
will be used.
serializer
- The JsonSerializer
that will be used to serialize patch operation values.public JsonPatchDocument appendAdd(String path, Object value)
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");
path
- The path to apply the addition.value
- The value that will be serialized and added to the path.NullPointerException
- If path
is null.public JsonPatchDocument appendAddRaw(String path, String rawJson)
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\"");
path
- The path to apply the addition.rawJson
- The raw JSON value that will be added to the path.NullPointerException
- If path
is null.public JsonPatchDocument appendReplace(String path, Object value)
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");
path
- The path to replace.value
- The value will be serialized and used as the replacement.NullPointerException
- If path
is null.public JsonPatchDocument appendReplaceRaw(String path, String rawJson)
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\"");
path
- The path to replace.rawJson
- The raw JSON value that will be used as the replacement.NullPointerException
- If path
is null.public JsonPatchDocument appendCopy(String from, String path)
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");
from
- The path to copy from.path
- The path to copy to.NullPointerException
- If from
or path
is null.public JsonPatchDocument appendMove(String from, String path)
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");
from
- The path to move from.path
- The path to move to.NullPointerException
- If from
or path
is null.public JsonPatchDocument appendRemove(String path)
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");
path
- The path to remove.NullPointerException
- If path
is null.public JsonPatchDocument appendTest(String path, Object value)
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");
path
- The path to test.value
- The value that will be serialized and used to test against.NullPointerException
- If path
is null.public JsonPatchDocument appendTestRaw(String path, String rawJson)
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\"");
path
- The path to test.rawJson
- The raw JSON value that will be used to test against.NullPointerException
- If path
is null.Copyright © 2021 Microsoft Corporation. All rights reserved.