Package org.axonframework.serialization
Class AbstractXStreamSerializer
java.lang.Object
org.axonframework.serialization.AbstractXStreamSerializer
- All Implemented Interfaces:
Serializer
- Direct Known Subclasses:
XStreamSerializer
Abstract implementation for XStream based serializers. It provides some helper methods and configuration features
independent of the actual format used to marshal to.
- Since:
- 2.0
- Author:
- Allard Buijze
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAbstract Builder class to instantiateAbstractXStreamSerializer. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedInstantiate aAbstractXStreamSerializerbased on the fields contained in theAbstractXStreamSerializer.Builder. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds an alias to use instead of the fully qualified class name.voidaddFieldAlias(String alias, Class definedIn, String fieldName) Adds an alias to use for a given field in the given class.voidaddPackageAlias(String alias, String pkgName) Add an alias for a package.<T> booleancanSerializeTo(Class<T> expectedRepresentation) Indicates whether this Serializer is capable of serializing to the givenexpectedRepresentation.classForType(SerializedType type) Returns the class for the given type identifier.protected <S,T> T Convert the givensource, of typesourceTypeto the giventargetType.<S,T> T deserialize(SerializedObject<S> serializedObject) Deserializes the first object read from the givenbytes.protected abstract ObjectdoDeserialize(SerializedObject serializedObject, com.thoughtworks.xstream.XStream xStream) Deserialize the givenserializedObject.protected abstract <T> TdoSerialize(Object object, Class<T> expectedFormat, com.thoughtworks.xstream.XStream xStream) Serialize the givenobjectto the givenexpectedFormat.Returns the character set used to convert character to bytes and vice versa.Returns the Converter used by this serialized.com.thoughtworks.xstream.XStreamReturns a reference to the underlyingXStreaminstance, that does the actual serialization.protected abstract voidregisterConverters(ChainingConverter converter) Registers any converters that are specific to the type of content written by this serializer.<T> SerializedObject<T> Serialize the givenobjectinto a Serialized Object containing the givenexpectedRepresentation.typeForClass(Class type) Returns the type identifier for the given class.
-
Constructor Details
-
AbstractXStreamSerializer
Instantiate aAbstractXStreamSerializerbased on the fields contained in theAbstractXStreamSerializer.Builder.- Parameters:
builder- theAbstractXStreamSerializer.Builderused to instantiate aAbstractXStreamSerializerinstance
-
-
Method Details
-
registerConverters
Registers any converters that are specific to the type of content written by this serializer.- Parameters:
converter- the Converter to register the converters with
-
canSerializeTo
Description copied from interface:SerializerIndicates whether this Serializer is capable of serializing to the givenexpectedRepresentation. Whentrue, this does *not* guarantee that the serialization and (optional) conversion will also succeed when executed. For example, when a serializer produces abyte[]containing JSON, trying to convert to a Dom4J Document will fail, even though this serializer has a converter to convertbyte[]to Dom4J instances.- Specified by:
canSerializeToin interfaceSerializer- Type Parameters:
T- The type of data a Serialized Object should contain- Parameters:
expectedRepresentation- The type of data a Serialized Object should contain- Returns:
trueif theexpectedRepresentationis supported, otherwisefalse.
-
serialize
Description copied from interface:SerializerSerialize the givenobjectinto a Serialized Object containing the givenexpectedRepresentation. UseSerializer.canSerializeTo(Class)to detect whether theexpectedRepresentationis supported by this serializer.- Specified by:
serializein interfaceSerializer- Type Parameters:
T- The expected data type representing the serialized object- Parameters:
object- The object to serializeexpectedType- The expected data type representing the serialized object- Returns:
- the instance representing the serialized object.
-
doSerialize
protected abstract <T> T doSerialize(Object object, Class<T> expectedFormat, com.thoughtworks.xstream.XStream xStream) Serialize the givenobjectto the givenexpectedFormat. The subclass may useconvert(Object, Class, Class)to convert the result of the serialization to the expected type.- Type Parameters:
T- The format in which the serialized object must be returned- Parameters:
object- The object to serializeexpectedFormat- The format in which the serialized object must be returnedxStream- The XStream instance to serialize with- Returns:
- The serialized object
-
doDeserialize
protected abstract Object doDeserialize(SerializedObject serializedObject, com.thoughtworks.xstream.XStream xStream) Deserialize the givenserializedObject.- Parameters:
serializedObject- The instance containing the serialized format of the objectxStream- The XStream instance to deserialize with- Returns:
- the deserialized object
-
convert
Convert the givensource, of typesourceTypeto the giventargetType.- Type Parameters:
S- The type of data that needs to be convertedT- The target type of the conversion- Parameters:
source- The object to convertsourceType- The source type of the conversiontargetType- The target type of the conversion- Returns:
- The converted object
-
deserialize
Description copied from interface:SerializerDeserializes the first object read from the givenbytes. Thebytesare not consumed from the array or modified in any way. The resulting object instance is cast to the expected type.- Specified by:
deserializein interfaceSerializer- Type Parameters:
S- The data type of the serialized objectT- 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
Description copied from interface:SerializerReturns the class for the given type identifier. The result of this method must guarantee that the deserialized SerializedObject with the giventypeis 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 anUnknownSerializedTypeinstance which proides access to the raw underlying data.- Specified by:
classForTypein interfaceSerializer- Parameters:
type- The type identifier of the object- Returns:
- the Class representing the type of the serialized Object
-
typeForClass
Description copied from interface:SerializerReturns the type identifier for the given class. This is the type identifier of the Serialized object as returned bySerializer.serialize(Object, Class).- Specified by:
typeForClassin interfaceSerializer- Parameters:
type- Class representing the type of the serializable Object.- Returns:
- The type identifier of the object
-
addAlias
Adds an alias to use instead of the fully qualified class name.- Parameters:
name- The alias to usetype- The Class to use the alias for- See Also:
-
addPackageAlias
Add an alias for a package. This allows long package names to be shortened considerably. Will also use the alias for sub-packages of the provided package. E.g. an alias of "axon-modelling" for the package "org.axonframework.modelling" will use "axon-modelling.command" for the package "org.axonframework.modelling.command".- Parameters:
alias- The alias to use.pkgName- The package to use the alias for- See Also:
-
addFieldAlias
Adds an alias to use for a given field in the given class.- Parameters:
alias- The alias to use instead of the original field namedefinedIn- The class that defines the field.fieldName- The name of the field to use the alias for- See Also:
-
getXStream
public com.thoughtworks.xstream.XStream getXStream()Returns a reference to the underlyingXStreaminstance, that does the actual serialization.- Returns:
- the XStream instance that does the actual (de)serialization.
- See Also:
-
getCharset
Returns the character set used to convert character to bytes and vice versa.- Returns:
- the character set used to convert character to bytes and vice versa
-
getConverter
Returns the Converter used by this serialized. The converter factory allows registration of ContentTypeConverters needed by the upcasters.- Specified by:
getConverterin interfaceSerializer- Returns:
- the Converter used by this serializer
-