Class JsonPatchImpl

  • All Implemented Interfaces:
    jakarta.json.JsonPatch

    public class JsonPatchImpl
    extends java.lang.Object
    implements jakarta.json.JsonPatch
    This class is an immutable representation of a JSON Patch as specified in RFC 6902.

    A JsonPatch can be instantiated with Json.createPatch(JsonArray) by specifying the patch operations in a JSON Patch. Alternately, it can also be constructed with a JsonPatchBuilder.

    The following illustrates both approaches.

    1. Construct a JsonPatch with a JSON Patch.

    
       JsonArray contacts = ... // The target to be patched
       JsonArray patch = ...  ; // JSON Patch
       JsonPatch jsonpatch = Json.createPatch(patch);
       JsonArray result = jsonpatch.apply(contacts);
      
    2. Construct a JsonPatch with JsonPatchBuilder.
    
       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 jakarta.json.JsonPatch

        jakarta.json.JsonPatch.Operation
    • Constructor Summary

      Constructors 
      Constructor Description
      JsonPatchImpl​(jakarta.json.JsonArray patch, JsonContext jsonContext)
      Constructs a JsonPatchImpl
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      jakarta.json.JsonStructure apply​(jakarta.json.JsonStructure target)
      Applies the patch operations to the specified target.
      private jakarta.json.JsonStructure apply​(jakarta.json.JsonStructure target, jakarta.json.JsonObject operation)
      Applies a JSON Patch operation to the target.
      (package private) static jakarta.json.JsonArray diff​(jakarta.json.JsonStructure source, jakarta.json.JsonStructure target, JsonContext jsonContext)
      Generates a JSON Patch from the source and target JsonStructure.
      boolean equals​(java.lang.Object obj)
      Compares this JsonPatchImpl with another object.
      private jakarta.json.JsonPointer getPointer​(jakarta.json.JsonObject operation, java.lang.String member)  
      private jakarta.json.JsonValue getValue​(jakarta.json.JsonObject operation)  
      int hashCode()
      Returns the hash code value for this JsonPatchImpl.
      private void missingMember​(java.lang.String op, java.lang.String member)  
      jakarta.json.JsonArray toJsonArray()  
      java.lang.String toString()
      Returns the JSON Patch text
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • patch

        private final jakarta.json.JsonArray patch
    • Constructor Detail

      • JsonPatchImpl

        public JsonPatchImpl​(jakarta.json.JsonArray patch,
                             JsonContext jsonContext)
        Constructs a JsonPatchImpl
        Parameters:
        patch - the JSON Patch
    • Method Detail

      • equals

        public boolean equals​(java.lang.Object obj)
        Compares this JsonPatchImpl with another object.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        obj - the object to compare this JsonPatchImpl 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 this JsonPatchImpl.
        Overrides:
        hashCode in class java.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 class java.lang.Object
        Returns:
        the JSON Patch text
      • apply

        public jakarta.json.JsonStructure apply​(jakarta.json.JsonStructure target)
        Applies the patch operations to the specified target. The target is not modified by the patch.
        Specified by:
        apply in interface jakarta.json.JsonPatch
        Parameters:
        target - the target to apply the patch operations
        Returns:
        the transformed target after the patch
        Throws:
        jakarta.json.JsonException - if the supplied JSON Patch is malformed or if it contains references to non-existing members
      • toJsonArray

        public jakarta.json.JsonArray toJsonArray()
        Specified by:
        toJsonArray in interface jakarta.json.JsonPatch
      • diff

        static jakarta.json.JsonArray diff​(jakarta.json.JsonStructure source,
                                           jakarta.json.JsonStructure target,
                                           JsonContext jsonContext)
        Generates a JSON Patch from the source and target JsonStructure. The generated JSON Patch need not be unique.
        Parameters:
        source - the source
        target - 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 jakarta.json.JsonStructure apply​(jakarta.json.JsonStructure target,
                                                 jakarta.json.JsonObject operation)
        Applies a JSON Patch operation to the target.
        Parameters:
        target - the target to apply the operation
        operation - the JSON Patch operation
        Returns:
        the target after the patch
      • getPointer

        private jakarta.json.JsonPointer getPointer​(jakarta.json.JsonObject operation,
                                                    java.lang.String member)
      • getValue

        private jakarta.json.JsonValue getValue​(jakarta.json.JsonObject operation)
      • missingMember

        private void missingMember​(java.lang.String op,
                                   java.lang.String member)