Class JsonPatch

  • All Implemented Interfaces:
    com.fasterxml.jackson.databind.JsonSerializable, Patch

    public final class JsonPatch
    extends java.lang.Object
    implements com.fasterxml.jackson.databind.JsonSerializable, Patch
    Implementation of JSON Patch

    JSON Patch, as its name implies, is an IETF RFC describing a mechanism to apply a patch to any JSON value. This implementation covers all operations according to the specification; however, there are some subtle differences with regards to some operations which are covered in these operations' respective documentation.

    An example of a JSON Patch is as follows:

         [
             {
                 "op": "add",
                 "path": "/-",
                 "value": {
                     "productId": 19,
                     "name": "Duvel",
                     "type": "beer"
                 }
             }
         ]
     

    This patch contains a single operation which adds an item at the end of an array. A JSON Patch can contain more than one operation; in this case, all operations are applied to the input JSON value in their order of appearance, until all operations are applied or an error condition is encountered.

    The main point where this implementation differs from the specification is initial JSON parsing. The draft says:

         Operation objects MUST have exactly one "op" member
     

    and:

         Additionally, operation objects MUST have exactly one "path" member.
     

    However, obeying these to the letter forces constraints on the JSON parser. Here, these constraints are not enforced, which means:

         [ { "op": "add", "op": "remove", "path": "/x" } ]
     

    is parsed (as a remove operation, since it appears last).

    IMPORTANT NOTE: the JSON Patch is supposed to be VALID when the constructor for this class (fromJson(JsonNode) is used.

    • Nested Class Summary

      • Nested classes/interfaces inherited from interface com.fasterxml.jackson.databind.JsonSerializable

        com.fasterxml.jackson.databind.JsonSerializable.Base
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      com.fasterxml.jackson.databind.JsonNode apply​(com.fasterxml.jackson.databind.JsonNode node)
      Apply this patch to a JSON value
      static JsonPatch fromJson​(com.fasterxml.jackson.databind.JsonNode node)
      Static factory method to build a JSON Patch out of a JSON representation
      java.util.List<JsonPatchOperation> getOperations()  
      void serialize​(com.fasterxml.jackson.core.JsonGenerator jgen, com.fasterxml.jackson.databind.SerializerProvider provider)  
      void serializeWithType​(com.fasterxml.jackson.core.JsonGenerator jgen, com.fasterxml.jackson.databind.SerializerProvider provider, com.fasterxml.jackson.databind.jsontype.TypeSerializer typeSer)  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • JsonPatch

        public JsonPatch​(java.util.List<JsonPatchOperation> operations)
        Constructor

        Normally, you should never have to use it.

        Parameters:
        operations - the list of operations for this patch
        See Also:
        JsonPatchOperation
    • Method Detail

      • fromJson

        public static JsonPatch fromJson​(com.fasterxml.jackson.databind.JsonNode node)
                                  throws java.io.IOException
        Static factory method to build a JSON Patch out of a JSON representation
        Parameters:
        node - the JSON representation of the generated JSON Patch
        Returns:
        a JSON Patch
        Throws:
        java.io.IOException - input is not a valid JSON patch
        java.lang.NullPointerException - input is null
      • apply

        public com.fasterxml.jackson.databind.JsonNode apply​(com.fasterxml.jackson.databind.JsonNode node)
                                                      throws JsonPatchException
        Apply this patch to a JSON value
        Specified by:
        apply in interface Patch
        Parameters:
        node - the value to apply the patch to
        Returns:
        the patched JSON value
        Throws:
        JsonPatchException - failed to apply patch
        java.lang.NullPointerException - input is null
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • serialize

        public void serialize​(com.fasterxml.jackson.core.JsonGenerator jgen,
                              com.fasterxml.jackson.databind.SerializerProvider provider)
                       throws java.io.IOException
        Specified by:
        serialize in interface com.fasterxml.jackson.databind.JsonSerializable
        Throws:
        java.io.IOException
      • serializeWithType

        public void serializeWithType​(com.fasterxml.jackson.core.JsonGenerator jgen,
                                      com.fasterxml.jackson.databind.SerializerProvider provider,
                                      com.fasterxml.jackson.databind.jsontype.TypeSerializer typeSer)
                               throws java.io.IOException
        Specified by:
        serializeWithType in interface com.fasterxml.jackson.databind.JsonSerializable
        Throws:
        java.io.IOException