Class NodePointer


  • public final class NodePointer
    extends java.lang.Object
    JSON Pointer abstraction over Smithy Node values.

    A parsed JSON pointer can get a value from a Node by pointer and perform JSON-patch like operations like adding a value to a specific pointer target.

    See Also:
    RFC 6902
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Node addValue​(Node container, Node value)
      Adds or replaces a value in container at the JSON pointer location.
      Node addWithIntermediateValues​(Node container, Node value)
      Adds or replaces a value in container at the JSON pointer location.
      static NodePointer empty()
      Gets an empty Node pointer.
      boolean equals​(java.lang.Object other)  
      static NodePointer fromNode​(Node node)
      Creates a NodePointer from a Node value.
      java.util.List<java.lang.String> getParts()
      Gets the parsed parts of the pointer.
      Node getValue​(Node container)
      Gets a value from a container shape at the pointer location.
      int hashCode()  
      static NodePointer parse​(java.lang.String pointer)
      Parses a JSON pointer.
      java.lang.String toString()  
      static java.lang.String unescape​(java.lang.String pointerPart)
      Unescapes special JSON pointer cases.
      • Methods inherited from class java.lang.Object

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

      • empty

        public static NodePointer empty()
        Gets an empty Node pointer.
        Returns:
        Returns a node pointer with a value of "".
      • fromNode

        public static NodePointer fromNode​(Node node)
        Creates a NodePointer from a Node value.
        Parameters:
        node - Node value to parse.
        Returns:
        Returns the parsed NodePointer.
        Throws:
        ExpectationNotMetException - if the pointer cannot be parsed.
      • unescape

        public static java.lang.String unescape​(java.lang.String pointerPart)
        Unescapes special JSON pointer cases.
        Parameters:
        pointerPart - Pointer to unescape.
        Returns:
        Returns the unescaped pointer.
      • parse

        public static NodePointer parse​(java.lang.String pointer)
        Parses a JSON pointer.

        A JSON pointer that starts with "#/" is treated as "/". JSON pointers must start with "#/" or "/" to be parsed correctly.

        Parameters:
        pointer - JSON pointer to parse.
        Returns:
        Returns the parsed pointer.
        Throws:
        java.lang.IllegalArgumentException - if the pointer does not start with slash (/).
      • getParts

        public java.util.List<java.lang.String> getParts()
        Gets the parsed parts of the pointer.
        Returns:
        Returns the immutable pointer parts.
      • toString

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

        public boolean equals​(java.lang.Object other)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • getValue

        public Node getValue​(Node container)
        Gets a value from a container shape at the pointer location.

        When the pointer is "", then provided value is returned. When the pointer is "/", a root object key value of "" is returned. When an invalid property or array index is accessed, a NullNode is returned. "-" can be used to access the last element of an array. Any error like accessing an object key from an array or attempting to access an invalid array index will return a NullNode.

        Parameters:
        container - Node value container to extract a value from.
        Returns:
        Returns the extracted value or a NullNode if not found.
      • addValue

        public Node addValue​(Node container,
                             Node value)
        Adds or replaces a value in container at the JSON pointer location.

        When the JSON pointer is "", the entire document is replaced with the given value. "-" can be used to access the last element of an array or to add an element to the end of an array. Any error like adding an object key to an array or attempting to access an invalid array segment will log a warning and return the given value as-is.

        Parameters:
        container - Node to update.
        value - Value to update or replace.
        Returns:
        Returns a representation of container with the updated value.
      • addWithIntermediateValues

        public Node addWithIntermediateValues​(Node container,
                                              Node value)
        Adds or replaces a value in container at the JSON pointer location.

        When the JSON pointer is "", the entire document is replaced with the given value. "-" can be used to access the last element of an array or to add an element to the end of an array. Unlike addValue(Node, Node), attempting to add a property to a non-existent object will create a new object and continue adding to the created result.

        Parameters:
        container - Node to update.
        value - Value to update or replace.
        Returns:
        Returns a representation of container with the updated value.