Class Node

java.lang.Object
software.amazon.smithy.model.node.Node
All Implemented Interfaces:
FromSourceLocation, ToNode
Direct Known Subclasses:
ArrayNode, BooleanNode, NullNode, NumberNode, ObjectNode, StringNode

public abstract class Node extends Object implements FromSourceLocation, ToNode
Base class of for all Smithy model nodes.

When loading a Smithy model the data is loaded from the source model file into a tree of nodes. These nodes represent the unvalidated structure of the model document.

  • Method Details

    • parse

      public static Node parse(String json)
      Attempts to parse the given JSON string and return a Node.
      Parameters:
      json - JSON text to parse.
      Returns:
      Returns the parsed Node on success.
      Throws:
      ModelSyntaxException - if the JSON text is invalid.
    • parse

      public static Node parse(String json, String file)
      Attempts to parse the given JSON string and File Name and return a Node.
      Parameters:
      json - JSON text to parse.
      file - Filename corresponding to json text
      Returns:
      Returns the parsed Node on success.
      Throws:
      ModelSyntaxException - if the JSON text is invalid.
    • parse

      public static Node parse(InputStream json)
      Attempts to parse the given JSON input stream and returns a Node.
      Parameters:
      json - JSON input stream to parse. The input stream is closed automatically when the content is fully parsed.
      Returns:
      Returns the parsed Node on success.
      Throws:
      ModelSyntaxException - if the JSON text is invalid.
    • parse

      public static Node parse(InputStream json, String file)
      Attempts to parse the given JSON input stream and returns a Node.
      Parameters:
      json - JSON input stream to parse. The input stream is closed automatically when the content is fully parsed.
      file - Filename corresponding to json text
      Returns:
      Returns the parsed Node on success.
      Throws:
      ModelSyntaxException - if the JSON text is invalid.
    • parseJsonWithComments

      public static Node parseJsonWithComments(String json, String file)
      Attempts to parse the given JSON string and File Name and return a Node.

      This parser allows for comments in the JSON.

      Parameters:
      json - JSON text to parse.
      file - Filename corresponding to json text
      Returns:
      Returns the parsed Node on success.
      Throws:
      ModelSyntaxException - if the JSON text is invalid.
    • parseJsonWithComments

      public static Node parseJsonWithComments(String json)
      Attempts to parse the given JSON string and return a Node.

      This parser allows for comments in the JSON.

      Parameters:
      json - JSON text to parse.
      Returns:
      Returns the parsed Node on success.
      Throws:
      ModelSyntaxException - if the JSON text is invalid.
    • prettyPrintJson

      public static String prettyPrintJson(Node node)
      Writes the contents of a Node to a pretty-printed JSON string.
      Parameters:
      node - Node to write.
      Returns:
      Returns the serialized Node.
    • prettyPrintJson

      public static String prettyPrintJson(Node node, String indentString)
      Writes the contents of a Node to a pretty-printed JSON string.
      Parameters:
      node - Node to write.
      indentString - String to use for indention.
      Returns:
      Returns the serialized Node.
    • printJson

      public static String printJson(Node node)
      Writes the contents of a Node to a non-pretty-printed JSON string.
      Parameters:
      node - Node to write.
      Returns:
      Returns the serialized Node.
    • from

      public static StringNode from(String value)
      Create a StringNode from a String value.
      Parameters:
      value - Value to create a node from.
      Returns:
      Returns the created StringNode.
    • from

      public static NumberNode from(Number number)
      Create a NumberNode from a Number value.
      Parameters:
      number - Value to create a node from.
      Returns:
      Returns the created NumberNode.
    • from

      public static BooleanNode from(boolean value)
      Create a BooleanNode from a boolean value.
      Parameters:
      value - Value to create a node from.
      Returns:
      Returns the created BooleanNode.
    • from

      public static Node from(ToNode value)
      Create a Node from a potentially null ToNode value.
      Parameters:
      value - Value to create a node from.
      Returns:
      Returns the created Node.
    • fromNodes

      public static ArrayNode fromNodes(List<? extends Node> values)
      Creates an ArrayNode from a Collection of Node values.
      Parameters:
      values - String values to add to the ArrayNode.
      Returns:
      Returns the created ArrayNode.
    • fromNodes

      public static ArrayNode fromNodes(Node... values)
      Creates an ArrayNode from a variadic list of Node values.
      Parameters:
      values - String values to add to the ArrayNode.
      Returns:
      Returns the created ArrayNode.
    • fromStrings

      public static ArrayNode fromStrings(Collection<String> values)
      Creates an ArrayNode from a Collection of String values.
      Parameters:
      values - String values to add to the ArrayNode.
      Returns:
      Returns the created ArrayNode.
    • fromStrings

      public static ArrayNode fromStrings(String... values)
      Creates an ArrayNode from a variadic list of String values.
      Parameters:
      values - String values to add to the ArrayNode.
      Returns:
      Returns the created ArrayNode.
    • objectNodeBuilder

      public static ObjectNode.Builder objectNodeBuilder()
      Creates an ObjectNode.Builder.
      Returns:
      Returns the ObjectNode builder.
    • objectNode

      public static ObjectNode objectNode()
      Creates an empty ObjectNode.
      Returns:
      Returns the ObjectNode.
    • objectNode

      public static ObjectNode objectNode(Map<StringNode,Node> values)
      Creates an ObjectNode from the given map of Nodes.
      Parameters:
      values - Values to add to the object node.
      Returns:
      Returns the created ObjectNode.
    • arrayNode

      public static ArrayNode arrayNode()
      Creates an empty ArrayNode.
      Returns:
      Returns the ArrayNode.
    • arrayNode

      public static ArrayNode arrayNode(Node... nodes)
      Creates an ArrayNode from a variadic list of Nodes.
      Parameters:
      nodes - Nodes to add to the array.
      Returns:
      Returns the created ArrayNode.
    • nullNode

      public static NullNode nullNode()
      Creates a NullNode.
      Returns:
      Returns the NullNode.
    • loadArrayOfString

      public static List<String> loadArrayOfString(String descriptor, Node node)
      Expects an array of strings and returns the loaded strings.
      Parameters:
      descriptor - Name of the property being loaded.
      node - Node to load.
      Returns:
      Returns the loaded strings.
      Throws:
      SourceException - on error.
    • assertEquals

      public static void assertEquals(ToNode actual, ToNode expected)
      Testing helper used to compare two Nodes for equivalence.

      Compares two Node values and throws if they aren't equal. The thrown exception contains a message that shows the differences between the two Nodes as returned by diff(ToNode, ToNode).

      Parameters:
      actual - Node to use as the starting node.
      expected - Node to compare against.
      Throws:
      ExpectationNotMetException - if the nodes are not equivalent.
    • diff

      public static List<String> diff(ToNode actual, ToNode expected)
      Computes the differences between two Nodes as a String.
      Parameters:
      actual - Node to use as the starting node.
      expected - Node to compare against.
      Returns:
      Returns the differences as a String.
    • getType

      public abstract NodeType getType()
      Gets the type of the node.
      Returns:
      Returns the node type.
    • accept

      public abstract <R> R accept(NodeVisitor<R> visitor)
      Accepts a visitor with the node.
      Type Parameters:
      R - visitor return type.
      Parameters:
      visitor - Visitor to dispatch to.
      Returns:
      Returns the accepted result.
    • isObjectNode

      public final boolean isObjectNode()
      Checks if this node is an object type.
      Returns:
      Returns true if this node is an object type.
    • isArrayNode

      public final boolean isArrayNode()
      Checks if this node is an array type.
      Returns:
      Returns true if this node is an array type.
    • isStringNode

      public final boolean isStringNode()
      Checks if this node is a string type.
      Returns:
      Returns true if this node is a string type.
    • isNumberNode

      public final boolean isNumberNode()
      Checks if this node is a number type.
      Returns:
      Returns true if this node is a number type.
    • isBooleanNode

      public final boolean isBooleanNode()
      Checks if this node is a boolean type.
      Returns:
      Returns true if this node is a boolean type.
    • isNullNode

      public final boolean isNullNode()
      Checks if this node is a null type.
      Returns:
      Returns true if this node is a null type.
    • asObjectNode

      public Optional<ObjectNode> asObjectNode()
      Gets the node as an ObjectNode if it is an object.
      Returns:
      Returns the optional object node.
    • asArrayNode

      public Optional<ArrayNode> asArrayNode()
      Gets the node as an ArrayNode if it is an array.
      Returns:
      Returns the optional array node.
    • asStringNode

      public Optional<StringNode> asStringNode()
      Gets the node as an StringNode if it is an string.
      Returns:
      Returns the optional StringNode.
    • asBooleanNode

      public Optional<BooleanNode> asBooleanNode()
      Gets the node as an BooleanNode if it is an boolean.
      Returns:
      Returns the optional BooleanNode.
    • asNumberNode

      public Optional<NumberNode> asNumberNode()
      Gets the node as an NumberNode if it is an number.
      Returns:
      Returns the optional NumberNode.
    • asNullNode

      public Optional<NullNode> asNullNode()
      Gets the node as an NullNode if it is a null.
      Returns:
      Returns the optional NullNode.
    • expectObjectNode

      public final ObjectNode expectObjectNode()
      Casts the current node to an ObjectNode.
      Returns:
      Returns an object node.
      Throws:
      ExpectationNotMetException - when the node is not an ObjectNode.
    • expectObjectNode

      public ObjectNode expectObjectNode(String message)
      Casts the current node to an ObjectNode, throwing ExpectationNotMetException when the node is the wrong type.
      Parameters:
      message - Error message to use if the node is of the wrong type.
      Returns:
      Returns an object node.
      Throws:
      ExpectationNotMetException - when the node is not an ObjectNode.
    • expectObjectNode

      public ObjectNode expectObjectNode(Supplier<String> message)
      Casts the current node to an ObjectNode, throwing ExpectationNotMetException when the node is the wrong type.
      Parameters:
      message - Error message supplier.
      Returns:
      Returns an object node.
      Throws:
      ExpectationNotMetException - when the node is not an ObjectNode.
    • expectArrayNode

      public final ArrayNode expectArrayNode()
      Casts the current node to an ArrayNode.
      Returns:
      Returns an array node.
      Throws:
      ExpectationNotMetException - when the node is not an ArrayNode.
    • expectArrayNode

      public ArrayNode expectArrayNode(String message)
      Casts the current node to an ArrayNode, throwing ExpectationNotMetException when the node is the wrong type.
      Parameters:
      message - Error message to use if the node is of the wrong type.
      Returns:
      Returns an array node.
      Throws:
      ExpectationNotMetException - when the node is the wrong type.
    • expectArrayNode

      public ArrayNode expectArrayNode(Supplier<String> message)
      Casts the current node to an ArrayNode, throwing ExpectationNotMetException when the node is the wrong type.
      Parameters:
      message - Error message supplier.
      Returns:
      Returns an array node.
      Throws:
      ExpectationNotMetException - when the node is the wrong type.
    • expectStringNode

      public final StringNode expectStringNode()
      Casts the current node to a StringNode.
      Returns:
      Returns a string node.
      Throws:
      ExpectationNotMetException - when the node is the wrong type.
    • expectStringNode

      public StringNode expectStringNode(String message)
      Casts the current node to a StringNode, throwing ExpectationNotMetException when the node is the wrong type.
      Parameters:
      message - Error message to use if the node is of the wrong type.
      Returns:
      Returns a string node.
      Throws:
      ExpectationNotMetException - when the node is the wrong type.
    • expectStringNode

      public StringNode expectStringNode(Supplier<String> message)
      Casts the current node to a StringNode, throwing ExpectationNotMetException when the node is the wrong type.
      Parameters:
      message - Error message supplier.
      Returns:
      Returns a string node.
      Throws:
      ExpectationNotMetException - when the node is the wrong type.
    • expectNumberNode

      public final NumberNode expectNumberNode()
      Casts the current node to a NumberNode.
      Returns:
      Returns a number node.
      Throws:
      ExpectationNotMetException - when the node is the wrong type.
    • expectNumberNode

      public NumberNode expectNumberNode(String message)
      Casts the current node to a NumberNode, throwing ExpectationNotMetException when the node is the wrong type.
      Parameters:
      message - Error message to use if the node is of the wrong type.
      Returns:
      Returns a number node.
      Throws:
      ExpectationNotMetException - when the node is the wrong type.
    • expectNumberNode

      public NumberNode expectNumberNode(Supplier<String> message)
      Casts the current node to a NumberNode, throwing ExpectationNotMetException when the node is the wrong type.
      Parameters:
      message - Error message supplier.
      Returns:
      Returns a number node.
      Throws:
      ExpectationNotMetException - when the node is the wrong type.
    • expectBooleanNode

      public final BooleanNode expectBooleanNode()
      Casts the current node to a BooleanNode.
      Returns:
      Returns a boolean node.
      Throws:
      ExpectationNotMetException - when the node is the wrong type.
    • expectBooleanNode

      public BooleanNode expectBooleanNode(String message)
      Casts the current node to a BooleanNode, throwing ExpectationNotMetException when the node is the wrong type.
      Parameters:
      message - Error message to use if the node is of the wrong type.
      Returns:
      Returns a boolean node.
      Throws:
      ExpectationNotMetException - when the node is the wrong type.
    • expectBooleanNode

      public BooleanNode expectBooleanNode(Supplier<String> message)
      Casts the current node to a BooleanNode, throwing ExpectationNotMetException when the node is the wrong type.
      Parameters:
      message - Error message supplier.
      Returns:
      Returns a boolean node.
      Throws:
      ExpectationNotMetException - when the node is the wrong type.
    • expectNullNode

      public final NullNode expectNullNode()
      Casts the current node to a NullNode.
      Returns:
      Returns a null node.
      Throws:
      ExpectationNotMetException - when the node is the wrong type.
    • expectNullNode

      public NullNode expectNullNode(String message)
      Casts the current node to a NullNode, throwing ExpectationNotMetException when the node is the wrong type.
      Parameters:
      message - Error message to use if the node is of the wrong type.
      Returns:
      Returns a null node.
      Throws:
      ExpectationNotMetException - when the node is the wrong type.
    • expectNullNode

      public NullNode expectNullNode(Supplier<String> message)
      Casts the current node to a NullNode, throwing ExpectationNotMetException when the node is the wrong type.
      Parameters:
      message - Error message supplier.
      Returns:
      Returns a null node.
      Throws:
      ExpectationNotMetException - when the node is the wrong type.
    • getSourceLocation

      public final SourceLocation getSourceLocation()
      Description copied from interface: FromSourceLocation
      Gets the source location of a value.
      Specified by:
      getSourceLocation in interface FromSourceLocation
      Returns:
      Returns the source location of the value.
    • toNode

      public final Node toNode()
      Description copied from interface: ToNode
      Converts a value to a Node.
      Specified by:
      toNode in interface ToNode
      Returns:
      Returns the creates Node.
    • withDeepSortedKeys

      public final Node withDeepSortedKeys()
      Returns a node with sorted keys and sorted keys of all nested object nodes.
      Returns:
      Returns the node in which all object nodes have sorted keys.
    • withDeepSortedKeys

      public final Node withDeepSortedKeys(Comparator<StringNode> keyComparator)
      Returns a node with sorted keys and sorted keys of all nested object nodes using a custom comparator.
      Parameters:
      keyComparator - Compares keys.
      Returns:
      Returns the node in which all object nodes have sorted keys.