Package org.glassfish.json
Class JsonPatchImpl
- java.lang.Object
-
- org.glassfish.json.JsonPatchImpl
-
- All Implemented Interfaces:
JsonPatch
public class JsonPatchImpl extends java.lang.Object implements JsonPatch
This class is an immutable representation of a JSON Patch as specified in RFC 6902.A
The following illustrates both approaches.JsonPatch
can be instantiated withJson.createPatch(JsonArray)
by specifying the patch operations in a JSON Patch. Alternately, it can also be constructed with aJsonPatchBuilder
.1. Construct a JsonPatch with a JSON Patch.
2. Construct a JsonPatch with JsonPatchBuilder.JsonArray contacts = ... // The target to be patched JsonArray patch = ... ; // JSON Patch JsonPatch jsonpatch = Json.createPatch(patch); JsonArray result = jsonpatch.apply(contacts);
JsonPatchBuilder builder = Json.createPatchBuilder(); JsonArray result = builder.add("/John/phones/office", "1234-567") .remove("/Amy/age") .build() .apply(contacts);
- Since:
- 1.1
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
JsonPatchImpl.DiffGenerator
-
Nested classes/interfaces inherited from interface javax.json.JsonPatch
JsonPatch.Operation
-
-
Constructor Summary
Constructors Constructor Description JsonPatchImpl(JsonArray patch)
Constructs a JsonPatchImpl
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description JsonStructure
apply(JsonStructure target)
Applies the patch operations to the specifiedtarget
.private JsonStructure
apply(JsonStructure target, JsonObject operation)
Applies a JSON Patch operation to the target.static JsonArray
diff(JsonStructure source, JsonStructure target)
Generates a JSON Patch from the source and targetJsonStructure
.boolean
equals(java.lang.Object obj)
Compares thisJsonPatchImpl
with another object.private JsonPointer
getPointer(JsonObject operation, java.lang.String member)
private JsonValue
getValue(JsonObject operation)
int
hashCode()
Returns the hash code value for thisJsonPatchImpl
.private void
missingMember(java.lang.String op, java.lang.String member)
JsonArray
toJsonArray()
Returns theJsonPatch
asJsonArray
.java.lang.String
toString()
Returns the JSON Patch text
-
-
-
Field Detail
-
patch
private final JsonArray patch
-
-
Constructor Detail
-
JsonPatchImpl
public JsonPatchImpl(JsonArray patch)
Constructs a JsonPatchImpl- Parameters:
patch
- the JSON Patch
-
-
Method Detail
-
equals
public boolean equals(java.lang.Object obj)
Compares thisJsonPatchImpl
with another object.- Overrides:
equals
in classjava.lang.Object
- Parameters:
obj
- the object to compare thisJsonPatchImpl
against- Returns:
- true if the given object is a
JsonPatchImpl
with the same reference tokens as this one, false otherwise.
-
hashCode
public int hashCode()
Returns the hash code value for thisJsonPatchImpl
.- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- the hash code value for this
JsonPatchImpl
object
-
toString
public java.lang.String toString()
Returns the JSON Patch text- Overrides:
toString
in classjava.lang.Object
- Returns:
- the JSON Patch text
-
apply
public JsonStructure apply(JsonStructure target)
Applies the patch operations to the specifiedtarget
. The target is not modified by the patch.- Specified by:
apply
in interfaceJsonPatch
- 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
-
toJsonArray
public JsonArray toJsonArray()
Description copied from interface:JsonPatch
Returns theJsonPatch
asJsonArray
.- Specified by:
toJsonArray
in interfaceJsonPatch
- Returns:
- this
JsonPatch
asJsonArray
-
diff
public static JsonArray diff(JsonStructure source, JsonStructure target)
Generates a JSON Patch from the source and targetJsonStructure
. The generated JSON Patch need not be unique.- Parameters:
source
- the sourcetarget
- the target, must be the same type as the source- Returns:
- a JSON Patch which when applied to the source, yields the target
-
apply
private JsonStructure apply(JsonStructure target, JsonObject operation)
Applies a JSON Patch operation to the target.- Parameters:
target
- the target to apply the operationoperation
- the JSON Patch operation- Returns:
- the target after the patch
-
getPointer
private JsonPointer getPointer(JsonObject operation, java.lang.String member)
-
getValue
private JsonValue getValue(JsonObject operation)
-
missingMember
private void missingMember(java.lang.String op, java.lang.String member)
-
-