Class JsonPatchDocument


  • public final class JsonPatchDocument
    extends Object
    Represents a JSON Patch document.
    • Constructor Detail

      • JsonPatchDocument

        public JsonPatchDocument()
        Creates a new JSON Patch document.
      • JsonPatchDocument

        public JsonPatchDocument​(JsonSerializer serializer)
        Creates a new JSON Patch document.

        If serializer isn't specified JacksonAdapter will be used.

        Parameters:
        serializer - The JsonSerializer that will be used to serialize patch operation values.
    • Method Detail

      • appendAdd

        public JsonPatchDocument appendAdd​(String path,
                                           Object value)
        Appends an "add" operation to this JSON Patch document.

        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");
         
        Parameters:
        path - The path to apply the addition.
        value - The value that will be serialized and added to the path.
        Returns:
        The updated JsonPatchDocument object.
        Throws:
        NullPointerException - If path is null.
      • appendAddRaw

        public JsonPatchDocument appendAddRaw​(String path,
                                              String rawJson)
        Appends an "add" operation to this JSON Patch document.

        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\"");
         
        Parameters:
        path - The path to apply the addition.
        rawJson - The raw JSON value that will be added to the path.
        Returns:
        The updated JsonPatchDocument object.
        Throws:
        NullPointerException - If path is null.
      • appendReplace

        public JsonPatchDocument appendReplace​(String path,
                                               Object value)
        Appends a "replace" operation to this JSON Patch document.

        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");
         
        Parameters:
        path - The path to replace.
        value - The value will be serialized and used as the replacement.
        Returns:
        The updated JsonPatchDocument object.
        Throws:
        NullPointerException - If path is null.
      • appendReplaceRaw

        public JsonPatchDocument appendReplaceRaw​(String path,
                                                  String rawJson)
        Appends a "replace" operation to this JSON Patch document.

        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\"");
         
        Parameters:
        path - The path to replace.
        rawJson - The raw JSON value that will be used as the replacement.
        Returns:
        The updated JsonPatchDocument object.
        Throws:
        NullPointerException - If path is null.
      • appendCopy

        public JsonPatchDocument appendCopy​(String from,
                                            String path)
        Appends a "copy" operation to this JSON Patch document.

        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");
         
        Parameters:
        from - The path to copy from.
        path - The path to copy to.
        Returns:
        The updated JsonPatchDocument object.
        Throws:
        NullPointerException - If from or path is null.
      • appendMove

        public JsonPatchDocument appendMove​(String from,
                                            String path)
        Appends a "move" operation to this JSON Patch document.

        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");
         
        Parameters:
        from - The path to move from.
        path - The path to move to.
        Returns:
        The updated JsonPatchDocument object.
        Throws:
        NullPointerException - If from or path is null.
      • appendRemove

        public JsonPatchDocument appendRemove​(String path)
        Appends a "remove" operation to this JSON Patch document.

        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");
         
        Parameters:
        path - The path to remove.
        Returns:
        The updated JsonPatchDocument object.
        Throws:
        NullPointerException - If path is null.
      • appendTest

        public JsonPatchDocument appendTest​(String path,
                                            Object value)
        Appends a "test" operation to this JSON Patch document.

        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");
         
        Parameters:
        path - The path to test.
        value - The value that will be serialized and used to test against.
        Returns:
        The updated JsonPatchDocument object.
        Throws:
        NullPointerException - If path is null.
      • appendTestRaw

        public JsonPatchDocument appendTestRaw​(String path,
                                               String rawJson)
        Appends a "test" operation to this JSON Patch document.

        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\"");
         
        Parameters:
        path - The path to test.
        rawJson - The raw JSON value that will be used to test against.
        Returns:
        The updated JsonPatchDocument object.
        Throws:
        NullPointerException - If path is null.
      • toString

        public String toString()
        Gets a formatted JSON string representation of this JSON Patch document.
        Overrides:
        toString in class Object
        Returns:
        The formatted JSON String representing this JSON Patch docuemnt.