Class 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 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. An AnnotationRevisionResolver is 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 use
        revisionResolver - 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 given charset and xStream instance. The xStream instance is configured with several converters for the most common types in Axon. An AnnotationRevisionResolver is used to resolve revision for serialized objects.
        Parameters:
        charset - The character set to use
        xStream - The XStream instance to use
      • AbstractXStreamSerializer

        protected AbstractXStreamSerializer​(Charset charset,
                                            com.thoughtworks.xstream.XStream xStream,
                                            RevisionResolver revisionResolver)
        Initialize the serializer using the given charset and xStream instance. The xStream instance is configured with several converters for the most common types in Axon.
        Parameters:
        charset - The character set to use
        xStream - The XStream instance to use
        revisionResolver - 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 given charset, xStream instance, revisionResolver and converter. The xStream instance is configured with several converters for the most common types in Axon.
        Parameters:
        charset - The character set to use
        xStream - The XStream instance to use
        revisionResolver - The strategy to use to resolve the revision of an object
        converter - 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: 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.
      • serialize

        public <T> SerializedObject<T> serialize​(Object object,
                                                 Class<T> expectedType)
        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
        expectedType - 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 given object to the given expectedFormat. The subclass may use convert(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 serialize
        expectedFormat - The format in which the serialized object must be returned
        xStream - The XStream instance to serialize with
        Returns:
        The serialized object
      • doDeserialize

        protected abstract Object doDeserialize​(SerializedObject serializedObject,
                                                com.thoughtworks.xstream.XStream xStream)
        Deserialize the given serializedObject.
        Parameters:
        serializedObject - The instance containing the serialized format of the object
        xStream - 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 given source, of type sourceType to the given targetType.
        Type Parameters:
        S - The type of data that needs to be converted
        T - The target type of the conversion
        Parameters:
        source - The object to convert
        sourceType - The source type of the conversion
        targetType - The target type of the conversion
        Returns:
        The converted object
      • 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
      • 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 use
        type - 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 name
        definedIn - 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 underlying XStream instance, 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:
        getConverter in interface Serializer
        Returns:
        the Converter used by this serializer