Class ObjectNode
java.lang.Object
software.amazon.smithy.model.node.Node
software.amazon.smithy.model.node.ObjectNode
- All Implemented Interfaces:
FromSourceLocation
,ToNode
,ToSmithyBuilder<ObjectNode>
Represents an object node.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
Builder used to efficiently create an ObjectNode.Nested classes/interfaces inherited from class software.amazon.smithy.model.node.Node
Node.NonNumericFloat
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescription<R> R
accept
(NodeVisitor<R> visitor) Accepts a visitor with the node.Gets the node as an ObjectNode if it is an object.static ObjectNode.Builder
builder()
static <T> Collector<T,
Map<StringNode, Node>, ObjectNode> collect
(Function<T, StringNode> keyMapper, Function<T, ToNode> valueMapper) Creates a collector that creates an ObjectNode.static <T> Collector<T,
Map<StringNode, Node>, ObjectNode> collectStringKeys
(Function<T, String> keyMapper, Function<T, ToNode> valueMapper) Creates a collector that creates an ObjectNode.boolean
containsMember
(String memberName) Checks if the given member name exists in the ObjectNode.boolean
expectArrayMember
(String name) Gets a member and requires it to be an array.<N extends Node,
T>
ObjectNodeexpectArrayMember
(String k, Function<N, T> map, Consumer<List<T>> consumer) Requires that the given member exists and is an array; then creates a list of values for the array by passing array element through the mapping function, and passes that list to the consumer.expectBooleanMember
(String name) Gets a member and requires it to be a boolean.expectBooleanMember
(String key, Consumer<Boolean> consumer) Requires thatkey
exists, is a boolean, and passes the value toconsumer
.expectMember
(String name) Gets the member with the given name.expectMember
(String name, String errorMessage) Gets the member with the given name, throwingExpectationNotMetException
when the member is not present.<T> ObjectNode
expectMember
(String key, Function<Node, T> mapper, Consumer<T> consumer) Requires that thekey
member is present, passes the value through the givenmapper
, and then passes the mapped value toconsumer
.expectMember
(String name, Supplier<String> errorMessage) Gets the member with the given name, throwingExpectationNotMetException
when the member is not present.expectNoAdditionalProperties
(Collection<String> allowedProperties) Ensures that there are no additional properties other than the provided member names.expectNullMember
(String name) Gets a member and requires it to be a null.expectNumberMember
(String name) Gets a member and requires it to be a number.expectNumberMember
(String key, Consumer<Number> consumer) Requires thatkey
exists, is a number, and passes the value toconsumer
.expectObjectMember
(String name) Gets a member and requires it to be an object.expectObjectMember
(String name, Consumer<ObjectNode> consumer) Gets a member and requires it to be an object.expectObjectNode
(String errorMessage) Casts the current node to anObjectNode
, throwingExpectationNotMetException
when the node is the wrong type.expectObjectNode
(Supplier<String> errorMessage) Casts the current node to anObjectNode
, throwingExpectationNotMetException
when the node is the wrong type.expectStringMember
(String name) Gets a member and requires it to be a string.expectStringMember
(String key, Consumer<String> consumer) Requires thatkey
exists, is a string, and passes the value toconsumer
.static ObjectNode
fromStringMap
(Map<String, String> map) getArrayMember
(String memberName) Gets the member with the given name, and if present, expects it to be an array.getArrayMember
(String key, Consumer<List<Node>> consumer) Gets the nodes of an optional member that contains an array.<N extends Node,
T>
ObjectNodegetArrayMember
(String k, Function<N, T> map, Consumer<List<T>> consumer) The same asexpectArrayMember(String, Function, Consumer)
, but the member is optional.getBooleanMember
(String memberName) Gets the member with the given name, and if present, expects it to be a boolean.getBooleanMember
(String key, Consumer<Boolean> consumer) The same asexpectBooleanMember(String, Consumer)
but the member is optional.boolean
getBooleanMemberOrDefault
(String memberName) Gets the member with the given name, and if present, expects it to be a boolean, otherwise returns false.getBooleanMemberOrDefault
(String memberName, Boolean defaultValue) Gets the member with the given name, and if present, expects it to be a boolean, otherwise returns a default value.Gets the member with the given name.<T> ObjectNode
The same asexpectMember(String, Function, Consumer)
, but the member is optional.Gets the map of members.getMembersByPrefix
(String prefix) Gets a map of all members where the key starts with the given prefix.getNumberMember
(String memberName) Gets the member with the given name, and if present, expects it to be a number.getNumberMember
(String key, Consumer<Number> consumer) The same asexpectNumberMember(String, Consumer)
but the member is optional.getNumberMemberOrDefault
(String memberName, Number defaultValue) Gets the member with the given name, and if present, expects it to be a number, otherwise returns the default value.getObjectMember
(String memberName) Gets the member with the given name, and if present, expects it to be an object.getObjectMember
(String memberName, Consumer<ObjectNode> consumer) Gets the member with the given name, and if present, expects it to be an object.Gets an immutableMap<String, Node>
that represents the ObjectNode.getStringMember
(String memberName) Gets the member with the given name, and if present, expects it to be a string.getStringMember
(String key, Consumer<String> consumer) The same asexpectStringMember(String, Consumer)
but the member is optional.getStringMemberOrDefault
(String memberName, String defaultValue) Gets the member with the given name, and if present, expects it to be a string, otherwise returns the default value.getType()
Gets the type of the node.int
hashCode()
boolean
isEmpty()
Returns true if this object has no members.merge
(ObjectNode other) Merge this object node with another, creating a new ObjectNode.int
size()
Returns the number of members.Take this object and create a builder that contains all of the current property values of this object.warnIfAdditionalProperties
(Collection<String> allowedProperties) Warns if unknown properties are found in object.withMember
(String key, boolean value) Constructs a new object node with the given member added.withMember
(String key, Number value) Constructs a new object node with the given member added.withMember
(String key, String value) Constructs a new object node with the given member added.<T extends ToNode>
ObjectNodewithMember
(String key, T value) Constructs a new object node with the given member added.<T extends ToNode>
ObjectNodewithMember
(StringNode key, T value) Constructs a new object node with the given member added.<T extends ToNode>
ObjectNodewithOptionalMember
(String key, Optional<T> value) Adds a member to a new ObjectNode if the provided value is present.withoutMember
(String memberName) Constructs a new object node from the current node, but without the named member.Methods inherited from class software.amazon.smithy.model.node.Node
arrayNode, arrayNode, asArrayNode, asBooleanNode, asNullNode, asNumberNode, assertEquals, asStringNode, diff, expectArrayNode, expectArrayNode, expectArrayNode, expectBooleanNode, expectBooleanNode, expectBooleanNode, expectNullNode, expectNullNode, expectNullNode, expectNumberNode, expectNumberNode, expectNumberNode, expectObjectNode, expectStringNode, expectStringNode, expectStringNode, from, from, from, from, fromNodes, fromNodes, fromStrings, fromStrings, getSourceLocation, isArrayNode, isBooleanNode, isNullNode, isNumberNode, isObjectNode, isStringNode, loadArrayOfString, nullNode, objectNode, objectNode, objectNodeBuilder, parse, parse, parse, parse, parseJsonWithComments, parseJsonWithComments, prettyPrintJson, prettyPrintJson, printJson, toNode, withDeepSortedKeys, withDeepSortedKeys
-
Constructor Details
-
ObjectNode
-
-
Method Details
-
fromStringMap
-
builder
-
getType
Description copied from class:Node
Gets the type of the node. -
accept
Description copied from class:Node
Accepts a visitor with the node. -
expectObjectNode
Description copied from class:Node
Casts the current node to anObjectNode
, throwingExpectationNotMetException
when the node is the wrong type.- Overrides:
expectObjectNode
in classNode
- Parameters:
errorMessage
- Error message to use if the node is of the wrong type.- Returns:
- Returns an object node.
-
expectObjectNode
Description copied from class:Node
Casts the current node to anObjectNode
, throwingExpectationNotMetException
when the node is the wrong type.- Overrides:
expectObjectNode
in classNode
- Parameters:
errorMessage
- Error message supplier.- Returns:
- Returns an object node.
-
asObjectNode
Description copied from class:Node
Gets the node as an ObjectNode if it is an object.- Overrides:
asObjectNode
in classNode
- Returns:
- Returns the optional object node.
-
withMember
Constructs a new object node with the given member added.- Type Parameters:
T
- Type of the value member to add.- Parameters:
key
- Name of the member to add.value
- Value of the member to add.- Returns:
- Returns a new object node.
-
withMember
Constructs a new object node with the given member added.- Type Parameters:
T
- Type of the value member to add.- Parameters:
key
- Name of the member to add as a string.value
- Value of the member to add.- Returns:
- Returns a new object node.
-
withMember
Constructs a new object node with the given member added.- Parameters:
key
- Name of the member to add as a string.value
- Value of the member to add.- Returns:
- Returns a new object node.
-
withMember
Constructs a new object node with the given member added.- Parameters:
key
- Name of the member to add as a string.value
- Value of the member to add.- Returns:
- Returns a new object node.
-
withMember
Constructs a new object node with the given member added.- Parameters:
key
- Name of the member to add as a string.value
- Value of the member to add.- Returns:
- Returns a new object node.
-
withOptionalMember
Adds a member to a new ObjectNode if the provided value is present.- Type Parameters:
T
- Type of the value member to add.- Parameters:
key
- Key to set if value is present.value
- Value that may be present.- Returns:
- Returns an object with the optionally added member.
-
withoutMember
Constructs a new object node from the current node, but without the named member.- Parameters:
memberName
- Name of a member that should be omitted.- Returns:
- Returns a new object node.
-
getMembers
Gets the map of members.- Returns:
- Returns a map of nodes.
-
containsMember
Checks if the given member name exists in the ObjectNode.- Parameters:
memberName
- Member name to check.- Returns:
- Returns true if this member is in the ObjectNode.
-
getMember
Gets the member with the given name.- Parameters:
memberName
- Name of the member to get.- Returns:
- Returns the optional node with the given member name.
-
getMembersByPrefix
Gets a map of all members where the key starts with the given prefix.- Parameters:
prefix
- Prefix to search for in keys.- Returns:
- Returns the map of matching members.
-
getStringMap
Gets an immutableMap<String, Node>
that represents the ObjectNode.- Returns:
- Returns the immutable map.
-
getStringMember
Gets the member with the given name, and if present, expects it to be a string.- Parameters:
memberName
- Name of the member to get.- Returns:
- Returns the optional node with the given member name.
- Throws:
ExpectationNotMetException
- if the member is not a string.
-
getStringMemberOrDefault
Gets the member with the given name, and if present, expects it to be a string, otherwise returns the default value.- Parameters:
memberName
- Name of the member to get.defaultValue
- Default string value to return if the member is not present.- Returns:
- Returns a String value contained in the String node or the default value.
- Throws:
ExpectationNotMetException
- if the member is not a string.
-
getNumberMember
Gets the member with the given name, and if present, expects it to be a number.- Parameters:
memberName
- Name of the member to get.- Returns:
- Returns the optional node with the given member name.
- Throws:
ExpectationNotMetException
- if the member is not a number.
-
getNumberMemberOrDefault
Gets the member with the given name, and if present, expects it to be a number, otherwise returns the default value.- Parameters:
memberName
- Name of the member to get.defaultValue
- Default value to return if a member is not found.- Returns:
- Returns the Number value or a default value.
- Throws:
ExpectationNotMetException
- if the member is not a number.
-
getArrayMember
Gets the member with the given name, and if present, expects it to be an array.- Parameters:
memberName
- Name of the member to get.- Returns:
- Returns the optional node with the given member name.
- Throws:
ExpectationNotMetException
- if the member is not an array.
-
getObjectMember
Gets the member with the given name, and if present, expects it to be an object.- Parameters:
memberName
- Name of the member to get.- Returns:
- Returns the optional node with the given member name.
- Throws:
ExpectationNotMetException
- if the member is not an object.
-
getBooleanMember
Gets the member with the given name, and if present, expects it to be a boolean.- Parameters:
memberName
- Name of the member to get.- Returns:
- Returns the optional node with the given member name.
- Throws:
ExpectationNotMetException
- if the member is not a boolean.
-
getBooleanMemberOrDefault
Gets the member with the given name, and if present, expects it to be a boolean, otherwise returns a default value.- Parameters:
memberName
- Name of the member to get.defaultValue
- Default value to return if not present.- Returns:
- Returns the boolean value or a default value.
- Throws:
ExpectationNotMetException
- if the member is not a boolean.
-
getBooleanMemberOrDefault
Gets the member with the given name, and if present, expects it to be a boolean, otherwise returns false.- Parameters:
memberName
- Name of the member to get.- Returns:
- Returns the boolean value or false if not found.
- Throws:
ExpectationNotMetException
- if the member is found and not a boolean.
-
expectMember
Gets the member with the given name.- Parameters:
name
- Name of the member to get.- Returns:
- Returns the node with the given member name.
- Throws:
IllegalArgumentException
- whenmemberName
is null.ExpectationNotMetException
- whenmemberName
is not present in the members map.
-
expectMember
Gets the member with the given name, throwingExpectationNotMetException
when the member is not present.- Parameters:
name
- Name of the member to get.errorMessage
- The error message to use if the expectation is not met.- Returns:
- Returns the node with the given member name.
- Throws:
ExpectationNotMetException
- whenmemberName
is not present in the members map.
-
expectMember
Gets the member with the given name, throwingExpectationNotMetException
when the member is not present.- Parameters:
name
- Name of the member to get.errorMessage
- Error message supplier.- Returns:
- Returns the node with the given member name.
- Throws:
ExpectationNotMetException
- whenmemberName
is not present in the members map.
-
expectArrayMember
Gets a member and requires it to be an array.- Parameters:
name
- Name of the member to get.- Returns:
- Returns the node with the given member name.
- Throws:
ExpectationNotMetException
- when not present or not an array.
-
expectBooleanMember
Gets a member and requires it to be a boolean.- Parameters:
name
- Name of the member to get.- Returns:
- Returns the node with the given member name.
- Throws:
ExpectationNotMetException
- when not present or not a boolean.
-
expectNullMember
Gets a member and requires it to be a null.- Parameters:
name
- Name of the member to get.- Returns:
- Returns the node with the given member name.
- Throws:
ExpectationNotMetException
- when not present or not a null.
-
expectNumberMember
Gets a member and requires it to be a number.- Parameters:
name
- Name of the member to get.- Returns:
- Returns the node with the given member name.
- Throws:
ExpectationNotMetException
- when not present or not a number.
-
expectObjectMember
Gets a member and requires it to be an object.- Parameters:
name
- Name of the member to get.- Returns:
- Returns the node with the given member name.
- Throws:
ExpectationNotMetException
- when not present or not an object.
-
expectStringMember
Gets a member and requires it to be a string.- Parameters:
name
- Name of the member to get.- Returns:
- Returns the node with the given member name.
- Throws:
ExpectationNotMetException
- when not present or not a string.
-
expectNoAdditionalProperties
Ensures that there are no additional properties other than the provided member names.- Parameters:
allowedProperties
- Properties that may exist.- Returns:
- Returns self
- Throws:
ExpectationNotMetException
- if other properties are found.
-
warnIfAdditionalProperties
Warns if unknown properties are found in object.- Parameters:
allowedProperties
- Properties that may exist.- Returns:
- Returns self
-
expectMember
Requires that thekey
member is present, passes the value through the givenmapper
, and then passes the mapped value toconsumer
.- Type Parameters:
T
- Mapped value type.- Parameters:
key
- Key to get from the object.mapper
- Mapping function used to convert the node value.consumer
- Consumer to pass the found value to.- Returns:
- Returns the node.
-
getMember
The same asexpectMember(String, Function, Consumer)
, but the member is optional.- Type Parameters:
T
- Mapped value type.- Parameters:
key
- Key to get from the object.mapper
- Mapping function used to convert the node value.consumer
- Consumer to pass the found value to.- Returns:
- Returns the node.
-
expectObjectMember
Gets a member and requires it to be an object.- Parameters:
name
- Name of the member to get.consumer
- Consumer that accepts the object member.- Returns:
- Returns the node.
- Throws:
ExpectationNotMetException
- when not present or not an object.
-
getObjectMember
Gets the member with the given name, and if present, expects it to be an object.- Parameters:
memberName
- Name of the member to get.consumer
- Consumer that accepts the member if found.- Returns:
- Returns the node.
- Throws:
ExpectationNotMetException
- if the member is not an object.
-
expectStringMember
Requires thatkey
exists, is a string, and passes the value toconsumer
.- Parameters:
key
- Key to retrieve.consumer
- Consumer that accepts the string value.- Returns:
- Returns the node.
-
getStringMember
The same asexpectStringMember(String, Consumer)
but the member is optional.- Parameters:
key
- Key to retrieve.consumer
- Consumer that accepts the string value.- Returns:
- Returns the node.
-
expectBooleanMember
Requires thatkey
exists, is a boolean, and passes the value toconsumer
.- Parameters:
key
- Key to retrieve.consumer
- Consumer that accepts the boolean value.- Returns:
- Returns the node.
-
getBooleanMember
The same asexpectBooleanMember(String, Consumer)
but the member is optional.- Parameters:
key
- Key to retrieve.consumer
- Consumer that accepts the boolean value.- Returns:
- Returns the node.
-
expectNumberMember
Requires thatkey
exists, is a number, and passes the value toconsumer
.- Parameters:
key
- Key to retrieve.consumer
- Consumer that accepts the number value.- Returns:
- Returns the node.
-
getNumberMember
The same asexpectNumberMember(String, Consumer)
but the member is optional.- Parameters:
key
- Key to retrieve.consumer
- Consumer that accepts the number value.- Returns:
- Returns the node.
-
getArrayMember
Gets the nodes of an optional member that contains an array.- Parameters:
key
- Key to retrieve.consumer
- Consumer that accepts the array node value.- Returns:
- Returns the node.
-
expectArrayMember
public <N extends Node,T> ObjectNode expectArrayMember(String k, Function<N, T> map, Consumer<List<T>> consumer) Requires that the given member exists and is an array; then creates a list of values for the array by passing array element through the mapping function, and passes that list to the consumer.- Type Parameters:
N
- Type of Node to expect in each array element.T
- Type of value returned from the mapper.- Parameters:
k
- Key to retrieve.map
- Mapper that takes each array node and returns a mapped value.consumer
- Consumer that accepts the collected mapped values.- Returns:
- Returns the node.
-
getArrayMember
public <N extends Node,T> ObjectNode getArrayMember(String k, Function<N, T> map, Consumer<List<T>> consumer) The same asexpectArrayMember(String, Function, Consumer)
, but the member is optional.- Type Parameters:
N
- Type of Node to expect in each array element.T
- Type of value returned from the mapper.- Parameters:
k
- Key to retrieve.map
- Mapper that takes each array node and returns a mapped value.consumer
- Consumer that accepts the collected mapped values.- Returns:
- Returns the node.
-
isEmpty
public boolean isEmpty()Returns true if this object has no members.- Returns:
- Returns if this object is empty.
-
size
public int size()Returns the number of members.- Returns:
- Returns the number of members.
-
merge
Merge this object node with another, creating a new ObjectNode.Conflicting keys are overwritten by
other
. If the current object has a source location, it is applied to the result. Otherwise, the source location ofother
is applied to the result.- Parameters:
other
- Other object node to merge with.- Returns:
- Returns the merged object node.
-
collect
public static <T> Collector<T,Map<StringNode, collectNode>, ObjectNode> (Function<T, StringNode> keyMapper, Function<T, ToNode> valueMapper) Creates a collector that creates an ObjectNode.- Type Parameters:
T
- Type being collected over (e.g., Map.Entry, Pair, etc.).- Parameters:
keyMapper
- Key mapping function that returns a ToNode.valueMapper
- Value mapping function that returns a ToNode.- Returns:
- Returns the created collector.
-
collectStringKeys
public static <T> Collector<T,Map<StringNode, collectStringKeysNode>, ObjectNode> (Function<T, String> keyMapper, Function<T, ToNode> valueMapper) Creates a collector that creates an ObjectNode.- Type Parameters:
T
- Type being collected over (e.g., Map.Entry, Pair, etc.).- Parameters:
keyMapper
- Key mapping function that returns a string.valueMapper
- Value mapping function that returns a ToNode.- Returns:
- Returns the created collector.
-
equals
-
hashCode
public int hashCode() -
toBuilder
Description copied from interface:ToSmithyBuilder
Take this object and create a builder that contains all of the current property values of this object.- Specified by:
toBuilder
in interfaceToSmithyBuilder<ObjectNode>
- Returns:
- a builder for type T
-