Class NodeMapper
- java.lang.Object
-
- software.amazon.smithy.model.node.NodeMapper
-
public final class NodeMapper extends java.lang.Object
Serializes and deserializes SmithyNode
values to/from objects.This class does not serialize a
Node
value as a JSON string. It converts Java Object values to and fromNode
values. UseNode.printJson(Node)
to serialize JSON strings from aNode
value.When stable, we may add the ability to add custom serializers and deserializers. Until then, there is no way to customize the serialization and deserialization rules.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
NodeMapper.WhenMissing
Specifies the behavior of the mapper when attempting to deserialize an unknown property.
-
Constructor Summary
Constructors Constructor Description NodeMapper()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> T
deserialize(Node value, java.lang.Class<T> into)
Deserialize a Nodevalue
into an instance ofT
.<T extends java.util.Collection<?>,U,V extends java.util.Collection<? extends U>>
VdeserializeCollection(Node value, java.lang.Class<T> into, java.lang.Class<U> members)
Deserialize a Nodevalue
into aCollection
T
ofU
members.<T> T
deserializeInto(Node value, T objectToMutate)
Invokes setters on the givenobjectToMutate
from the providedNode
.<T extends java.util.Map<?,?>,U,V extends java.util.Map<java.lang.String,? extends U>>
VdeserializeMap(Node value, java.lang.Class<T> into, java.lang.Class<U> members)
Deserialize a Nodevalue
into aMap
T
with String keys andU
values.void
disableFromNodeForClass(java.lang.reflect.Type type)
Disables the use offromNode
method for a specific class when deserializing the class.void
disableToNodeForClass(java.lang.reflect.Type type)
Disables the use of thetoNode
method for a specific class when serializing the class as a POJO.void
enableFromNodeForClass(java.lang.reflect.Type type)
Enables the use of theFromNode
method for a specific class when deserializing the class.void
enableToNodeForClass(java.lang.reflect.Type type)
Enables the use of thetoNode
method for a specific class when serializing the class as a POJO.java.util.Set<java.lang.reflect.Type>
getDisableFromNode()
Gets the set of classes wherefromNode
is disabled.java.util.Set<java.lang.reflect.Type>
getDisableToNode()
Gets the set of classes wheretoNode
is disabled.boolean
getOmitEmptyValues()
Gets whether or not false, empty arrays, and empty objects are omitted from serialized POJOs.boolean
getSerializeNullValues()
NodeMapper.WhenMissing
getWhenMissingSetter()
Node
serialize(java.lang.Object object)
Serializes the givenobject
as aNode
.void
setOmitEmptyValues(boolean omitEmptyValues)
Gets whether or not false, empty arrays, and empty objects are omitted from serialized POJOs.void
setSerializeNullValues(boolean serializeNullValues)
Specifies ifnull
values returned from getters are serialized.void
setWhenMissingSetter(NodeMapper.WhenMissing whenMissing)
Sets the behavior of the deserializer when a setting is missing.
-
-
-
Method Detail
-
setSerializeNullValues
public void setSerializeNullValues(boolean serializeNullValues)
Specifies ifnull
values returned from getters are serialized.- Parameters:
serializeNullValues
- Set to true to serializenull
values.
-
getSerializeNullValues
public boolean getSerializeNullValues()
- Returns:
- Gets whether or not
null
values are serialized.
-
setWhenMissingSetter
public void setWhenMissingSetter(NodeMapper.WhenMissing whenMissing)
Sets the behavior of the deserializer when a setting is missing.- Parameters:
whenMissing
- Behavior when a property is not matched to a setter.
-
getWhenMissingSetter
public NodeMapper.WhenMissing getWhenMissingSetter()
- Returns:
- Gets the behavior of the deserializer when a setting is missing.
-
disableToNodeForClass
public void disableToNodeForClass(java.lang.reflect.Type type)
Disables the use of thetoNode
method for a specific class when serializing the class as a POJO.This method disables a specific concrete class and does not disable subclasses or implementations of an interface.
This is useful when using the NodeMapper inside of a
toNode
implementation.- Parameters:
type
- Class to disable thetoNode
method serialization for.
-
enableToNodeForClass
public void enableToNodeForClass(java.lang.reflect.Type type)
Enables the use of thetoNode
method for a specific class when serializing the class as a POJO.- Parameters:
type
- Class to enable thetoNode
method serialization for.
-
getDisableToNode
public java.util.Set<java.lang.reflect.Type> getDisableToNode()
Gets the set of classes wheretoNode
is disabled.- Returns:
- Returns the disabled classes.
-
disableFromNodeForClass
public void disableFromNodeForClass(java.lang.reflect.Type type)
Disables the use offromNode
method for a specific class when deserializing the class.This method disables a specific concrete class and does not disable subclasses or implementations of an interface.
This is useful when using the NodeMapper inside of a
fromNode
implementation.- Parameters:
type
- Class to disable thefromNode
method deserialization for.
-
enableFromNodeForClass
public void enableFromNodeForClass(java.lang.reflect.Type type)
Enables the use of theFromNode
method for a specific class when deserializing the class.- Parameters:
type
- Class to enable thefromNode
method deserialization for.
-
getDisableFromNode
public java.util.Set<java.lang.reflect.Type> getDisableFromNode()
Gets the set of classes wherefromNode
is disabled.- Returns:
- Returns the disabled classes.
-
getOmitEmptyValues
public boolean getOmitEmptyValues()
Gets whether or not false, empty arrays, and empty objects are omitted from serialized POJOs.- Returns:
- Returns true if empty arrays and POJOs returned from POJO getters are omitted.
-
setOmitEmptyValues
public void setOmitEmptyValues(boolean omitEmptyValues)
Gets whether or not false, empty arrays, and empty objects are omitted from serialized POJOs.- Parameters:
omitEmptyValues
- Set to true if false, empty arrays, and objects returned from POJO getters are omitted.
-
serialize
public Node serialize(java.lang.Object object)
Serializes the givenobject
as aNode
.This method is able to serialize the following types in the given evaluation order:
- A
null
value is serialized as aNullNode
ifgetSerializeNullValues()
returnstrue
. - Instances of
ToNode
will return the result of callingToNode.toNode()
. - Instances of
Optional
will serialize aNullNode
when the Optional is empty, or the result of serializing the value contained in theOptional
when present. String
value is serialized as aStringNode
.Boolean
value orboolean
is serialized as aBooleanNode
.- Any instance of
Number
value is serialized as aNumberNode
. - The
toString
method is called whenURL
,URI
,Pattern
, andPath
are serialized. - A
File
is serialized by serializing the string value ofFile.toURI()
. Enum
value is serialized as aStringNode
by calling itstoString
method.ShapeId
is serialized as aStringNode
that contains the absolute shape ID.- Any instance of a
Map
is supported as long as the key and value of the map are both supported types (note that Map keys must serialize as StringNode). AMap
is converted to anObjectNode
. - Any instance of a
Iterable
is supported as long as the value contained in theIterable
is a supported type. AnIterable
is converted to anArrayNode
. AnIterable
broadly covers many Java types, includingCollection
. - Primitive arrays are converted to an
ArrayNode
if and only if the values contained in the array are one of the supported types supported by the serializer. - Finally, an object is serialized using Bean style semantics; any public getter
(get* or is* method with no arguments) is invoked and it's return value is put in the
ObjectNode
. Each property of the Bean recursively invokes the serializer and must be one of the supported types. Properties associated with a getter that are marked astransient
are not serialized (where an "association" is defined as a class field with the same lowercase name as the suffix of the getter method). For example, given a method "getFoo", both "foo" and "Foo" are checked as associated property names.
- Parameters:
object
- Object to serialize.- Returns:
- Returns the serialized
Node
. - Throws:
NodeSerializationException
- on error.
- A
-
deserialize
public <T> T deserialize(Node value, java.lang.Class<T> into)
Deserialize a Nodevalue
into an instance ofT
.This method can deserialize various kinds of values depending on the given node type and the target type:
null
- String
- Primitive and boxed booleans
- Primitive and boxed
Number
types - Lists and Sets of any support value
- Maps with String keys and values of any supported type
- Direct
Node
toNode
conversions. - Any object that has a public static
fromNode
method that accepts aNode
and returns an instance of the object. - Strings are deserialized to enums by finding the first enum value that has a
toString
method that matches the string value. - Built-in support for URI, URL, Pattern, Path, and File
- When deserializing an object, any target object that provides a public static method named
builder
that returns an instance ofSmithyBuilder
is invoked, and the builder is then mutated using bean like setters (with an optional "set") prefix, until finally, the build method is called and its result is returned. - When deserializing an object, the last thing tried is to find a public, zero-arg constructor, and then the object is mutated using bean-style setter conventions for each key-value pair.
- NodeMapper does not support non-static inner classes, classes with generic parameters, or generic arrays. Support for these may be added in the future.
Objects with a public method named
sourceLocation
orsetSourceLocation
are invoked and provided the source location of the deserializedvalue
.- Type Parameters:
T
- Type of value to create.- Parameters:
value
- Value to deserialize.into
- Class to create.- Returns:
- Returns the created value.
- Throws:
NodeDeserializationException
- on error.- See Also:
deserializeCollection(Node, Class, Class)
,deserializeMap(Node, Class, Class)
-
deserializeInto
public <T> T deserializeInto(Node value, T objectToMutate)
Invokes setters on the givenobjectToMutate
from the providedNode
.- Type Parameters:
T
- The value to mutate using Bean style setters.- Parameters:
value
- Value to deserialize.objectToMutate
- Object to mutate and populate from the node.- Returns:
- Returns the passed in value.
- Throws:
NodeDeserializationException
- on error.
-
deserializeCollection
public <T extends java.util.Collection<?>,U,V extends java.util.Collection<? extends U>> V deserializeCollection(Node value, java.lang.Class<T> into, java.lang.Class<U> members)
Deserialize a Nodevalue
into aCollection
T
ofU
members.This method is necessary because of Java's runtime type erasure.
- Type Parameters:
T
- Type of collection value to create.U
- Type contained within the collection.V
- Returned collection type.- Parameters:
value
- Value to deserialize.into
- Collection class to create.members
- The collection's parametric type.- Returns:
- Returns the created collection.
- Throws:
NodeDeserializationException
- on error.- See Also:
deserialize(Node, Class)
-
deserializeMap
public <T extends java.util.Map<?,?>,U,V extends java.util.Map<java.lang.String,? extends U>> V deserializeMap(Node value, java.lang.Class<T> into, java.lang.Class<U> members)
Deserialize a Nodevalue
into aMap
T
with String keys andU
values.This method is necessary because of Java's runtime type erasure.
- Type Parameters:
T
- Type of map value to create.U
- Type contained within the map values.V
- Returned map type.- Parameters:
value
- Value to deserialize.into
- Map class to create.members
- The maps's parametric type.- Returns:
- Returns the created map.
- Throws:
NodeDeserializationException
- on error.- See Also:
deserialize(Node, Class)
-
-