public final class NodeMapper
extends java.lang.Object
Node
values to/from objects.
This class does not serialize a Node
value as a JSON
string. It converts Java Object values to and from Node
values.
Use Node.printJson(Node)
to serialize JSON strings from a
Node
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.
Modifier and Type | Class and Description |
---|---|
static class |
NodeMapper.WhenMissing
Specifies the behavior of the mapper when attempting to deserialize an unknown property.
|
Constructor and Description |
---|
NodeMapper() |
Modifier and Type | Method and Description |
---|---|
<T> T |
deserialize(Node value,
java.lang.Class<T> into)
Deserialize a Node
value into an instance of T . |
<T extends java.util.Collection<?>,U,V extends java.util.Collection<? extends U>> |
deserializeCollection(Node value,
java.lang.Class<T> into,
java.lang.Class<U> members)
Deserialize a Node
value into a Collection T of U members. |
<T> T |
deserializeInto(Node value,
T objectToMutate)
Invokes setters on the given
objectToMutate from the provided
Node . |
<T extends java.util.Map<?,?>,U,V extends java.util.Map<java.lang.String,? extends U>> |
deserializeMap(Node value,
java.lang.Class<T> into,
java.lang.Class<U> members)
Deserialize a Node
value into a Map T
with String keys and U values. |
void |
disableFromNodeForClass(java.lang.reflect.Type type)
Disables the use of
fromNode method for a specific class
when deserializing the class. |
void |
disableToNodeForClass(java.lang.reflect.Type type)
Disables the use of the
toNode method for a specific class
when serializing the class as a POJO. |
void |
enableFromNodeForClass(java.lang.reflect.Type type)
Enables the use of the
FromNode method for a specific class
when deserializing the class. |
void |
enableToNodeForClass(java.lang.reflect.Type type)
Enables the use of the
toNode 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 where
fromNode is disabled. |
java.util.Set<java.lang.reflect.Type> |
getDisableToNode()
Gets the set of classes where
toNode 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 given
object as a Node . |
void |
setOmitEmptyValues(boolean omitEmptyValues)
Gets whether or not false, empty arrays, and empty objects are omitted from serialized POJOs.
|
void |
setSerializeNullValues(boolean serializeNullValues)
Specifies if
null values returned from getters are serialized. |
void |
setWhenMissingSetter(NodeMapper.WhenMissing whenMissing)
Sets the behavior of the deserializer when a setting is missing.
|
public void setSerializeNullValues(boolean serializeNullValues)
null
values returned from getters are serialized.serializeNullValues
- Set to true to serialize null
values.public boolean getSerializeNullValues()
null
values are serialized.public void setWhenMissingSetter(NodeMapper.WhenMissing whenMissing)
whenMissing
- Behavior when a property is not matched to a setter.public NodeMapper.WhenMissing getWhenMissingSetter()
public void disableToNodeForClass(java.lang.reflect.Type type)
toNode
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.
type
- Class to disable the toNode
method serialization for.public void enableToNodeForClass(java.lang.reflect.Type type)
toNode
method for a specific class
when serializing the class as a POJO.type
- Class to enable the toNode
method serialization for.public java.util.Set<java.lang.reflect.Type> getDisableToNode()
toNode
is disabled.public void disableFromNodeForClass(java.lang.reflect.Type type)
fromNode
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.
type
- Class to disable the fromNode
method deserialization for.public void enableFromNodeForClass(java.lang.reflect.Type type)
FromNode
method for a specific class
when deserializing the class.type
- Class to enable the fromNode
method deserialization for.public java.util.Set<java.lang.reflect.Type> getDisableFromNode()
fromNode
is disabled.public boolean getOmitEmptyValues()
public void setOmitEmptyValues(boolean omitEmptyValues)
omitEmptyValues
- Set to true if false, empty arrays, and objects returned from POJO getters are omitted.public Node serialize(java.lang.Object object)
object
as a Node
.
This method is able to serialize the following types in the given evaluation order:
null
value is serialized as a NullNode
if getSerializeNullValues()
returns true
.
ToNode
will return the result of calling ToNode.toNode()
.
Optional
will serialize a NullNode
when the Optional is empty, or
the result of serializing the value contained in the Optional
when present.
String
value is serialized as a StringNode
.
Boolean
value or boolean
is serialized as a BooleanNode
.
Number
value is serialized as a NumberNode
.
toString
method is called when URL
, URI
, Pattern
, and
Path
are serialized.
File
is serialized by serializing the string value of File.toURI()
.
Enum
value is serialized as a StringNode
by calling its toString
method.
ShapeId
is serialized as a StringNode
that contains the absolute shape ID.
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). A Map
is converted to
an ObjectNode
.
Iterable
is supported as long as the value contained in the
Iterable
is a supported type. An Iterable
is converted to an ArrayNode
.
An Iterable
broadly covers many Java types, including Collection
.
ArrayNode
if and only if the values contained in the
array are one of the supported types supported by the serializer.
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 as transient
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.
object
- Object to serialize.Node
.NodeSerializationException
- on error.public <T> T deserialize(Node value, java.lang.Class<T> into)
value
into an instance of T
.
This method can deserialize various kinds of values depending on the given node type and the target type:
null
Number
typesNode
to Node
conversions.fromNode
method that accepts a
Node
and returns an instance of the object.toString
method that matches the string value.builder
that returns an instance of SmithyBuilder
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.Objects with a public method named sourceLocation
or setSourceLocation
are invoked and provided the source location of the deserialized value
.
T
- Type of value to create.value
- Value to deserialize.into
- Class to create.NodeDeserializationException
- on error.deserializeCollection(Node, Class, Class)
,
deserializeMap(Node, Class, Class)
public <T> T deserializeInto(Node value, T objectToMutate)
objectToMutate
from the provided
Node
.T
- The value to mutate using Bean style setters.value
- Value to deserialize.objectToMutate
- Object to mutate and populate from the node.NodeDeserializationException
- on error.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)
value
into a Collection
T
of U
members.
This method is necessary because of Java's runtime type erasure.
T
- Type of collection value to create.U
- Type contained within the collection.V
- Returned collection type.value
- Value to deserialize.into
- Collection class to create.members
- The collection's parametric type.NodeDeserializationException
- on error.deserialize(Node, Class)
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)
value
into a Map
T
with String keys and U
values.
This method is necessary because of Java's runtime type erasure.
T
- Type of map value to create.U
- Type contained within the map values.V
- Returned map type.value
- Value to deserialize.into
- Map class to create.members
- The maps's parametric type.NodeDeserializationException
- on error.deserialize(Node, Class)