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
Modifier and TypeClassDescriptionstatic enum
Non-numeric values for floats and doubles. -
Method Summary
Modifier and TypeMethodDescriptionabstract <R> R
accept
(NodeVisitor<R> visitor) Accepts a visitor with the node.static ArrayNode
Creates an emptyArrayNode
.static ArrayNode
Creates anArrayNode
from 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 void
assertEquals
(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 ArrayNode
Casts the current node to anArrayNode
.expectArrayNode
(String message) Casts the current node to anArrayNode
, throwingExpectationNotMetException
when the node is the wrong type.expectArrayNode
(Supplier<String> message) Casts the current node to anArrayNode
, throwingExpectationNotMetException
when the node is the wrong type.final BooleanNode
Casts the current node to aBooleanNode
.expectBooleanNode
(String message) Casts the current node to aBooleanNode
, throwingExpectationNotMetException
when the node is the wrong type.expectBooleanNode
(Supplier<String> message) Casts the current node to aBooleanNode
, throwingExpectationNotMetException
when the node is the wrong type.final NullNode
Casts the current node to aNullNode
.expectNullNode
(String message) Casts the current node to aNullNode
, throwingExpectationNotMetException
when the node is the wrong type.expectNullNode
(Supplier<String> message) Casts the current node to aNullNode
, throwingExpectationNotMetException
when the node is the wrong type.final NumberNode
Casts the current node to aNumberNode
.expectNumberNode
(String message) Casts the current node to aNumberNode
, throwingExpectationNotMetException
when the node is the wrong type.expectNumberNode
(Supplier<String> message) Casts the current node to aNumberNode
, throwingExpectationNotMetException
when the node is the wrong type.final ObjectNode
Casts the current node to anObjectNode
.expectObjectNode
(String message) Casts the current node to anObjectNode
, throwingExpectationNotMetException
when the node is the wrong type.expectObjectNode
(Supplier<String> message) Casts the current node to anObjectNode
, throwingExpectationNotMetException
when the node is the wrong type.final StringNode
Casts the current node to aStringNode
.expectStringNode
(String message) Casts the current node to aStringNode
, throwingExpectationNotMetException
when the node is the wrong type.expectStringNode
(Supplier<String> message) Casts the current node to aStringNode
, throwingExpectationNotMetException
when the node is the wrong type.static BooleanNode
from
(boolean value) Create aBooleanNode
from a boolean value.static NumberNode
Create aNumberNode
from a Number value.static StringNode
Create aStringNode
from a String value.static Node
Create a Node from a potentially nullToNode
value.static ArrayNode
Creates anArrayNode
from a Collection of Node values.static ArrayNode
Creates anArrayNode
from a variadic list of Node values.static ArrayNode
fromStrings
(String... values) Creates anArrayNode
from a variadic list of String values.static ArrayNode
fromStrings
(Collection<String> values) Creates anArrayNode
from a Collection of String values.final SourceLocation
Gets the source location of a value.abstract NodeType
getType()
Gets the type of the node.final boolean
Checks if this node is an array type.final boolean
Checks if this node is a boolean type.final boolean
Checks if this node is a null type.final boolean
Checks if this node is a number type.final boolean
Checks if this node is an object type.final boolean
Checks if this node is a string type.loadArrayOfString
(String descriptor, Node node) Expects an array of strings and returns the loaded strings.static NullNode
nullNode()
Creates aNullNode
.static ObjectNode
Creates an emptyObjectNode
.static ObjectNode
objectNode
(Map<StringNode, Node> values) Creates anObjectNode
from the given map of Nodes.static ObjectNode.Builder
Creates anObjectNode.Builder
.static Node
parse
(InputStream json) Attempts to parse the given JSON input stream and returns a Node.static Node
parse
(InputStream json, String file) Attempts to parse the given JSON input stream and returns a Node.static Node
Attempts to parse the given JSON string and return a Node.static Node
Attempts to parse the given JSON string and File Name and return a Node.static Node
parseJsonWithComments
(String json) Attempts to parse the given JSON string and return a Node.static Node
parseJsonWithComments
(String json, String file) Attempts to parse the given JSON string and File Name and return a Node.static String
prettyPrintJson
(Node node) Writes the contents of a Node to a pretty-printed JSON string.static String
prettyPrintJson
(Node node, String indentString) Writes the contents of a Node to a pretty-printed JSON string.static String
Writes the contents of a Node to a non-pretty-printed JSON string.final Node
toNode()
Converts a value to aNode
.final Node
Returns a node with sorted keys and sorted keys of all nested object nodes.final Node
withDeepSortedKeys
(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 aStringNode
from a String value.- Parameters:
value
- Value to create a node from.- Returns:
- Returns the created StringNode.
-
from
Create aNumberNode
from a Number value.- Parameters:
number
- Value to create a node from.- Returns:
- Returns the created NumberNode.
-
from
Create aBooleanNode
from a boolean value.- Parameters:
value
- Value to create a node from.- Returns:
- Returns the created BooleanNode.
-
from
Create a Node from a potentially nullToNode
value.- Parameters:
value
- Value to create a node from.- Returns:
- Returns the created Node.
-
fromNodes
Creates anArrayNode
from a Collection of Node values.- Parameters:
values
- String values to add to the ArrayNode.- Returns:
- Returns the created ArrayNode.
-
fromNodes
Creates anArrayNode
from a variadic list of Node values.- Parameters:
values
- String values to add to the ArrayNode.- Returns:
- Returns the created ArrayNode.
-
fromStrings
Creates anArrayNode
from a Collection of String values.- Parameters:
values
- String values to add to the ArrayNode.- Returns:
- Returns the created ArrayNode.
-
fromStrings
Creates anArrayNode
from 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 anObjectNode
from 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 anArrayNode
from 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
, throwingExpectationNotMetException
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 anObjectNode
.
-
expectObjectNode
Casts the current node to anObjectNode
, throwingExpectationNotMetException
when 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
, throwingExpectationNotMetException
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
Casts the current node to anArrayNode
, throwingExpectationNotMetException
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
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
, throwingExpectationNotMetException
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
Casts the current node to aStringNode
, throwingExpectationNotMetException
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
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
, throwingExpectationNotMetException
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
Casts the current node to aNumberNode
, throwingExpectationNotMetException
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
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
, throwingExpectationNotMetException
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
Casts the current node to aBooleanNode
, throwingExpectationNotMetException
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
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
, throwingExpectationNotMetException
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
Casts the current node to aNullNode
, throwingExpectationNotMetException
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
Description copied from interface:FromSourceLocation
Gets the source location of a value.- Specified by:
getSourceLocation
in interfaceFromSourceLocation
- Returns:
- Returns the source location of the value.
-
toNode
Description copied from interface:ToNode
Converts 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.
-