Class NodeMapper
- java.lang.Object
-
- software.amazon.smithy.model.node.NodeMapper
-
public final class NodeMapper extends java.lang.ObjectSerializes and deserializes SmithyNodevalues to/from objects.This class does not serialize a
Nodevalue as a JSON string. It converts Java Object values to and fromNodevalues. UseNode.printJson(Node)to serialize JSON strings from aNodevalue.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 classNodeMapper.WhenMissingSpecifies 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> Tdeserialize(Node value, java.lang.Class<T> into)Deserialize a Nodevalueinto 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 Nodevalueinto aCollectionTofUmembers.<T> TdeserializeInto(Node value, T objectToMutate)Invokes setters on the givenobjectToMutatefrom 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 Nodevalueinto aMapTwith String keys andUvalues.voiddisableFromNodeForClass(java.lang.reflect.Type type)Disables the use offromNodemethod for a specific class when deserializing the class.voiddisableToNodeForClass(java.lang.reflect.Type type)Disables the use of thetoNodemethod for a specific class when serializing the class as a POJO.voidenableFromNodeForClass(java.lang.reflect.Type type)Enables the use of theFromNodemethod for a specific class when deserializing the class.voidenableToNodeForClass(java.lang.reflect.Type type)Enables the use of thetoNodemethod for a specific class when serializing the class as a POJO.java.util.Set<java.lang.reflect.Type>getDisableFromNode()Gets the set of classes wherefromNodeis disabled.java.util.Set<java.lang.reflect.Type>getDisableToNode()Gets the set of classes wheretoNodeis disabled.booleangetOmitEmptyValues()Gets whether or not false, empty arrays, and empty objects are omitted from serialized POJOs.booleangetSerializeNullValues()NodeMapper.WhenMissinggetWhenMissingSetter()Nodeserialize(java.lang.Object object)Serializes the givenobjectas aNode.voidsetOmitEmptyValues(boolean omitEmptyValues)Gets whether or not false, empty arrays, and empty objects are omitted from serialized POJOs.voidsetSerializeNullValues(boolean serializeNullValues)Specifies ifnullvalues returned from getters are serialized.voidsetWhenMissingSetter(NodeMapper.WhenMissing whenMissing)Sets the behavior of the deserializer when a setting is missing.
-
-
-
Method Detail
-
setSerializeNullValues
public void setSerializeNullValues(boolean serializeNullValues)
Specifies ifnullvalues returned from getters are serialized.- Parameters:
serializeNullValues- Set to true to serializenullvalues.
-
getSerializeNullValues
public boolean getSerializeNullValues()
- Returns:
- Gets whether or not
nullvalues 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 thetoNodemethod 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
toNodeimplementation.- Parameters:
type- Class to disable thetoNodemethod serialization for.
-
enableToNodeForClass
public void enableToNodeForClass(java.lang.reflect.Type type)
Enables the use of thetoNodemethod for a specific class when serializing the class as a POJO.- Parameters:
type- Class to enable thetoNodemethod serialization for.
-
getDisableToNode
public java.util.Set<java.lang.reflect.Type> getDisableToNode()
Gets the set of classes wheretoNodeis disabled.- Returns:
- Returns the disabled classes.
-
disableFromNodeForClass
public void disableFromNodeForClass(java.lang.reflect.Type type)
Disables the use offromNodemethod 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
fromNodeimplementation.- Parameters:
type- Class to disable thefromNodemethod deserialization for.
-
enableFromNodeForClass
public void enableFromNodeForClass(java.lang.reflect.Type type)
Enables the use of theFromNodemethod for a specific class when deserializing the class.- Parameters:
type- Class to enable thefromNodemethod deserialization for.
-
getDisableFromNode
public java.util.Set<java.lang.reflect.Type> getDisableFromNode()
Gets the set of classes wherefromNodeis 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 givenobjectas aNode.This method is able to serialize the following types in the given evaluation order:
- A
nullvalue is serialized as aNullNodeifgetSerializeNullValues()returnstrue. - Instances of
ToNodewill return the result of callingToNode.toNode(). - Instances of
Optionalwill serialize aNullNodewhen the Optional is empty, or the result of serializing the value contained in theOptionalwhen present. Stringvalue is serialized as aStringNode.Booleanvalue orbooleanis serialized as aBooleanNode.- Any instance of
Numbervalue is serialized as aNumberNode. - The
toStringmethod is called whenURL,URI,Pattern, andPathare serialized. - A
Fileis serialized by serializing the string value ofFile.toURI(). Enumvalue is serialized as aStringNodeby calling itstoStringmethod.ShapeIdis serialized as aStringNodethat contains the absolute shape ID.- Any instance of a
Mapis supported as long as the key and value of the map are both supported types (note that Map keys must serialize as StringNode). AMapis converted to anObjectNode. - Any instance of a
Iterableis supported as long as the value contained in theIterableis a supported type. AnIterableis converted to anArrayNode. AnIterablebroadly covers many Java types, includingCollection. - Primitive arrays are converted to an
ArrayNodeif 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 astransientare 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 Nodevalueinto 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
Numbertypes - Lists and Sets of any support value
- Maps with String keys and values of any supported type
- Direct
NodetoNodeconversions. - Any object that has a public static
fromNodemethod that accepts aNodeand returns an instance of the object. - Strings are deserialized to enums by finding the first enum value that has a
toStringmethod 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
builderthat returns an instance ofSmithyBuilderis 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
sourceLocationorsetSourceLocationare 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 givenobjectToMutatefrom 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 Nodevalueinto aCollectionTofUmembers.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 Nodevalueinto aMapTwith String keys andUvalues.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)
-
-