public abstract class Node extends java.lang.Object implements FromSourceLocation, ToNode
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.
Modifier and Type | Class and Description |
---|---|
static class |
Node.NonNumericFloat
Non-numeric values for floats and doubles.
|
Modifier and Type | Method and Description |
---|---|
abstract <R> R |
accept(NodeVisitor<R> visitor)
Accepts a visitor with the node.
|
static ArrayNode |
arrayNode()
Creates an empty
ArrayNode . |
static ArrayNode |
arrayNode(Node... nodes)
Creates an
ArrayNode 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 an
ArrayNode . |
ArrayNode |
expectArrayNode(java.lang.String message)
Casts the current node to an
ArrayNode , throwing
ExpectationNotMetException when the node is the wrong type. |
ArrayNode |
expectArrayNode(java.util.function.Supplier<java.lang.String> message)
Casts the current node to an
ArrayNode , throwing
ExpectationNotMetException when the node is the wrong type. |
BooleanNode |
expectBooleanNode()
Casts the current node to a
BooleanNode . |
BooleanNode |
expectBooleanNode(java.lang.String message)
Casts the current node to a
BooleanNode , throwing
ExpectationNotMetException when the node is the wrong type. |
BooleanNode |
expectBooleanNode(java.util.function.Supplier<java.lang.String> message)
Casts the current node to a
BooleanNode , throwing
ExpectationNotMetException when the node is the wrong type. |
NullNode |
expectNullNode()
Casts the current node to a
NullNode . |
NullNode |
expectNullNode(java.lang.String message)
Casts the current node to a
NullNode , throwing
ExpectationNotMetException when the node is the wrong type. |
NullNode |
expectNullNode(java.util.function.Supplier<java.lang.String> message)
Casts the current node to a
NullNode , throwing
ExpectationNotMetException when the node is the wrong type. |
NumberNode |
expectNumberNode()
Casts the current node to a
NumberNode . |
NumberNode |
expectNumberNode(java.lang.String message)
Casts the current node to a
NumberNode , throwing
ExpectationNotMetException when the node is the wrong type. |
NumberNode |
expectNumberNode(java.util.function.Supplier<java.lang.String> message)
Casts the current node to a
NumberNode , throwing
ExpectationNotMetException when the node is the wrong type. |
ObjectNode |
expectObjectNode()
Casts the current node to an
ObjectNode . |
ObjectNode |
expectObjectNode(java.lang.String message)
Casts the current node to an
ObjectNode , throwing
ExpectationNotMetException when the node is the wrong type. |
ObjectNode |
expectObjectNode(java.util.function.Supplier<java.lang.String> message)
Casts the current node to an
ObjectNode , throwing
ExpectationNotMetException when the node is the wrong type. |
StringNode |
expectStringNode()
Casts the current node to a
StringNode . |
StringNode |
expectStringNode(java.lang.String message)
Casts the current node to a
StringNode , throwing
ExpectationNotMetException when the node is the wrong type. |
StringNode |
expectStringNode(java.util.function.Supplier<java.lang.String> message)
Casts the current node to a
StringNode , throwing
ExpectationNotMetException when the node is the wrong type. |
static BooleanNode |
from(boolean value)
Create a
BooleanNode from a boolean value. |
static NumberNode |
from(java.lang.Number number)
Create a
NumberNode from a Number value. |
static StringNode |
from(java.lang.String value)
Create a
StringNode from a String value. |
static ArrayNode |
fromNodes(java.util.List<? extends Node> values)
Creates an
ArrayNode from a Collection of Node values. |
static ArrayNode |
fromNodes(Node... values)
Creates an
ArrayNode from a variadic list of Node values. |
static ArrayNode |
fromStrings(java.util.Collection<java.lang.String> values)
Creates an
ArrayNode from a Collection of String values. |
static ArrayNode |
fromStrings(java.lang.String... values)
Creates an
ArrayNode from a variadic list 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 a
NullNode . |
static ObjectNode |
objectNode()
Creates an empty
ObjectNode . |
static ObjectNode |
objectNode(java.util.Map<StringNode,Node> values)
Creates an
ObjectNode from the given map of Nodes. |
static ObjectNode.Builder |
objectNodeBuilder()
Creates an
ObjectNode.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 a
Node . |
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.
|
public static Node parse(java.lang.String json)
json
- JSON text to parse.ModelSyntaxException
- if the JSON text is invalid.public static Node parse(java.lang.String json, java.lang.String file)
json
- JSON text to parse.file
- Filename corresponding to json textModelSyntaxException
- if the JSON text is invalid.public static Node parse(java.io.InputStream json)
json
- JSON input stream to parse. The input stream is closed
automatically when the content is fully parsed.ModelSyntaxException
- if the JSON text is invalid.public static Node parse(java.io.InputStream json, java.lang.String file)
json
- JSON input stream to parse. The input stream is closed
automatically when the content is fully parsed.file
- Filename corresponding to json textModelSyntaxException
- if the JSON text is invalid.public static Node parseJsonWithComments(java.lang.String json, java.lang.String file)
This parser allows for comments in the JSON.
json
- JSON text to parse.file
- Filename corresponding to json textModelSyntaxException
- if the JSON text is invalid.public static Node parseJsonWithComments(java.lang.String json)
This parser allows for comments in the JSON.
json
- JSON text to parse.ModelSyntaxException
- if the JSON text is invalid.public static java.lang.String prettyPrintJson(Node node)
node
- Node to write.public static java.lang.String prettyPrintJson(Node node, java.lang.String indentString)
node
- Node to write.indentString
- String to use for indention.public static java.lang.String printJson(Node node)
node
- Node to write.public static StringNode from(java.lang.String value)
StringNode
from a String value.value
- Value to create a node from.public static NumberNode from(java.lang.Number number)
NumberNode
from a Number value.number
- Value to create a node from.public static BooleanNode from(boolean value)
BooleanNode
from a boolean value.value
- Value to create a node from.public static ArrayNode fromNodes(java.util.List<? extends Node> values)
ArrayNode
from a Collection of Node values.values
- String values to add to the ArrayNode.public static ArrayNode fromNodes(Node... values)
ArrayNode
from a variadic list of Node values.values
- String values to add to the ArrayNode.public static ArrayNode fromStrings(java.util.Collection<java.lang.String> values)
ArrayNode
from a Collection of String values.values
- String values to add to the ArrayNode.public static ArrayNode fromStrings(java.lang.String... values)
ArrayNode
from a variadic list of String values.values
- String values to add to the ArrayNode.public static ObjectNode.Builder objectNodeBuilder()
ObjectNode.Builder
.public static ObjectNode objectNode()
ObjectNode
.public static ObjectNode objectNode(java.util.Map<StringNode,Node> values)
ObjectNode
from the given map of Nodes.values
- Values to add to the object node.public static ArrayNode arrayNode()
ArrayNode
.public static ArrayNode arrayNode(Node... nodes)
ArrayNode
from a variadic list of Nodes.nodes
- Nodes to add to the array.public static java.util.List<java.lang.String> loadArrayOfString(java.lang.String descriptor, Node node)
descriptor
- Name of the property being loaded.node
- Node to load.SourceException
- on error.public static void assertEquals(ToNode actual, ToNode expected)
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)
.
actual
- Node to use as the starting node.expected
- Node to compare against.ExpectationNotMetException
- if the nodes are not equivalent.public static java.util.List<java.lang.String> diff(ToNode actual, ToNode expected)
actual
- Node to use as the starting node.expected
- Node to compare against.public abstract NodeType getType()
public abstract <R> R accept(NodeVisitor<R> visitor)
R
- visitor return type.visitor
- Visitor to dispatch to.public final boolean isObjectNode()
public final boolean isArrayNode()
public final boolean isStringNode()
public final boolean isNumberNode()
public final boolean isBooleanNode()
public final boolean isNullNode()
public java.util.Optional<ObjectNode> asObjectNode()
public java.util.Optional<ArrayNode> asArrayNode()
public java.util.Optional<StringNode> asStringNode()
public java.util.Optional<BooleanNode> asBooleanNode()
public java.util.Optional<NumberNode> asNumberNode()
public java.util.Optional<NullNode> asNullNode()
public final ObjectNode expectObjectNode()
ObjectNode
.ExpectationNotMetException
- when the node is not an ObjectNode
.public ObjectNode expectObjectNode(java.lang.String message)
ObjectNode
, throwing
ExpectationNotMetException
when the node is the wrong type.message
- Error message to use if the node is of the wrong type.ExpectationNotMetException
- when the node is not an ObjectNode
.public ObjectNode expectObjectNode(java.util.function.Supplier<java.lang.String> message)
ObjectNode
, throwing
ExpectationNotMetException
when the node is the wrong type.message
- Error message supplier.ExpectationNotMetException
- when the node is not an ObjectNode
.public final ArrayNode expectArrayNode()
ArrayNode
.ExpectationNotMetException
- when the node is not an ArrayNode
.public ArrayNode expectArrayNode(java.lang.String message)
ArrayNode
, throwing
ExpectationNotMetException
when the node is the wrong type.message
- Error message to use if the node is of the wrong type.ExpectationNotMetException
- when the node is the wrong type.public ArrayNode expectArrayNode(java.util.function.Supplier<java.lang.String> message)
ArrayNode
, throwing
ExpectationNotMetException
when the node is the wrong type.message
- Error message supplier.ExpectationNotMetException
- when the node is the wrong type.public final StringNode expectStringNode()
StringNode
.ExpectationNotMetException
- when the node is the wrong type.public StringNode expectStringNode(java.lang.String message)
StringNode
, throwing
ExpectationNotMetException
when the node is the wrong type.message
- Error message to use if the node is of the wrong type.ExpectationNotMetException
- when the node is the wrong type.public StringNode expectStringNode(java.util.function.Supplier<java.lang.String> message)
StringNode
, throwing
ExpectationNotMetException
when the node is the wrong type.message
- Error message supplier.ExpectationNotMetException
- when the node is the wrong type.public final NumberNode expectNumberNode()
NumberNode
.ExpectationNotMetException
- when the node is the wrong type.public NumberNode expectNumberNode(java.lang.String message)
NumberNode
, throwing
ExpectationNotMetException
when the node is the wrong type.message
- Error message to use if the node is of the wrong type.ExpectationNotMetException
- when the node is the wrong type.public NumberNode expectNumberNode(java.util.function.Supplier<java.lang.String> message)
NumberNode
, throwing
ExpectationNotMetException
when the node is the wrong type.message
- Error message supplier.ExpectationNotMetException
- when the node is the wrong type.public final BooleanNode expectBooleanNode()
BooleanNode
.ExpectationNotMetException
- when the node is the wrong type.public BooleanNode expectBooleanNode(java.lang.String message)
BooleanNode
, throwing
ExpectationNotMetException
when the node is the wrong type.message
- Error message to use if the node is of the wrong type.ExpectationNotMetException
- when the node is the wrong type.public BooleanNode expectBooleanNode(java.util.function.Supplier<java.lang.String> message)
BooleanNode
, throwing
ExpectationNotMetException
when the node is the wrong type.message
- Error message supplier.ExpectationNotMetException
- when the node is the wrong type.public final NullNode expectNullNode()
NullNode
.ExpectationNotMetException
- when the node is the wrong type.public NullNode expectNullNode(java.lang.String message)
NullNode
, throwing
ExpectationNotMetException
when the node is the wrong type.message
- Error message to use if the node is of the wrong type.ExpectationNotMetException
- when the node is the wrong type.public NullNode expectNullNode(java.util.function.Supplier<java.lang.String> message)
NullNode
, throwing
ExpectationNotMetException
when the node is the wrong type.message
- Error message supplier.ExpectationNotMetException
- when the node is the wrong type.public final SourceLocation getSourceLocation()
FromSourceLocation
getSourceLocation
in interface FromSourceLocation
public final Node toNode()
ToNode
Node
.public final Node withDeepSortedKeys()
public final Node withDeepSortedKeys(java.util.Comparator<StringNode> keyComparator)
keyComparator
- Compares keys.