Class AbstractXStreamSerializer
- java.lang.Object
-
- io.axoniq.dataprotection.serialization.AbstractXStreamSerializer
-
- All Implemented Interfaces:
Serializer
- Direct Known Subclasses:
XStreamSerializer
public abstract class AbstractXStreamSerializer extends Object implements Serializer
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
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractXStreamSerializer(com.thoughtworks.xstream.XStream xStream)Initialize a generic serializer using the UTF-8 character set.protectedAbstractXStreamSerializer(com.thoughtworks.xstream.XStream xStream, RevisionResolver revisionResolver)Initialize a generic serializer using the UTF-8 character set.protectedAbstractXStreamSerializer(Charset charset, com.thoughtworks.xstream.XStream xStream)Initialize the serializer using the givencharsetandxStreaminstance.protectedAbstractXStreamSerializer(Charset charset, com.thoughtworks.xstream.XStream xStream, RevisionResolver revisionResolver)Initialize the serializer using the givencharsetandxStreaminstance.protectedAbstractXStreamSerializer(Charset charset, com.thoughtworks.xstream.XStream xStream, RevisionResolver revisionResolver, Converter converter)Initialize the serializer using the givencharset,xStreaminstance,revisionResolverandconverter.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaddAlias(String name, Class type)Adds 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.ClassclassForType(SerializedType type)Returns the class for the given type identifier.protected <S,T>
Tconvert(S source, Class<S> sourceType, Class<T> targetType)Convert the givensource, of typesourceTypeto the giventargetType.<S,T>
Tdeserialize(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.CharsetgetCharset()Returns the character set used to convert character to bytes and vice versa.ConvertergetConverter()Returns the Converter used by this serialized.com.thoughtworks.xstream.XStreamgetXStream()Returns 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(Object object, Class<T> expectedType)Serialize the givenobjectinto a Serialized Object containing the givenexpectedRepresentation.SerializedTypetypeForClass(Class type)Returns the type identifier for the given class.
-
-
-
Constructor Detail
-
AbstractXStreamSerializer
protected AbstractXStreamSerializer(com.thoughtworks.xstream.XStream xStream)
Initialize a generic serializer using the UTF-8 character set. The provided XStream instance is used to perform the serialization. AnAnnotationRevisionResolveris used to resolve the revision for serialized objects.- Parameters:
xStream- XStream instance to use
-
AbstractXStreamSerializer
protected AbstractXStreamSerializer(com.thoughtworks.xstream.XStream xStream, RevisionResolver revisionResolver)Initialize a generic serializer using the UTF-8 character set. The provided XStream instance is used to perform the serialization.- Parameters:
xStream- XStream instance to userevisionResolver- The strategy to use to resolve the revision of an object
-
AbstractXStreamSerializer
protected AbstractXStreamSerializer(Charset charset, com.thoughtworks.xstream.XStream xStream)
Initialize the serializer using the givencharsetandxStreaminstance. ThexStreaminstance is configured with several converters for the most common types in Axon. AnAnnotationRevisionResolveris used to resolve revision for serialized objects.- Parameters:
charset- The character set to usexStream- The XStream instance to use
-
AbstractXStreamSerializer
protected AbstractXStreamSerializer(Charset charset, com.thoughtworks.xstream.XStream xStream, RevisionResolver revisionResolver)
Initialize the serializer using the givencharsetandxStreaminstance. ThexStreaminstance is configured with several converters for the most common types in Axon.- Parameters:
charset- The character set to usexStream- The XStream instance to userevisionResolver- The strategy to use to resolve the revision of an object
-
AbstractXStreamSerializer
protected AbstractXStreamSerializer(Charset charset, com.thoughtworks.xstream.XStream xStream, RevisionResolver revisionResolver, Converter converter)
Initialize the serializer using the givencharset,xStreaminstance,revisionResolverandconverter. ThexStreaminstance is configured with several converters for the most common types in Axon.- Parameters:
charset- The character set to usexStream- The XStream instance to userevisionResolver- The strategy to use to resolve the revision of an objectconverter- The converter providing the necessary content converters
-
-
Method Detail
-
registerConverters
protected abstract void registerConverters(ChainingConverter converter)
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
public <T> boolean canSerializeTo(Class<T> expectedRepresentation)
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
public <T> SerializedObject<T> serialize(Object object, Class<T> expectedType)
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
protected <S,T> T convert(S source, Class<S> sourceType, Class<T> targetType)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
public <S,T> T deserialize(SerializedObject<S> serializedObject)
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
public Class classForType(SerializedType type)
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 throws UnknownSerializedTypeException.- Specified by:
classForTypein interfaceSerializer- 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: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
public void addAlias(String name, Class type)
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:
XStream.alias(String, Class)
-
addPackageAlias
public void addPackageAlias(String alias, String pkgName)
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 "axoncore" for the package "org.axonframework.core" will use "axoncore.repository" for the package "org.axonframework.core.repository".- Parameters:
alias- The alias to use.pkgName- The package to use the alias for- See Also:
XStream.aliasPackage(String, String)
-
addFieldAlias
public void addFieldAlias(String alias, Class definedIn, String fieldName)
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:
XStream.aliasField(String, Class, String)
-
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:
XStream
-
getCharset
public Charset 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
public Converter 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
-
-