Class NodeMapper
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.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
Specifies the behavior of the mapper when attempting to deserialize an unknown property. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescription<T> T
deserialize
(Node value, Class<T> into) Deserialize a Nodevalue
into an instance ofT
.<T extends Collection<?>,
U, V extends Collection<? extends U>>
VdeserializeCollection
(Node value, Class<T> into, Class<U> members) <T> T
deserializeInto
(Node value, T objectToMutate) Invokes setters on the givenobjectToMutate
from the providedNode
.deserializeMap
(Node value, Class<T> into, Class<U> members) void
disableFromNodeForClass
(Type type) Disables the use offromNode
method for a specific class when deserializing the class.void
disableToNodeForClass
(Type type) Disables the use of thetoNode
method for a specific class when serializing the class as a POJO.void
enableFromNodeForClass
(Type type) Enables the use of theFromNode
method for a specific class when deserializing the class.void
enableToNodeForClass
(Type type) Enables the use of thetoNode
method for a specific class when serializing the class as a POJO.Gets the set of classes wherefromNode
is disabled.Gets the set of classes wheretoNode
is disabled.boolean
Gets whether or not false, empty arrays, and empty objects are omitted from serialized POJOs.boolean
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.
-
Constructor Details
-
NodeMapper
public NodeMapper()
-
-
Method Details
-
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
Sets the behavior of the deserializer when a setting is missing.- Parameters:
whenMissing
- Behavior when a property is not matched to a setter.
-
getWhenMissingSetter
- Returns:
- Gets the behavior of the deserializer when a setting is missing.
-
disableToNodeForClass
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
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
Gets the set of classes wheretoNode
is disabled.- Returns:
- Returns the disabled classes.
-
disableFromNodeForClass
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
Enables the use of theFromNode
method for a specific class when deserializing the class.- Parameters:
type
- Class to enable thefromNode
method deserialization for.
-
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
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
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:
-
deserializeInto
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 Collection<?>,U, V deserializeCollectionV extends Collection<? extends U>> (Node value, Class<T> into, 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:
-
deserializeMap
public <T extends Map<?,?>, V deserializeMapU, V extends Map<String, ? extends U>> (Node value, Class<T> into, 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:
-