org.axonframework.serializer
Class GenericXStreamSerializer

java.lang.Object
  extended by org.axonframework.serializer.GenericXStreamSerializer

public class GenericXStreamSerializer
extends Object

Serializer that uses XStream to serialize and deserialize arbitrary objects.

When running on a Sun JVM, XStream does not pose any restrictions on classes to serialize. On other JVM's, however, you need to either implement Serializable, or provide a default constructor (accessible under the JVM's security policy). That means that for portability, you should do either of these two.

Since:
0.6
Author:
Allard Buijze
See Also:
XStream

Constructor Summary
GenericXStreamSerializer()
          Initialize a generic serializer using the UTF-8 character set.
GenericXStreamSerializer(Charset charset)
          Initialize the serializer using the given charset.
GenericXStreamSerializer(Charset charset, com.thoughtworks.xstream.XStream xStream)
          Initialize the serializer using the given charset and xStream instance.
 
Method Summary
 void addAlias(String name, Class type)
          Adds an alias to use instead of the fully qualified class name.
 void addFieldAlias(String alias, Class definedIn, String fieldName)
          Adds an alias to use for a given field in the given class.
 void addPackageAlias(String alias, String pkgName)
          Add an alias for a package.
 Object deserialize(com.thoughtworks.xstream.io.HierarchicalStreamReader reader)
          Deserialize an object using the given dom4j Document.
 Object deserialize(InputStream inputStream)
          Deserialize an object using the bytes in the given inputStream.
 com.thoughtworks.xstream.XStream getXStream()
          Returns a reference to the underlying XStream instance, that does the actual serialization.
 void serialize(Object object, OutputStream outputStream)
          Serialize the given object and write the bytes to the given outputStream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GenericXStreamSerializer

public GenericXStreamSerializer()
Initialize a generic serializer using the UTF-8 character set. A default XStream instance (with XppDriver) is used to perform the serialization.


GenericXStreamSerializer

public GenericXStreamSerializer(Charset charset)
Initialize the serializer using the given charset. A default XStream instance (with XppDriver) is used to perform the serialization.

Parameters:
charset - The character set to use

GenericXStreamSerializer

public GenericXStreamSerializer(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.

Parameters:
charset - The character set to use
xStream - The XStream instance to use
Method Detail

serialize

public void serialize(Object object,
                      OutputStream outputStream)
Serialize the given object and write the bytes to the given outputStream. Bytes are written using the character set provided during initialization of the serializer.

Parameters:
object - The object to serialize.
outputStream - The stream to write bytes to

deserialize

public Object deserialize(InputStream inputStream)
Deserialize an object using the bytes in the given inputStream. The deserialization process may read more bytes from the input stream than might be absolutely necessary.

Parameters:
inputStream - The input stream providing the bytes of the serialized object
Returns:
the deserialized object

deserialize

public Object deserialize(com.thoughtworks.xstream.io.HierarchicalStreamReader reader)
Deserialize an object using the given dom4j Document. The document needs to describe the XML as it can be parsed by XStream.

Parameters:
reader - The hierarchical stream reader providing the data for the object to deserialize
Returns:
the deserialized 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 subpackages 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


Copyright © 2011. All Rights Reserved.