public class JacksonSerializer extends Object implements Serializer
Modifier and Type | Class and Description |
---|---|
static class |
JacksonSerializer.Builder
Builder class to instantiate a
JacksonSerializer . |
Modifier | Constructor and Description |
---|---|
protected |
JacksonSerializer(JacksonSerializer.Builder builder)
Instantiate a
JacksonSerializer based on the fields contained in the JacksonSerializer.Builder . |
Modifier and Type | Method and Description |
---|---|
static JacksonSerializer.Builder |
builder()
Instantiate a Builder to be able to create a
JacksonSerializer . |
<T> boolean |
canSerializeTo(Class<T> expectedRepresentation)
Indicates whether this Serializer is capable of serializing to the given
expectedRepresentation . |
Class |
classForType(SerializedType type)
Returns the class for the given type identifier.
|
static JacksonSerializer |
defaultSerializer()
Instantiate a default
JacksonSerializer . |
<S,T> T |
deserialize(SerializedObject<S> serializedObject)
Deserializes the first object read from the given
bytes . |
Converter |
getConverter()
Returns the
Converter used by this Serializer to convert between serialized representations. |
com.fasterxml.jackson.databind.ObjectMapper |
getObjectMapper()
Returns the ObjectMapper used by this serializer, allowing for configuration of the serialization settings.
|
protected com.fasterxml.jackson.databind.ObjectReader |
getReader(Class<?> type)
Provides the ObjectReader, with which objects are read from the JSON form.
|
protected RevisionResolver |
getRevisionResolver()
Returns the revision resolver used by this serializer.
|
protected com.fasterxml.jackson.databind.ObjectWriter |
getWriter()
Provides the ObjectWriter, with which objects are serialized to JSON form.
|
protected void |
registerConverters(ChainingConverter converter)
Registers converters with the given
converter which depend on the actual contents of the serialized form
to represent a JSON format. |
protected String |
resolveClassName(SerializedType serializedType)
Resolve the class name from the given
serializedType . |
<T> SerializedObject<T> |
serialize(Object object,
Class<T> expectedRepresentation)
Serialize the given
object into a Serialized Object containing the given
expectedRepresentation . |
SerializedType |
typeForClass(Class type)
Returns the type identifier for the given class.
|
protected JacksonSerializer(JacksonSerializer.Builder builder)
JacksonSerializer
based on the fields contained in the JacksonSerializer.Builder
.
Upon instantiation, the ObjectMapper will get two modules registered to it by default, (1) the MetaDataDeserializer
and the (2) JavaTimeModule
. Lastly, if the provided converter is of type
ChainingConverter, the registerConverters(org.axonframework.serialization.ChainingConverter)
is performed to automatically add the JsonNodeToByteArrayConverter
and ByteArrayToJsonNodeConverter
.
builder
- the JacksonSerializer.Builder
used to instantiate a JacksonSerializer
instancepublic static JacksonSerializer.Builder builder()
JacksonSerializer
.
The RevisionResolver
is defaulted to an AnnotationRevisionResolver
, the Converter
to a
ChainingConverter
, the ObjectMapper
defaults to a ObjectMapper()
result and
the ClassLoader
to the ClassLoader of this
class.
Upon instantiation, the ObjectMapper will get two modules registered to it by default, (1) the MetaDataDeserializer
and the (2) JavaTimeModule
. Lastly, if the provided converter is of type
ChainingConverter, the registerConverters(org.axonframework.serialization.ChainingConverter)
is performed to automatically add the JsonNodeToByteArrayConverter
and ByteArrayToJsonNodeConverter
.
JacksonSerializer
public static JacksonSerializer defaultSerializer()
JacksonSerializer
.
The RevisionResolver
is defaulted to an AnnotationRevisionResolver
, the Converter
to a
ChainingConverter
, the ObjectMapper
defaults to a ObjectMapper()
result and
the ClassLoader
to the ClassLoader of this
class.
Upon instantiation, the ObjectMapper will get two modules registered to it by default, (1) the MetaDataDeserializer
and the (2) JavaTimeModule
. Lastly, if the provided converter is of type
ChainingConverter, the registerConverters(org.axonframework.serialization.ChainingConverter)
is performed to automatically add the JsonNodeToByteArrayConverter
and ByteArrayToJsonNodeConverter
.
JacksonSerializer
protected void registerConverters(ChainingConverter converter)
converter
which depend on the actual contents of the serialized form
to represent a JSON format.converter
- The ChainingConverter instance to register the converters with.public <T> SerializedObject<T> serialize(Object object, @Nonnull Class<T> expectedRepresentation)
Serializer
object
into a Serialized Object containing the given
expectedRepresentation
.
Use Serializer.canSerializeTo(Class)
to detect whether the expectedRepresentation
is supported by
this serializer.serialize
in interface Serializer
T
- The expected data type representing the serialized objectobject
- The object to serializeexpectedRepresentation
- The expected data type representing the serialized objectpublic final com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
protected com.fasterxml.jackson.databind.ObjectWriter getWriter()
protected com.fasterxml.jackson.databind.ObjectReader getReader(Class<?> type)
type
- The type of object to create a reader forpublic <T> boolean canSerializeTo(@Nonnull Class<T> expectedRepresentation)
Serializer
expectedRepresentation
.
When true
, this does *not* guarantee that the serialization and (optional) conversion will also
succeed when executed. For example, when a serializer produces a byte[]
containing JSON, trying to
convert to a Dom4J Document will fail, even though this serializer has a converter to convert
byte[]
to Dom4J instances.canSerializeTo
in interface Serializer
T
- The type of data a Serialized Object should containexpectedRepresentation
- The type of data a Serialized Object should containtrue
if the expectedRepresentation
is supported, otherwise false
.public <S,T> T deserialize(@Nonnull SerializedObject<S> serializedObject)
Serializer
bytes
. The bytes
are not consumed
from the array or modified in any way. The resulting object instance is cast to the expected type.deserialize
in interface Serializer
S
- The data type of the serialized objectT
- The expected deserialized typeserializedObject
- the instance describing the type of object and the bytes providing the serialized datapublic Class classForType(@Nonnull SerializedType type)
Serializer
type
is an instance of the returned Class.
If a class cannot be resolved (i.e. because the class is not available on this JVM's classpath) this method
returns an UnknownSerializedType
instance which proides access to the raw underlying data.classForType
in interface Serializer
type
- The type identifier of the objectprotected String resolveClassName(SerializedType serializedType)
serializedType
. This method may be overridden to customize the
names used to denote certain classes, for example, by leaving out a certain base package for brevity.serializedType
- The serialized type to resolve the class name forpublic SerializedType typeForClass(Class type)
Serializer
Serializer.serialize(Object, Class)
.typeForClass
in interface Serializer
type
- Class representing the type of the serializable Object.public Converter getConverter()
Serializer
Converter
used by this Serializer to convert between serialized representations. Generally,
this Converter depends on the type of data the serializer serializes to.getConverter
in interface Serializer
protected RevisionResolver getRevisionResolver()
Copyright © 2010–2024. All rights reserved.