Class NodePointer

java.lang.Object
software.amazon.smithy.model.node.NodePointer

public final class NodePointer extends 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:
  • Method Details

    • 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 String unescape(String pointerPart)
      Unescapes special JSON pointer cases.
      Parameters:
      pointerPart - Pointer to unescape.
      Returns:
      Returns the unescaped pointer.
    • parse

      public static NodePointer parse(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:
      IllegalArgumentException - if the pointer does not start with slash (/).
    • getParts

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

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

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class 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.