Class AbstractXStreamSerializer

java.lang.Object
org.axonframework.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 Details

  • Method Details

    • 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(@Nonnull 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, @Nonnull 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(@Nonnull 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(@Nonnull 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 returns an UnknownSerializedType instance which proides access to the raw underlying data.

      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 "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:
      • 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