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 java.lang.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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Node.NonNumericFloat
Non-numeric values for floats and doubles.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract <R> R
accept(NodeVisitor<R> visitor)
Accepts a visitor with the node.static ArrayNode
arrayNode()
Creates an emptyArrayNode
.static ArrayNode
arrayNode(Node... nodes)
Creates anArrayNode
from a variadic list of Nodes.java.util.Optional<ArrayNode>
asArrayNode()
Gets the node as an ArrayNode if it is an array.java.util.Optional<BooleanNode>
asBooleanNode()
Gets the node as an BooleanNode if it is an boolean.java.util.Optional<NullNode>
asNullNode()
Gets the node as an NullNode if it is a null.java.util.Optional<NumberNode>
asNumberNode()
Gets the node as an NumberNode if it is an number.java.util.Optional<ObjectNode>
asObjectNode()
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.java.util.Optional<StringNode>
asStringNode()
Gets the node as an StringNode if it is an string.static java.util.List<java.lang.String>
diff(ToNode actual, ToNode expected)
Computes the differences between two Nodes as a String.ArrayNode
expectArrayNode()
Casts the current node to anArrayNode
.ArrayNode
expectArrayNode(java.lang.String message)
Casts the current node to anArrayNode
, throwingExpectationNotMetException
when the node is the wrong type.ArrayNode
expectArrayNode(java.util.function.Supplier<java.lang.String> message)
Casts the current node to anArrayNode
, throwingExpectationNotMetException
when the node is the wrong type.BooleanNode
expectBooleanNode()
Casts the current node to aBooleanNode
.BooleanNode
expectBooleanNode(java.lang.String message)
Casts the current node to aBooleanNode
, throwingExpectationNotMetException
when the node is the wrong type.BooleanNode
expectBooleanNode(java.util.function.Supplier<java.lang.String> message)
Casts the current node to aBooleanNode
, throwingExpectationNotMetException
when the node is the wrong type.NullNode
expectNullNode()
Casts the current node to aNullNode
.NullNode
expectNullNode(java.lang.String message)
Casts the current node to aNullNode
, throwingExpectationNotMetException
when the node is the wrong type.NullNode
expectNullNode(java.util.function.Supplier<java.lang.String> message)
Casts the current node to aNullNode
, throwingExpectationNotMetException
when the node is the wrong type.NumberNode
expectNumberNode()
Casts the current node to aNumberNode
.NumberNode
expectNumberNode(java.lang.String message)
Casts the current node to aNumberNode
, throwingExpectationNotMetException
when the node is the wrong type.NumberNode
expectNumberNode(java.util.function.Supplier<java.lang.String> message)
Casts the current node to aNumberNode
, throwingExpectationNotMetException
when the node is the wrong type.ObjectNode
expectObjectNode()
Casts the current node to anObjectNode
.ObjectNode
expectObjectNode(java.lang.String message)
Casts the current node to anObjectNode
, throwingExpectationNotMetException
when the node is the wrong type.ObjectNode
expectObjectNode(java.util.function.Supplier<java.lang.String> message)
Casts the current node to anObjectNode
, throwingExpectationNotMetException
when the node is the wrong type.StringNode
expectStringNode()
Casts the current node to aStringNode
.StringNode
expectStringNode(java.lang.String message)
Casts the current node to aStringNode
, throwingExpectationNotMetException
when the node is the wrong type.StringNode
expectStringNode(java.util.function.Supplier<java.lang.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
from(java.lang.Number number)
Create aNumberNode
from a Number value.static StringNode
from(java.lang.String value)
Create aStringNode
from a String value.static Node
from(ToNode value)
Create a Node from a potentially nullToNode
value.static ArrayNode
fromNodes(java.util.List<? extends Node> values)
Creates anArrayNode
from a Collection of Node values.static ArrayNode
fromNodes(Node... values)
Creates anArrayNode
from a variadic list of Node values.static ArrayNode
fromStrings(java.lang.String... values)
Creates anArrayNode
from a variadic list of String values.static ArrayNode
fromStrings(java.util.Collection<java.lang.String> values)
Creates anArrayNode
from a Collection of String values.SourceLocation
getSourceLocation()
Gets the source location of a value.abstract NodeType
getType()
Gets the type of the node.boolean
isArrayNode()
Checks if this node is an array type.boolean
isBooleanNode()
Checks if this node is a boolean type.boolean
isNullNode()
Checks if this node is a null type.boolean
isNumberNode()
Checks if this node is a number type.boolean
isObjectNode()
Checks if this node is an object type.boolean
isStringNode()
Checks if this node is a string type.static java.util.List<java.lang.String>
loadArrayOfString(java.lang.String descriptor, Node node)
Expects an array of strings and returns the loaded strings.static NullNode
nullNode()
Creates aNullNode
.static ObjectNode
objectNode()
Creates an emptyObjectNode
.static ObjectNode
objectNode(java.util.Map<StringNode,Node> values)
Creates anObjectNode
from the given map of Nodes.static ObjectNode.Builder
objectNodeBuilder()
Creates anObjectNode.Builder
.static Node
parse(java.io.InputStream json)
Attempts to parse the given JSON input stream and returns a Node.static Node
parse(java.io.InputStream json, java.lang.String file)
Attempts to parse the given JSON input stream and returns a Node.static Node
parse(java.lang.String json)
Attempts to parse the given JSON string and return a Node.static Node
parse(java.lang.String json, java.lang.String file)
Attempts to parse the given JSON string and File Name and return a Node.static Node
parseJsonWithComments(java.lang.String json)
Attempts to parse the given JSON string and return a Node.static Node
parseJsonWithComments(java.lang.String json, java.lang.String file)
Attempts to parse the given JSON string and File Name and return a Node.static java.lang.String
prettyPrintJson(Node node)
Writes the contents of a Node to a pretty-printed JSON string.static java.lang.String
prettyPrintJson(Node node, java.lang.String indentString)
Writes the contents of a Node to a pretty-printed JSON string.static java.lang.String
printJson(Node node)
Writes the contents of a Node to a non-pretty-printed JSON string.Node
toNode()
Converts a value to aNode
.Node
withDeepSortedKeys()
Returns a node with sorted keys and sorted keys of all nested object nodes.Node
withDeepSortedKeys(java.util.Comparator<StringNode> keyComparator)
Returns a node with sorted keys and sorted keys of all nested object nodes using a custom comparator.
-
-
-
Method Detail
-
parse
public static Node parse(java.lang.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(java.lang.String json, java.lang.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(java.io.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(java.io.InputStream json, java.lang.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(java.lang.String json, java.lang.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(java.lang.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 java.lang.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 java.lang.String prettyPrintJson(Node node, java.lang.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 java.lang.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(java.lang.String value)
Create aStringNode
from a String value.- Parameters:
value
- Value to create a node from.- Returns:
- Returns the created StringNode.
-
from
public static NumberNode from(java.lang.Number number)
Create aNumberNode
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 aBooleanNode
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 nullToNode
value.- Parameters:
value
- Value to create a node from.- Returns:
- Returns the created Node.
-
fromNodes
public static ArrayNode fromNodes(java.util.List<? extends Node> values)
Creates anArrayNode
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 anArrayNode
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(java.util.Collection<java.lang.String> values)
Creates anArrayNode
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(java.lang.String... values)
Creates anArrayNode
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 anObjectNode.Builder
.- Returns:
- Returns the ObjectNode builder.
-
objectNode
public static ObjectNode objectNode()
Creates an emptyObjectNode
.- Returns:
- Returns the ObjectNode.
-
objectNode
public static ObjectNode objectNode(java.util.Map<StringNode,Node> values)
Creates anObjectNode
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 emptyArrayNode
.- Returns:
- Returns the ArrayNode.
-
arrayNode
public static ArrayNode arrayNode(Node... nodes)
Creates anArrayNode
from a variadic list of Nodes.- Parameters:
nodes
- Nodes to add to the array.- Returns:
- Returns the created ArrayNode.
-
loadArrayOfString
public static java.util.List<java.lang.String> loadArrayOfString(java.lang.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 java.util.List<java.lang.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 java.util.Optional<ObjectNode> asObjectNode()
Gets the node as an ObjectNode if it is an object.- Returns:
- Returns the optional object node.
-
asArrayNode
public java.util.Optional<ArrayNode> asArrayNode()
Gets the node as an ArrayNode if it is an array.- Returns:
- Returns the optional array node.
-
asStringNode
public java.util.Optional<StringNode> asStringNode()
Gets the node as an StringNode if it is an string.- Returns:
- Returns the optional StringNode.
-
asBooleanNode
public java.util.Optional<BooleanNode> asBooleanNode()
Gets the node as an BooleanNode if it is an boolean.- Returns:
- Returns the optional BooleanNode.
-
asNumberNode
public java.util.Optional<NumberNode> asNumberNode()
Gets the node as an NumberNode if it is an number.- Returns:
- Returns the optional NumberNode.
-
asNullNode
public java.util.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 anObjectNode
.- Returns:
- Returns an object node.
- Throws:
ExpectationNotMetException
- when the node is not anObjectNode
.
-
expectObjectNode
public ObjectNode expectObjectNode(java.lang.String message)
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
public ObjectNode expectObjectNode(java.util.function.Supplier<java.lang.String> message)
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
public final ArrayNode expectArrayNode()
Casts the current node to anArrayNode
.- Returns:
- Returns an array node.
- Throws:
ExpectationNotMetException
- when the node is not anArrayNode
.
-
expectArrayNode
public ArrayNode expectArrayNode(java.lang.String message)
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
public ArrayNode expectArrayNode(java.util.function.Supplier<java.lang.String> message)
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
public final StringNode expectStringNode()
Casts the current node to aStringNode
.- Returns:
- Returns a string node.
- Throws:
ExpectationNotMetException
- when the node is the wrong type.
-
expectStringNode
public StringNode expectStringNode(java.lang.String message)
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
public StringNode expectStringNode(java.util.function.Supplier<java.lang.String> message)
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
public final NumberNode expectNumberNode()
Casts the current node to aNumberNode
.- Returns:
- Returns a number node.
- Throws:
ExpectationNotMetException
- when the node is the wrong type.
-
expectNumberNode
public NumberNode expectNumberNode(java.lang.String message)
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
public NumberNode expectNumberNode(java.util.function.Supplier<java.lang.String> message)
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
public final BooleanNode expectBooleanNode()
Casts the current node to aBooleanNode
.- Returns:
- Returns a boolean node.
- Throws:
ExpectationNotMetException
- when the node is the wrong type.
-
expectBooleanNode
public BooleanNode expectBooleanNode(java.lang.String message)
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
public BooleanNode expectBooleanNode(java.util.function.Supplier<java.lang.String> message)
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
public final NullNode expectNullNode()
Casts the current node to aNullNode
.- Returns:
- Returns a null node.
- Throws:
ExpectationNotMetException
- when the node is the wrong type.
-
expectNullNode
public NullNode expectNullNode(java.lang.String message)
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
public NullNode expectNullNode(java.util.function.Supplier<java.lang.String> message)
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
public final SourceLocation 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
public final Node toNode()
Description copied from interface:ToNode
Converts a value to aNode
.
-
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(java.util.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.
-
-