public class

XStream

extends Object
java.lang.Object
   ↳ com.thoughtworks.xstream.XStream

Class Overview

Simple facade to XStream library, a Java-XML serialization tool.


Example
 XStream xstream = new XStream();
 String xml = xstream.toXML(myObject); // serialize to XML
 Object myObject2 = xstream.fromXML(xml); // deserialize from XML
 

Aliasing classes

To create shorter XML, you can specify aliases for classes using the alias() method. For example, you can shorten all occurrences of element <com.blah.MyThing> to <my-thing> by registering an alias for the class.


 xstream.alias("my-thing", MyThing.class);
 

Converters

XStream contains a map of Converter instances, each of which acts as a strategy for converting a particular type of class to XML and back again. Out of the box, XStream contains converters for most basic types (String, Date, int, boolean, etc) and collections (Map, List, Set, Properties, etc). For other objects reflection is used to serialize each field recursively.

Extra converters can be registered using the registerConverter() method. Some non-standard converters are supplied in the com.thoughtworks.xstream.converters.extended package and you can create your own by implementing the Converter interface.


Example
 xstream.registerConverter(new SqlTimestampConverter());
 xstream.registerConverter(new DynamicProxyConverter());
 

The converters can be registered with an explicit priority. By default they are registered with XStream.PRIORITY_NORMAL. Converters of same priority will be used in the reverse sequence they have been registered. The default converter, i.e. the converter which will be used if no other registered converter is suitable, can be registered with priority XStream.PRIORITY_VERY_LOW. XStream uses by default the ReflectionConverter as the fallback converter.


Example
 xstream.registerConverter(new CustomDefaultConverter(), XStream.PRIORITY_VERY_LOW);
 

Object graphs

XStream has support for object graphs; a deserialized object graph will keep references intact, including circular references.

XStream can signify references in XML using either relative/absolute XPath or IDs. The mode can be changed using setMode():

xstream.setMode(XStream.XPATH_RELATIVE_REFERENCES); (Default) Uses XPath relative references to signify duplicate references. This produces XML with the least clutter.
xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES); Uses XPath absolute references to signify duplicate references. This produces XML with the least clutter.
xstream.setMode(XStream.ID_REFERENCES); Uses ID references to signify duplicate references. In some scenarios, such as when using hand-written XML, this is easier to work with.
xstream.setMode(XStream.NO_REFERENCES); This disables object graph support and treats the object structure like a tree. Duplicate references are treated as two separate objects and circular references cause an exception. This is slightly faster and uses less memory than the other two modes.

Thread safety

The XStream instance is thread-safe. That is, once the XStream instance has been created and configured, it may be shared across multiple threads allowing objects to be serialized/deserialized concurrently. Note, that this only applies if annotations are not auto-detected on -the-fly.

Implicit collections

To avoid the need for special tags for collections, you can define implicit collections using one of the addImplicitCollection methods.

Summary

Nested Classes
class XStream.InitializationException This class is deprecated. since 1.3, use InitializationException instead  
Constants
int ID_REFERENCES
int NO_REFERENCES
int PRIORITY_LOW
int PRIORITY_NORMAL
int PRIORITY_VERY_HIGH
int PRIORITY_VERY_LOW
int XPATH_ABSOLUTE_REFERENCES
int XPATH_REFERENCES This constant is deprecated. since 1.2, use XPATH_RELATIVE_REFERENCES or XPATH_ABSOLUTE_REFERENCES instead.
int XPATH_RELATIVE_REFERENCES
Public Constructors
XStream()
Constructs a default XStream.
XStream(ReflectionProvider reflectionProvider)
Constructs an XStream with a special ReflectionProvider.
XStream(HierarchicalStreamDriver hierarchicalStreamDriver)
Constructs an XStream with a special HierarchicalStreamDriver.
XStream(ReflectionProvider reflectionProvider, HierarchicalStreamDriver hierarchicalStreamDriver)
Constructs an XStream with a special HierarchicalStreamDriver and ReflectionProvider.
XStream(ReflectionProvider reflectionProvider, ClassMapper classMapper, HierarchicalStreamDriver driver)
This constructor is deprecated. As of 1.2, use XStream(ReflectionProvider, Mapper, HierarchicalStreamDriver)
XStream(ReflectionProvider reflectionProvider, ClassMapper classMapper, HierarchicalStreamDriver driver, String classAttributeIdentifier)
This constructor is deprecated. As of 1.2, use XStream(ReflectionProvider, Mapper, HierarchicalStreamDriver) and register classAttributeIdentifier as alias
XStream(ReflectionProvider reflectionProvider, Mapper mapper, HierarchicalStreamDriver driver)
This constructor is deprecated. since 1.3, use #XStream(ReflectionProvider, HierarchicalStreamDriver, Mapper, ClassLoader) instead
XStream(ReflectionProvider reflectionProvider, HierarchicalStreamDriver driver, ClassLoader classLoader)
Constructs an XStream with a special HierarchicalStreamDriver and ReflectionProvider and additionally with a prepared ClassLoader to use.
XStream(ReflectionProvider reflectionProvider, HierarchicalStreamDriver driver, ClassLoader classLoader, Mapper mapper)
Constructs an XStream with a special HierarchicalStreamDriver and ReflectionProvider and additionally with a prepared Mapper and the ClassLoader in use.
XStream(ReflectionProvider reflectionProvider, HierarchicalStreamDriver driver, ClassLoader classLoader, Mapper mapper, ConverterLookup converterLookup, ConverterRegistry converterRegistry)
Constructs an XStream with a special HierarchicalStreamDriver, ReflectionProvider, a prepared Mapper and the ClassLoader in use and an own ConverterRegistry.
Public Methods
void addDefaultImplementation(Class defaultImplementation, Class ofType)
Associate a default implementation of a class with an object.
void addImmutableType(Class type)
Add immutable types.
void addImplicitCollection(Class ownerType, String fieldName, String itemFieldName, Class itemType)
Adds implicit collection which is used for all items of the given element name defined by itemFieldName.
void addImplicitCollection(Class ownerType, String fieldName)
Adds a default implicit collection which is used for any unmapped XML tag.
void addImplicitCollection(Class ownerType, String fieldName, Class itemType)
Adds implicit collection which is used for all items of the given itemType.
void alias(String name, Class type, Class defaultImplementation)
Alias a Class to a shorter name to be used in XML elements.
void alias(String name, Class type)
Alias a Class to a shorter name to be used in XML elements.
void aliasAttribute(String alias, String attributeName)
Create an alias for an attribute
void aliasAttribute(Class definedIn, String attributeName, String alias)
Create an alias for an attribute.
void aliasField(String alias, Class definedIn, String fieldName)
Create an alias for a field name.
void aliasPackage(String name, String pkgName)
Alias a package to a shorter name to be used in XML elements.
void aliasSystemAttribute(String alias, String systemAttributeName)
Create an alias for a system attribute.
void aliasType(String name, Class type)
Alias a type to a shorter name to be used in XML elements.
void autodetectAnnotations(boolean mode)
Set the auto-detection mode of the AnnotationMapper.
ObjectInputStream createObjectInputStream(Reader xmlReader)
Creates an ObjectInputStream that deserializes a stream of objects from a reader using XStream.
ObjectInputStream createObjectInputStream(HierarchicalStreamReader reader)
Creates an ObjectInputStream that deserializes a stream of objects from a reader using XStream.
ObjectInputStream createObjectInputStream(InputStream in)
Creates an ObjectInputStream that deserializes a stream of objects from an InputStream using XStream.
ObjectOutputStream createObjectOutputStream(Writer writer, String rootNodeName)
Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream.
ObjectOutputStream createObjectOutputStream(HierarchicalStreamWriter writer)
Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream.
ObjectOutputStream createObjectOutputStream(Writer writer)
Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream.
ObjectOutputStream createObjectOutputStream(HierarchicalStreamWriter writer, String rootNodeName)
Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream.
ObjectOutputStream createObjectOutputStream(OutputStream out, String rootNodeName)
Creates an ObjectOutputStream that serializes a stream of objects to the OutputStream using XStream.
ObjectOutputStream createObjectOutputStream(OutputStream out)
Creates an ObjectOutputStream that serializes a stream of objects to the OutputStream using XStream.
Object fromXML(InputStream xml, Object root)
Deserialize an object from an XML InputStream, populating the fields of the given root object instead of instantiating a new one.
Object fromXML(Reader xml)
Deserialize an object from an XML Reader.
Object fromXML(String xml)
Deserialize an object from an XML String.
Object fromXML(Reader xml, Object root)
Deserialize an object from an XML Reader, populating the fields of the given root object instead of instantiating a new one.
Object fromXML(String xml, Object root)
Deserialize an object from an XML String, populating the fields of the given root object instead of instantiating a new one.
Object fromXML(InputStream input)
Deserialize an object from an XML InputStream.
ClassLoader getClassLoader()
Retrieve the ClassLoader XStream uses to load classes.
ClassMapper getClassMapper()
This method is deprecated. As of 1.2, use getMapper()
ConverterLookup getConverterLookup()
Mapper getMapper()
Retrieve the Mapper.
ReflectionProvider getReflectionProvider()
Retrieve the ReflectionProvider in use.
void marshal(Object obj, HierarchicalStreamWriter writer, DataHolder dataHolder)
Serialize and object to a hierarchical data structure (such as XML).
void marshal(Object obj, HierarchicalStreamWriter writer)
Serialize and object to a hierarchical data structure (such as XML).
DataHolder newDataHolder()
Create a DataHolder that can be used to pass data to the converters.
void omitField(Class definedIn, String fieldName)
Prevents a field from being serialized.
void processAnnotations(Class type)
Process the annotations of the given type and configure the XStream.
void processAnnotations(Class[] types)
Process the annotations of the given types and configure the XStream.
void registerConverter(Converter converter)
void registerConverter(SingleValueConverter converter)
void registerConverter(Converter converter, int priority)
void registerConverter(SingleValueConverter converter, int priority)
void registerLocalConverter(Class definedIn, String fieldName, SingleValueConverter converter)
Register a local SingleValueConverter for a field.
void registerLocalConverter(Class definedIn, String fieldName, Converter converter)
Register a local Converter for a field.
void setClassLoader(ClassLoader classLoader)
Change the ClassLoader XStream uses to load classes.
void setMarshallingStrategy(MarshallingStrategy marshallingStrategy)
void setMode(int mode)
Change mode for dealing with duplicate references.
void toXML(Object obj, Writer out)
Serialize an object to the given Writer as pretty-printed XML.
void toXML(Object obj, OutputStream out)
Serialize an object to the given OutputStream as pretty-printed XML.
String toXML(Object obj)
Serialize an object to a pretty-printed XML String.
Object unmarshal(HierarchicalStreamReader reader)
Deserialize an object from a hierarchical data structure (such as XML).
Object unmarshal(HierarchicalStreamReader reader, Object root)
Deserialize an object from a hierarchical data structure (such as XML), populating the fields of the given root object instead of instantiating a new one.
Object unmarshal(HierarchicalStreamReader reader, Object root, DataHolder dataHolder)
Deserialize an object from a hierarchical data structure (such as XML).
void useAttributeFor(String fieldName, Class type)
Use an attribute for a field or a specific type.
void useAttributeFor(Class type)
Use an attribute for an arbitrary type.
void useAttributeFor(Class definedIn, String fieldName)
Use an attribute for a field declared in a specific type.
Protected Methods
void setupAliases()
void setupConverters()
void setupDefaultImplementations()
void setupImmutableTypes()
boolean useXStream11XmlFriendlyMapper()
MapperWrapper wrapMapper(MapperWrapper next)
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int ID_REFERENCES

Constant Value: 1002 (0x000003ea)

public static final int NO_REFERENCES

Constant Value: 1001 (0x000003e9)

public static final int PRIORITY_LOW

Constant Value: -10 (0xfffffff6)

public static final int PRIORITY_NORMAL

Constant Value: 0 (0x00000000)

public static final int PRIORITY_VERY_HIGH

Constant Value: 10000 (0x00002710)

public static final int PRIORITY_VERY_LOW

Constant Value: -20 (0xffffffec)

public static final int XPATH_ABSOLUTE_REFERENCES

Constant Value: 1004 (0x000003ec)

public static final int XPATH_REFERENCES

This constant is deprecated.
since 1.2, use XPATH_RELATIVE_REFERENCES or XPATH_ABSOLUTE_REFERENCES instead.

Constant Value: 1003 (0x000003eb)

public static final int XPATH_RELATIVE_REFERENCES

Constant Value: 1003 (0x000003eb)

Public Constructors

public XStream ()

Constructs a default XStream. The instance will use the XppDriver as default and tries to determine the best match for the ReflectionProvider on its own.

Throws
XStream.InitializationException in case of an initialization problem

public XStream (ReflectionProvider reflectionProvider)

Constructs an XStream with a special ReflectionProvider. The instance will use the XppDriver as default.

Throws
XStream.InitializationException in case of an initialization problem

public XStream (HierarchicalStreamDriver hierarchicalStreamDriver)

Constructs an XStream with a special HierarchicalStreamDriver. The instance will tries to determine the best match for the ReflectionProvider on its own.

Throws
XStream.InitializationException in case of an initialization problem

public XStream (ReflectionProvider reflectionProvider, HierarchicalStreamDriver hierarchicalStreamDriver)

Constructs an XStream with a special HierarchicalStreamDriver and ReflectionProvider.

Throws
XStream.InitializationException in case of an initialization problem

public XStream (ReflectionProvider reflectionProvider, ClassMapper classMapper, HierarchicalStreamDriver driver)

This constructor is deprecated.
As of 1.2, use XStream(ReflectionProvider, Mapper, HierarchicalStreamDriver)

public XStream (ReflectionProvider reflectionProvider, ClassMapper classMapper, HierarchicalStreamDriver driver, String classAttributeIdentifier)

This constructor is deprecated.
As of 1.2, use XStream(ReflectionProvider, Mapper, HierarchicalStreamDriver) and register classAttributeIdentifier as alias

public XStream (ReflectionProvider reflectionProvider, Mapper mapper, HierarchicalStreamDriver driver)

This constructor is deprecated.
since 1.3, use #XStream(ReflectionProvider, HierarchicalStreamDriver, Mapper, ClassLoader) instead

Constructs an XStream with a special HierarchicalStreamDriver and ReflectionProvider and additionally with a prepared Mapper.

Throws
XStream.InitializationException in case of an initialization problem

public XStream (ReflectionProvider reflectionProvider, HierarchicalStreamDriver driver, ClassLoader classLoader)

Constructs an XStream with a special HierarchicalStreamDriver and ReflectionProvider and additionally with a prepared ClassLoader to use.

Throws
XStream.InitializationException in case of an initialization problem

public XStream (ReflectionProvider reflectionProvider, HierarchicalStreamDriver driver, ClassLoader classLoader, Mapper mapper)

Constructs an XStream with a special HierarchicalStreamDriver and ReflectionProvider and additionally with a prepared Mapper and the ClassLoader in use.

Note, if the class loader should be changed later again, you should provide a ClassLoaderReference as ClassLoader that is also use in the Mapper chain.

Throws
XStream.InitializationException in case of an initialization problem

public XStream (ReflectionProvider reflectionProvider, HierarchicalStreamDriver driver, ClassLoader classLoader, Mapper mapper, ConverterLookup converterLookup, ConverterRegistry converterRegistry)

Constructs an XStream with a special HierarchicalStreamDriver, ReflectionProvider, a prepared Mapper and the ClassLoader in use and an own ConverterRegistry.

Note, if the class loader should be changed later again, you should provide a ClassLoaderReference as ClassLoader that is also use in the Mapper chain.

Throws
XStream.InitializationException in case of an initialization problem

Public Methods

public void addDefaultImplementation (Class defaultImplementation, Class ofType)

Associate a default implementation of a class with an object. Whenever XStream encounters an instance of this type, it will use the default implementation instead. For example, java.util.ArrayList is the default implementation of java.util.List.

public void addImmutableType (Class type)

Add immutable types. The value of the instances of these types will always be written into the stream even if they appear multiple times.

public void addImplicitCollection (Class ownerType, String fieldName, String itemFieldName, Class itemType)

Adds implicit collection which is used for all items of the given element name defined by itemFieldName.

Parameters
ownerType class owning the implicit collection
fieldName name of the field in the ownerType. This field must be a concrete collection type or matching the default implementation type of the collection type.
itemFieldName element name of the implicit collection
itemType item type to be aliases be the itemFieldName

public void addImplicitCollection (Class ownerType, String fieldName)

Adds a default implicit collection which is used for any unmapped XML tag.

Parameters
ownerType class owning the implicit collection
fieldName name of the field in the ownerType. This field must be a concrete collection type or matching the default implementation type of the collection type.

public void addImplicitCollection (Class ownerType, String fieldName, Class itemType)

Adds implicit collection which is used for all items of the given itemType.

Parameters
ownerType class owning the implicit collection
fieldName name of the field in the ownerType. This field must be a concrete collection type or matching the default implementation type of the collection type.
itemType type of the items to be part of this collection.

public void alias (String name, Class type, Class defaultImplementation)

Alias a Class to a shorter name to be used in XML elements.

Parameters
name Short name
type Type to be aliased
defaultImplementation Default implementation of type to use if no other specified.

public void alias (String name, Class type)

Alias a Class to a shorter name to be used in XML elements.

Parameters
name Short name
type Type to be aliased

public void aliasAttribute (String alias, String attributeName)

Create an alias for an attribute

Parameters
alias the alias itself
attributeName the name of the attribute

public void aliasAttribute (Class definedIn, String attributeName, String alias)

Create an alias for an attribute.

Parameters
definedIn the type where the attribute is defined
attributeName the name of the attribute
alias the alias itself

public void aliasField (String alias, Class definedIn, String fieldName)

Create an alias for a field name.

Parameters
alias the alias itself
definedIn the type that declares the field
fieldName the name of the field

public void aliasPackage (String name, String pkgName)

Alias a package to a shorter name to be used in XML elements.

Parameters
name Short name
pkgName package to be aliased

public void aliasSystemAttribute (String alias, String systemAttributeName)

Create an alias for a system attribute. XStream will not write a system attribute if its alias is set to null. However, this is not reversible, i.e. deserialization of the result is likely to fail afterwards and will not produce an object equal to the originally written one.

Parameters
alias the alias itself (may be null)
systemAttributeName the name of the system attribute

public void aliasType (String name, Class type)

Alias a type to a shorter name to be used in XML elements. Any class that is assignable to this type will be aliased to the same name.

Parameters
name Short name
type Type to be aliased

public void autodetectAnnotations (boolean mode)

Set the auto-detection mode of the AnnotationMapper. Note that auto-detection implies that the XStream is configured while it is processing the XML steams. This is a potential concurrency problem. Also is it technically not possible to detect all class aliases at deserialization. You have been warned!

Parameters
mode true if annotations are auto-detected

public ObjectInputStream createObjectInputStream (Reader xmlReader)

public ObjectInputStream createObjectInputStream (HierarchicalStreamReader reader)

Creates an ObjectInputStream that deserializes a stream of objects from a reader using XStream.

Example

 ObjectInputStream in = xstream.createObjectOutputStream(aReader);
 int a = out.readInt();
 Object b = out.readObject();
 Object c = out.readObject();
 

Throws
IOException

public ObjectInputStream createObjectInputStream (InputStream in)

Creates an ObjectInputStream that deserializes a stream of objects from an InputStream using XStream.

Throws
IOException

public ObjectOutputStream createObjectOutputStream (Writer writer, String rootNodeName)

public ObjectOutputStream createObjectOutputStream (HierarchicalStreamWriter writer)

Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream.

To change the name of the root element (from <object-stream>), use createObjectOutputStream(java.io.Writer, String).

Throws
IOException

public ObjectOutputStream createObjectOutputStream (Writer writer)

Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream.

To change the name of the root element (from <object-stream>), use createObjectOutputStream(java.io.Writer, String).

Throws
IOException

public ObjectOutputStream createObjectOutputStream (HierarchicalStreamWriter writer, String rootNodeName)

Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream.

Because an ObjectOutputStream can contain multiple items and XML only allows a single root node, the stream must be written inside an enclosing node.

It is necessary to call ObjectOutputStream.close() when done, otherwise the stream will be incomplete.

Example

  ObjectOutputStream out = xstream.createObjectOutputStream(aWriter, "things");
   out.writeInt(123);
   out.writeObject("Hello");
   out.writeObject(someObject)
   out.close();
 

Parameters
writer The writer to serialize the objects to.
rootNodeName The name of the root node enclosing the stream of objects.
Throws
IOException

public ObjectOutputStream createObjectOutputStream (OutputStream out, String rootNodeName)

Creates an ObjectOutputStream that serializes a stream of objects to the OutputStream using XStream.

Throws
IOException

public ObjectOutputStream createObjectOutputStream (OutputStream out)

Creates an ObjectOutputStream that serializes a stream of objects to the OutputStream using XStream.

To change the name of the root element (from <object-stream>), use createObjectOutputStream(java.io.Writer, String).

Throws
IOException

public Object fromXML (InputStream xml, Object root)

Deserialize an object from an XML InputStream, populating the fields of the given root object instead of instantiating a new one. Note, that this is a special use case! With the ReflectionConverter XStream will write directly into the raw memory area of the existing object. Use with care!

Throws
XStreamException if the object cannot be deserialized

public Object fromXML (Reader xml)

Deserialize an object from an XML Reader.

Throws
XStreamException if the object cannot be deserialized

public Object fromXML (String xml)

Deserialize an object from an XML String.

Throws
XStreamException if the object cannot be deserialized

public Object fromXML (Reader xml, Object root)

Deserialize an object from an XML Reader, populating the fields of the given root object instead of instantiating a new one. Note, that this is a special use case! With the ReflectionConverter XStream will write directly into the raw memory area of the existing object. Use with care!

Throws
XStreamException if the object cannot be deserialized

public Object fromXML (String xml, Object root)

Deserialize an object from an XML String, populating the fields of the given root object instead of instantiating a new one. Note, that this is a special use case! With the ReflectionConverter XStream will write directly into the raw memory area of the existing object. Use with care!

Throws
XStreamException if the object cannot be deserialized

public Object fromXML (InputStream input)

Deserialize an object from an XML InputStream.

Throws
XStreamException if the object cannot be deserialized

public ClassLoader getClassLoader ()

Retrieve the ClassLoader XStream uses to load classes.

public ClassMapper getClassMapper ()

This method is deprecated.
As of 1.2, use getMapper()

Throws
ClassCastException if mapper is not really a deprecated ClassMapper instance

public ConverterLookup getConverterLookup ()

public Mapper getMapper ()

Retrieve the Mapper. This is by default a chain of MapperWrappers.

Returns
  • the mapper

public ReflectionProvider getReflectionProvider ()

Retrieve the ReflectionProvider in use.

Returns
  • the mapper

public void marshal (Object obj, HierarchicalStreamWriter writer, DataHolder dataHolder)

Serialize and object to a hierarchical data structure (such as XML).

Parameters
dataHolder Extra data you can use to pass to your converters. Use this as you want. If not present, XStream shall create one lazily as needed.
Throws
XStreamException if the object cannot be serialized

public void marshal (Object obj, HierarchicalStreamWriter writer)

Serialize and object to a hierarchical data structure (such as XML).

Throws
XStreamException if the object cannot be serialized

public DataHolder newDataHolder ()

Create a DataHolder that can be used to pass data to the converters. The DataHolder is provided with a call to marshal(Object, HierarchicalStreamWriter, DataHolder) or unmarshal(HierarchicalStreamReader, Object, DataHolder).

Returns

public void omitField (Class definedIn, String fieldName)

Prevents a field from being serialized. To omit a field you must always provide the declaring type and not necessarily the type that is converted.

public void processAnnotations (Class type)

Process the annotations of the given type and configure the XStream. A call of this method will automatically turn the auto-detection mode for annotations off.

Parameters
type the type with XStream annotations

public void processAnnotations (Class[] types)

Process the annotations of the given types and configure the XStream.

Parameters
types the types with XStream annotations

public void registerConverter (Converter converter)

public void registerConverter (SingleValueConverter converter)

public void registerConverter (Converter converter, int priority)

public void registerConverter (SingleValueConverter converter, int priority)

public void registerLocalConverter (Class definedIn, String fieldName, SingleValueConverter converter)

Register a local SingleValueConverter for a field.

Parameters
definedIn the class type the field is defined in
fieldName the field name
converter the converter to use

public void registerLocalConverter (Class definedIn, String fieldName, Converter converter)

Register a local Converter for a field.

Parameters
definedIn the class type the field is defined in
fieldName the field name
converter the converter to use

public void setClassLoader (ClassLoader classLoader)

Change the ClassLoader XStream uses to load classes. Creating an XStream instance it will register for all kind of classes and types of the current JDK, but not for any 3rd party type. To ensure that all other types are loaded with your classloader, you should call this method as early as possible - or consider to provide the classloader directly in the constructor.

public void setMarshallingStrategy (MarshallingStrategy marshallingStrategy)

public void setMode (int mode)

Change mode for dealing with duplicate references. Valid values are XPATH_ABSOLUTE_REFERENCES, XPATH_RELATIVE_REFERENCES, XStream.ID_REFERENCES and XStream.NO_REFERENCES.

Throws
IllegalArgumentException if the mode is not one of the declared types

public void toXML (Object obj, Writer out)

Serialize an object to the given Writer as pretty-printed XML. The Writer will be flushed afterwards and in case of an exception.

Throws
XStreamException if the object cannot be serialized

public void toXML (Object obj, OutputStream out)

Serialize an object to the given OutputStream as pretty-printed XML. The OutputStream will be flushed afterwards and in case of an exception.

Throws
XStreamException if the object cannot be serialized

public String toXML (Object obj)

Serialize an object to a pretty-printed XML String.

Throws
XStreamException if the object cannot be serialized

public Object unmarshal (HierarchicalStreamReader reader)

Deserialize an object from a hierarchical data structure (such as XML).

Throws
XStreamException if the object cannot be deserialized

public Object unmarshal (HierarchicalStreamReader reader, Object root)

Deserialize an object from a hierarchical data structure (such as XML), populating the fields of the given root object instead of instantiating a new one. Note, that this is a special use case! With the ReflectionConverter XStream will write directly into the raw memory area of the existing object. Use with care!

Throws
XStreamException if the object cannot be deserialized

public Object unmarshal (HierarchicalStreamReader reader, Object root, DataHolder dataHolder)

Deserialize an object from a hierarchical data structure (such as XML).

Parameters
root If present, the passed in object will have its fields populated, as opposed to XStream creating a new instance. Note, that this is a special use case! With the ReflectionConverter XStream will write directly into the raw memory area of the existing object. Use with care!
dataHolder Extra data you can use to pass to your converters. Use this as you want. If not present, XStream shall create one lazily as needed.
Throws
XStreamException if the object cannot be deserialized

public void useAttributeFor (String fieldName, Class type)

Use an attribute for a field or a specific type.

Parameters
fieldName the name of the field
type the Class of the type to be rendered as XML attribute

public void useAttributeFor (Class type)

Use an attribute for an arbitrary type.

Parameters
type the Class of the type to be rendered as XML attribute

public void useAttributeFor (Class definedIn, String fieldName)

Use an attribute for a field declared in a specific type.

Parameters
definedIn the Class containing such field
fieldName the name of the field

Protected Methods

protected void setupAliases ()

protected void setupConverters ()

protected void setupDefaultImplementations ()

protected void setupImmutableTypes ()

protected boolean useXStream11XmlFriendlyMapper ()

protected MapperWrapper wrapMapper (MapperWrapper next)