org.axonframework.serializer
Class MessageSerializer

java.lang.Object
  extended by org.axonframework.serializer.MessageSerializer
All Implemented Interfaces:
Serializer

public class MessageSerializer
extends Object
implements Serializer

Wrapper around a serializer that provides SerializationAware support. This class can either be used as a wrapper around a Serializer, or as a static utility class.

The serializePayload and serializeMetaData methods on this class are aware of SerializationAware messages. When a Message implements that interface, serialization is delegated to that message to allow performance optimizations.

Using this class to serialize the payload and meta-data of Message is preferred over the serialization using the Serializer class.

Since:
2.0
Author:
Allard Buijze

Constructor Summary
MessageSerializer(Serializer serializer)
          Initializes the MessageSerializer as a wrapper around the given serializer.
 
Method Summary
<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.
<S,T> T
deserialize(SerializedObject<S> serializedObject)
          Deserializes the first object read from the given bytes.
 ConverterFactory getConverterFactory()
          Returns the converter factory used by this Serializer to convert between serialized representations.
<T> SerializedObject<T>
serialize(Object object, Class<T> expectedRepresentation)
          Serialize the given object into a Serialized Object containing the given expectedRepresentation.
<T> SerializedObject<T>
serializeMetaData(Message<?> message, Class<T> expectedRepresentation)
          Serialize the meta data of given message to the given expectedRepresentation.
static
<T> SerializedObject<T>
serializeMetaData(Message<?> message, Serializer serializer, Class<T> expectedRepresentation)
          Utility method that serializes the meta data of the given message using given serializer and expectedRepresentation.
<T> SerializedObject<T>
serializePayload(Message<?> message, Class<T> expectedRepresentation)
          Serialize the payload of given message to the given expectedRepresentation.
static
<T> SerializedObject<T>
serializePayload(Message<?> message, Serializer serializer, Class<T> expectedRepresentation)
          Utility method that serializes the payload of the given message using given serializer and expectedRepresentation.
 SerializedType typeForClass(Class type)
          Returns the type identifier for the given class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MessageSerializer

public MessageSerializer(Serializer serializer)
Initializes the MessageSerializer as a wrapper around the given serializer.

Parameters:
serializer - the serializer to serialize and deserialize objects with
Method Detail

serializePayload

public static <T> SerializedObject<T> serializePayload(Message<?> message,
                                                       Serializer serializer,
                                                       Class<T> expectedRepresentation)
Utility method that serializes the payload of the given message using given serializer and expectedRepresentation. This method will verify if the eventMessage is SerializationAware.

Type Parameters:
T - The data type to serialize to
Parameters:
message - The message containing the payload to serialize
serializer - The serializer to serialize the payload with
expectedRepresentation - The data type to serialize to
Returns:
a Serialized Object containing the serialized for of the message's payload

serializeMetaData

public static <T> SerializedObject<T> serializeMetaData(Message<?> message,
                                                        Serializer serializer,
                                                        Class<T> expectedRepresentation)
Utility method that serializes the meta data of the given message using given serializer and expectedRepresentation. This method will verify if the eventMessage is SerializationAware.

Type Parameters:
T - The data type to serialize to
Parameters:
message - The message containing the meta data to serialize
serializer - The serializer to serialize the meta data with
expectedRepresentation - The data type to serialize to
Returns:
a Serialized Object containing the serialized for of the message's meta data

serializePayload

public <T> SerializedObject<T> serializePayload(Message<?> message,
                                                Class<T> expectedRepresentation)
Serialize the payload of given message to the given expectedRepresentation.

Type Parameters:
T - The representation of the serialized data
Parameters:
message - The message containing the payload to serialize
expectedRepresentation - The representation of the serialized data
Returns:
A serialized object containing the serialized representation of the message's payload

serializeMetaData

public <T> SerializedObject<T> serializeMetaData(Message<?> message,
                                                 Class<T> expectedRepresentation)
Serialize the meta data of given message to the given expectedRepresentation.

Type Parameters:
T - The representation of the serialized data
Parameters:
message - The message containing the meta data to serialize
expectedRepresentation - The representation of the serialized data
Returns:
A serialized object containing the serialized representation of the message's meta data

serialize

public <T> SerializedObject<T> serialize(Object object,
                                         Class<T> expectedRepresentation)
Description copied from interface: Serializer
Serialize the given object into a Serialized Object containing the given expectedRepresentation.

Use Serializer.canSerializeTo(Class) to detect whether the expectedRepresentation is supported by this serializer.

Specified by:
serialize in interface Serializer
Type Parameters:
T - The expected data type representing the serialized object
Parameters:
object - The object to serialize
expectedRepresentation - The expected data type representing the serialized object
Returns:
the instance representing the serialized object.

canSerializeTo

public <T> boolean canSerializeTo(Class<T> expectedRepresentation)
Description copied from interface: Serializer
Indicates whether this Serializer is capable of serializing to the given 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.

Specified by:
canSerializeTo in interface Serializer
Type Parameters:
T - The type of data a Serialized Object should contain
Parameters:
expectedRepresentation - The type of data a Serialized Object should contain
Returns:
true if the expectedRepresentation is supported, otherwise false.

deserialize

public <S,T> T deserialize(SerializedObject<S> serializedObject)
Description copied from interface: Serializer
Deserializes the first object read from the given bytes. The bytes are not consumed from the array or modified in any way. The resulting object instance is cast to the expected type.

Specified by:
deserialize in interface Serializer
Type Parameters:
S - The data type of the serialized object
T - The expected deserialized type
Parameters:
serializedObject - the instance describing the type of object and the bytes providing the serialized data
Returns:
the serialized object, cast to the expected type

classForType

public Class classForType(SerializedType type)
Description copied from interface: Serializer
Returns the class for the given type identifier. The result of this method must guarantee that the deserialized SerializedObject with the given 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 throws UnknownSerializedTypeException.

Specified by:
classForType in interface Serializer
Parameters:
type - The type identifier of the object
Returns:
the Class representing the type of the serialized Object

typeForClass

public SerializedType typeForClass(Class type)
Description copied from interface: Serializer
Returns the type identifier for the given class. This is the type identifier of the Serialized object as returned by Serializer.serialize(Object, Class).

Specified by:
typeForClass in interface Serializer
Parameters:
type - Class representing the type of the serializable Object.
Returns:
The type identifier of the object

getConverterFactory

public ConverterFactory getConverterFactory()
Description copied from interface: Serializer
Returns the converter factory used by this Serializer to convert between serialized representations. Generally, this ConverterFactory depends on the type of data the serializer serializes to.

Specified by:
getConverterFactory in interface Serializer
Returns:
the converter factory used by this Serializer


Copyright © 2010-2016. All Rights Reserved.