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
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumNon-numeric values for floats and doubles. -
Method Summary
Modifier and TypeMethodDescriptionabstract <R> Raccept(NodeVisitor<R> visitor) Accepts a visitor with the node.static ArrayNodeCreates an emptyArrayNode.static ArrayNodeCreates anArrayNodefrom a variadic list of Nodes.Gets the node as an ArrayNode if it is an array.Gets the node as an BooleanNode if it is an boolean.Gets the node as an NullNode if it is a null.Gets the node as an NumberNode if it is an number.Gets the node as an ObjectNode if it is an object.static voidassertEquals(ToNode actual, ToNode expected) Testing helper used to compare two Nodes for equivalence.Gets the node as an StringNode if it is an string.Computes the differences between two Nodes as a String.final ArrayNodeCasts the current node to anArrayNode.expectArrayNode(String message) Casts the current node to anArrayNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.expectArrayNode(Supplier<String> message) Casts the current node to anArrayNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.final BooleanNodeCasts the current node to aBooleanNode.expectBooleanNode(String message) Casts the current node to aBooleanNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.expectBooleanNode(Supplier<String> message) Casts the current node to aBooleanNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.final NullNodeCasts the current node to aNullNode.expectNullNode(String message) Casts the current node to aNullNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.expectNullNode(Supplier<String> message) Casts the current node to aNullNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.final NumberNodeCasts the current node to aNumberNode.expectNumberNode(String message) Casts the current node to aNumberNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.expectNumberNode(Supplier<String> message) Casts the current node to aNumberNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.final ObjectNodeCasts the current node to anObjectNode.expectObjectNode(String message) Casts the current node to anObjectNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.expectObjectNode(Supplier<String> message) Casts the current node to anObjectNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.final StringNodeCasts the current node to aStringNode.expectStringNode(String message) Casts the current node to aStringNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.expectStringNode(Supplier<String> message) Casts the current node to aStringNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.static BooleanNodefrom(boolean value) Create aBooleanNodefrom a boolean value.static NumberNodeCreate aNumberNodefrom a Number value.static StringNodeCreate aStringNodefrom a String value.static NodeCreate a Node from a potentially nullToNodevalue.static ArrayNodeCreates anArrayNodefrom a Collection of Node values.static ArrayNodeCreates anArrayNodefrom a variadic list of Node values.static ArrayNodefromStrings(String... values) Creates anArrayNodefrom a variadic list of String values.static ArrayNodefromStrings(Collection<String> values) Creates anArrayNodefrom a Collection of String values.final SourceLocationGets the source location of a value.abstract NodeTypegetType()Gets the type of the node.final booleanChecks if this node is an array type.final booleanChecks if this node is a boolean type.final booleanChecks if this node is a null type.final booleanChecks if this node is a number type.final booleanChecks if this node is an object type.final booleanChecks if this node is a string type.loadArrayOfString(String descriptor, Node node) Expects an array of strings and returns the loaded strings.static NullNodenullNode()Creates aNullNode.static ObjectNodeCreates an emptyObjectNode.static ObjectNodeobjectNode(Map<StringNode, Node> values) Creates anObjectNodefrom the given map of Nodes.static ObjectNode.BuilderCreates anObjectNode.Builder.static Nodeparse(InputStream json) Attempts to parse the given JSON input stream and returns a Node.static Nodeparse(InputStream json, String file) Attempts to parse the given JSON input stream and returns a Node.static NodeAttempts to parse the given JSON string and return a Node.static NodeAttempts to parse the given JSON string and File Name and return a Node.static NodeparseJsonWithComments(String json) Attempts to parse the given JSON string and return a Node.static NodeparseJsonWithComments(String json, String file) Attempts to parse the given JSON string and File Name and return a Node.static StringprettyPrintJson(Node node) Writes the contents of a Node to a pretty-printed JSON string.static StringprettyPrintJson(Node node, String indentString) Writes the contents of a Node to a pretty-printed JSON string.static StringWrites the contents of a Node to a non-pretty-printed JSON string.final NodetoNode()Converts a value to aNode.final NodeReturns a node with sorted keys and sorted keys of all nested object nodes.final NodewithDeepSortedKeys(Comparator<StringNode> keyComparator) Returns a node with sorted keys and sorted keys of all nested object nodes using a custom comparator.
-
Method Details
-
parse
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
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
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
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
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
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
Writes the contents of a Node to a pretty-printed JSON string.- Parameters:
node- Node to write.- Returns:
- Returns the serialized Node.
-
prettyPrintJson
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
Writes the contents of a Node to a non-pretty-printed JSON string.- Parameters:
node- Node to write.- Returns:
- Returns the serialized Node.
-
from
Create aStringNodefrom a String value.- Parameters:
value- Value to create a node from.- Returns:
- Returns the created StringNode.
-
from
Create aNumberNodefrom a Number value.- Parameters:
number- Value to create a node from.- Returns:
- Returns the created NumberNode.
-
from
Create aBooleanNodefrom a boolean value.- Parameters:
value- Value to create a node from.- Returns:
- Returns the created BooleanNode.
-
from
Create a Node from a potentially nullToNodevalue.- Parameters:
value- Value to create a node from.- Returns:
- Returns the created Node.
-
fromNodes
Creates anArrayNodefrom a Collection of Node values.- Parameters:
values- String values to add to the ArrayNode.- Returns:
- Returns the created ArrayNode.
-
fromNodes
Creates anArrayNodefrom a variadic list of Node values.- Parameters:
values- String values to add to the ArrayNode.- Returns:
- Returns the created ArrayNode.
-
fromStrings
Creates anArrayNodefrom a Collection of String values.- Parameters:
values- String values to add to the ArrayNode.- Returns:
- Returns the created ArrayNode.
-
fromStrings
Creates anArrayNodefrom a variadic list of String values.- Parameters:
values- String values to add to the ArrayNode.- Returns:
- Returns the created ArrayNode.
-
objectNodeBuilder
Creates anObjectNode.Builder.- Returns:
- Returns the ObjectNode builder.
-
objectNode
Creates an emptyObjectNode.- Returns:
- Returns the ObjectNode.
-
objectNode
Creates anObjectNodefrom the given map of Nodes.- Parameters:
values- Values to add to the object node.- Returns:
- Returns the created ObjectNode.
-
arrayNode
Creates an emptyArrayNode.- Returns:
- Returns the ArrayNode.
-
arrayNode
Creates anArrayNodefrom a variadic list of Nodes.- Parameters:
nodes- Nodes to add to the array.- Returns:
- Returns the created ArrayNode.
-
nullNode
Creates aNullNode.- Returns:
- Returns the NullNode.
-
loadArrayOfString
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
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
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
Gets the type of the node.- Returns:
- Returns the node type.
-
accept
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
Gets the node as an ObjectNode if it is an object.- Returns:
- Returns the optional object node.
-
asArrayNode
Gets the node as an ArrayNode if it is an array.- Returns:
- Returns the optional array node.
-
asStringNode
Gets the node as an StringNode if it is an string.- Returns:
- Returns the optional StringNode.
-
asBooleanNode
Gets the node as an BooleanNode if it is an boolean.- Returns:
- Returns the optional BooleanNode.
-
asNumberNode
Gets the node as an NumberNode if it is an number.- Returns:
- Returns the optional NumberNode.
-
asNullNode
Gets the node as an NullNode if it is a null.- Returns:
- Returns the optional NullNode.
-
expectObjectNode
Casts the current node to anObjectNode.- Returns:
- Returns an object node.
- Throws:
ExpectationNotMetException- when the node is not anObjectNode.
-
expectObjectNode
Casts the current node to anObjectNode, throwingExpectationNotMetExceptionwhen 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 anObjectNode.
-
expectObjectNode
Casts the current node to anObjectNode, throwingExpectationNotMetExceptionwhen the node is the wrong type.- Parameters:
message- Error message supplier.- Returns:
- Returns an object node.
- Throws:
ExpectationNotMetException- when the node is not anObjectNode.
-
expectArrayNode
Casts the current node to anArrayNode.- Returns:
- Returns an array node.
- Throws:
ExpectationNotMetException- when the node is not anArrayNode.
-
expectArrayNode
Casts the current node to anArrayNode, throwingExpectationNotMetExceptionwhen 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
Casts the current node to anArrayNode, throwingExpectationNotMetExceptionwhen 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
Casts the current node to aStringNode.- Returns:
- Returns a string node.
- Throws:
ExpectationNotMetException- when the node is the wrong type.
-
expectStringNode
Casts the current node to aStringNode, throwingExpectationNotMetExceptionwhen 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
Casts the current node to aStringNode, throwingExpectationNotMetExceptionwhen 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
Casts the current node to aNumberNode.- Returns:
- Returns a number node.
- Throws:
ExpectationNotMetException- when the node is the wrong type.
-
expectNumberNode
Casts the current node to aNumberNode, throwingExpectationNotMetExceptionwhen 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
Casts the current node to aNumberNode, throwingExpectationNotMetExceptionwhen 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
Casts the current node to aBooleanNode.- Returns:
- Returns a boolean node.
- Throws:
ExpectationNotMetException- when the node is the wrong type.
-
expectBooleanNode
Casts the current node to aBooleanNode, throwingExpectationNotMetExceptionwhen 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
Casts the current node to aBooleanNode, throwingExpectationNotMetExceptionwhen 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
Casts the current node to aNullNode.- Returns:
- Returns a null node.
- Throws:
ExpectationNotMetException- when the node is the wrong type.
-
expectNullNode
Casts the current node to aNullNode, throwingExpectationNotMetExceptionwhen 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
Casts the current node to aNullNode, throwingExpectationNotMetExceptionwhen 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
Description copied from interface:FromSourceLocationGets the source location of a value.- Specified by:
getSourceLocationin interfaceFromSourceLocation- Returns:
- Returns the source location of the value.
-
toNode
Description copied from interface:ToNodeConverts a value to aNode. -
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
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.
-