- java.lang.Object
-
- org.glassfish.json.JsonPatchBuilderImpl
-
- All Implemented Interfaces:
JsonPatchBuilder
public final class JsonPatchBuilderImpl extends Object implements JsonPatchBuilder
A builder for constructing a JSON Patch by adding JSON Patch operations incrementally.The following illustrates the approach.
JsonPatchBuilder builder = Json.createPatchBuilder(); JsonPatch patch = builder.add("/John/phones/office", "1234-567") .remove("/Amy/age") .build();
The result is equivalent to the following JSON Patch.[ {"op" = "add", "path" = "/John/phones/office", "value" = "1234-567"}, {"op" = "remove", "path" = "/Amy/age"} ]
- Since:
- 1.1
-
-
Constructor Summary
Constructors Constructor Description JsonPatchBuilderImpl()
Creates JsonPatchBuilderImpl with empty JSON PatchJsonPatchBuilderImpl(JsonArray patch)
Creates a JsonPatchBuilderImpl, starting with the specified JSON Patch
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description JsonPatchBuilder
add(String path, boolean value)
Adds an "add" JSON Patch operationJsonPatchBuilder
add(String path, int value)
Adds an "add" JSON Patch operationJsonPatchBuilder
add(String path, JsonValue value)
Adds an "add" JSON Patch operation.JsonPatchBuilder
add(String path, String value)
Adds an "add" JSON Patch operation<T extends JsonStructure>
Tapply(T target)
A convenience method fornew JsonPatchImpl(build()).apply(target)
.JsonPatch
build()
Returns the patch operation in a JsonPatchJsonArray
buildAsJsonArray()
Returns the patch operations in a JsonArrayJsonPatchBuilder
copy(String path, String from)
Adds a "copy" JSON Patch operation.JsonPatchBuilder
move(String path, String from)
Adds a "move" JSON Patch operation.JsonPatchBuilder
remove(String path)
Adds a "remove" JSON Patch operation.JsonPatchBuilder
replace(String path, boolean value)
Adds a "replace" JSON Patch operation.JsonPatchBuilder
replace(String path, int value)
Adds a "replace" JSON Patch operation.JsonPatchBuilder
replace(String path, JsonValue value)
Adds a "replace" JSON Patch operation.JsonPatchBuilder
replace(String path, String value)
Adds a "replace" JSON Patch operation.JsonPatchBuilder
test(String path, boolean value)
Adds a "test" JSON Patch operation.JsonPatchBuilder
test(String path, int value)
Adds a "test" JSON Patch operation.JsonPatchBuilder
test(String path, JsonValue value)
Adds a "test" JSON Patch operation.JsonPatchBuilder
test(String path, String value)
Adds a "test" JSON Patch operation.
-
-
-
Constructor Detail
-
JsonPatchBuilderImpl
public JsonPatchBuilderImpl(JsonArray patch)
Creates a JsonPatchBuilderImpl, starting with the specified JSON Patch- Parameters:
patch
- the JSON Patch
-
JsonPatchBuilderImpl
public JsonPatchBuilderImpl()
Creates JsonPatchBuilderImpl with empty JSON Patch
-
-
Method Detail
-
apply
public <T extends JsonStructure> T apply(T target)
A convenience method fornew JsonPatchImpl(build()).apply(target)
. The target is not modified by the patch.- Type Parameters:
T
- the target type, must be a subtype ofJsonStructure
- Parameters:
target
- the target to apply the patch operations- Returns:
- the transformed target after the patch
- Throws:
JsonException
- if the supplied JSON Patch is malformed or if it contains references to non-existing members
-
add
public JsonPatchBuilder add(String path, JsonValue value)
Adds an "add" JSON Patch operation.- Specified by:
add
in interfaceJsonPatchBuilder
- Parameters:
path
- the "path" member of the operationvalue
- the "value" member of the operation- Returns:
- this JsonPatchBuilder
-
add
public JsonPatchBuilder add(String path, String value)
Adds an "add" JSON Patch operation- Specified by:
add
in interfaceJsonPatchBuilder
- Parameters:
path
- the "path" member of the operationvalue
- the "value" member of the operation- Returns:
- this JsonPatchBuilder
-
add
public JsonPatchBuilder add(String path, int value)
Adds an "add" JSON Patch operation- Specified by:
add
in interfaceJsonPatchBuilder
- Parameters:
path
- the "path" member of the operationvalue
- the "value" member of the operation- Returns:
- this JsonPatchBuilder
-
add
public JsonPatchBuilder add(String path, boolean value)
Adds an "add" JSON Patch operation- Specified by:
add
in interfaceJsonPatchBuilder
- Parameters:
path
- the "path" member of the operationvalue
- the "value" member of the operation- Returns:
- this JsonPatchBuilder
-
remove
public JsonPatchBuilder remove(String path)
Adds a "remove" JSON Patch operation.- Specified by:
remove
in interfaceJsonPatchBuilder
- Parameters:
path
- the "path" member of the operation- Returns:
- this JsonPatchBuilder
-
replace
public JsonPatchBuilder replace(String path, JsonValue value)
Adds a "replace" JSON Patch operation.- Specified by:
replace
in interfaceJsonPatchBuilder
- Parameters:
path
- the "path" member of the operationvalue
- the "value" member of the operation- Returns:
- this JsonPatchBuilder
-
replace
public JsonPatchBuilder replace(String path, String value)
Adds a "replace" JSON Patch operation.- Specified by:
replace
in interfaceJsonPatchBuilder
- Parameters:
path
- the "path" member of the operationvalue
- the "value" member of the operation- Returns:
- this JsonPatchBuilder
-
replace
public JsonPatchBuilder replace(String path, int value)
Adds a "replace" JSON Patch operation.- Specified by:
replace
in interfaceJsonPatchBuilder
- Parameters:
path
- the "path" member of the operationvalue
- the "value" member of the operation- Returns:
- this JsonPatchBuilder
-
replace
public JsonPatchBuilder replace(String path, boolean value)
Adds a "replace" JSON Patch operation.- Specified by:
replace
in interfaceJsonPatchBuilder
- Parameters:
path
- the "path" member of the operationvalue
- the "value" member of the operation- Returns:
- this JsonPatchBuilder
-
move
public JsonPatchBuilder move(String path, String from)
Adds a "move" JSON Patch operation.- Specified by:
move
in interfaceJsonPatchBuilder
- Parameters:
path
- the "path" member of the operationfrom
- the "from" member of the operation- Returns:
- this JsonPatchBuilder
-
copy
public JsonPatchBuilder copy(String path, String from)
Adds a "copy" JSON Patch operation.- Specified by:
copy
in interfaceJsonPatchBuilder
- Parameters:
path
- the "path" member of the operationfrom
- the "from" member of the operation- Returns:
- this JsonPatchBuilder
-
test
public JsonPatchBuilder test(String path, JsonValue value)
Adds a "test" JSON Patch operation.- Specified by:
test
in interfaceJsonPatchBuilder
- Parameters:
path
- the "path" member of the operationvalue
- the "value" member of the operation- Returns:
- this JsonPatchBuilder
-
test
public JsonPatchBuilder test(String path, String value)
Adds a "test" JSON Patch operation.- Specified by:
test
in interfaceJsonPatchBuilder
- Parameters:
path
- the "path" member of the operationvalue
- the "value" member of the operation- Returns:
- this JsonPatchBuilder
-
test
public JsonPatchBuilder test(String path, int value)
Adds a "test" JSON Patch operation.- Specified by:
test
in interfaceJsonPatchBuilder
- Parameters:
path
- the "path" member of the operationvalue
- the "value" member of the operation- Returns:
- this JsonPatchBuilder
-
test
public JsonPatchBuilder test(String path, boolean value)
Adds a "test" JSON Patch operation.- Specified by:
test
in interfaceJsonPatchBuilder
- Parameters:
path
- the "path" member of the operationvalue
- the "value" member of the operation- Returns:
- this JsonPatchBuilder
-
buildAsJsonArray
public JsonArray buildAsJsonArray()
Returns the patch operations in a JsonArray- Returns:
- the patch operations in a JsonArray
-
build
public JsonPatch build()
Returns the patch operation in a JsonPatch- Specified by:
build
in interfaceJsonPatchBuilder
- Returns:
- the patch operation in a JsonPatch
-
-