java.lang.Object | |
↳ | com.thoughtworks.xstream.XStream |
Simple facade to XStream library, a Java-XML serialization tool.
XStream xstream = new XStream(); String xml = xstream.toXML(myObject); // serialize to XML Object myObject2 = xstream.fromXML(xml); // deserialize from XML
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);
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.
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.
xstream.registerConverter(new CustomDefaultConverter(), XStream.PRIORITY_VERY_LOW);
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. |
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.
To avoid the need for special tags for collections, you can define implicit collections using one
of the addImplicitCollection
methods.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
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 | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a default XStream.
| |||||||||||
Constructs an XStream with a special
ReflectionProvider . | |||||||||||
Constructs an XStream with a special
HierarchicalStreamDriver . | |||||||||||
Constructs an XStream with a special
HierarchicalStreamDriver and ReflectionProvider . | |||||||||||
This constructor is deprecated.
As of 1.2, use
XStream(ReflectionProvider, Mapper, HierarchicalStreamDriver)
| |||||||||||
This constructor is deprecated.
As of 1.2, use
XStream(ReflectionProvider, Mapper, HierarchicalStreamDriver) and
register classAttributeIdentifier as alias
| |||||||||||
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
ClassLoader to use. | |||||||||||
Constructs an XStream with a special
HierarchicalStreamDriver and ReflectionProvider and additionally with a prepared Mapper
and the ClassLoader in use. | |||||||||||
Constructs an XStream with a special
HierarchicalStreamDriver , ReflectionProvider , a prepared Mapper
and the ClassLoader in use and an own ConverterRegistry . |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Associate a default implementation of a class with an object.
| |||||||||||
Add immutable types.
| |||||||||||
Adds implicit collection which is used for all items of the given element name defined by
itemFieldName.
| |||||||||||
Adds a default implicit collection which is used for any unmapped XML tag.
| |||||||||||
Adds implicit collection which is used for all items of the given itemType.
| |||||||||||
Alias a Class to a shorter name to be used in XML elements.
| |||||||||||
Alias a Class to a shorter name to be used in XML elements.
| |||||||||||
Create an alias for an attribute
| |||||||||||
Create an alias for an attribute.
| |||||||||||
Create an alias for a field name.
| |||||||||||
Alias a package to a shorter name to be used in XML elements.
| |||||||||||
Create an alias for a system attribute.
| |||||||||||
Alias a type to a shorter name to be used in XML elements.
| |||||||||||
Set the auto-detection mode of the AnnotationMapper.
| |||||||||||
Creates an ObjectInputStream that deserializes a stream of objects from a reader using
XStream.
| |||||||||||
Creates an ObjectInputStream that deserializes a stream of objects from a reader using
XStream.
| |||||||||||
Creates an ObjectInputStream that deserializes a stream of objects from an InputStream using
XStream.
| |||||||||||
Creates an ObjectOutputStream that serializes a stream of objects to the writer using
XStream.
| |||||||||||
Creates an ObjectOutputStream that serializes a stream of objects to the writer using
XStream.
| |||||||||||
Creates an ObjectOutputStream that serializes a stream of objects to the writer using
XStream.
| |||||||||||
Creates an ObjectOutputStream that serializes a stream of objects to the writer using
XStream.
| |||||||||||
Creates an ObjectOutputStream that serializes a stream of objects to the OutputStream using
XStream.
| |||||||||||
Creates an ObjectOutputStream that serializes a stream of objects to the OutputStream using
XStream.
| |||||||||||
Deserialize an object from an XML InputStream, populating the fields of the given root object
instead of instantiating a new one.
| |||||||||||
Deserialize an object from an XML Reader.
| |||||||||||
Deserialize an object from an XML String.
| |||||||||||
Deserialize an object from an XML Reader, populating the fields of the given root object
instead of instantiating a new one.
| |||||||||||
Deserialize an object from an XML String, populating the fields of the given root object
instead of instantiating a new one.
| |||||||||||
Deserialize an object from an XML InputStream.
| |||||||||||
Retrieve the ClassLoader XStream uses to load classes.
| |||||||||||
This method is deprecated.
As of 1.2, use
getMapper()
| |||||||||||
Retrieve the
Mapper . | |||||||||||
Retrieve the
ReflectionProvider in use. | |||||||||||
Serialize and object to a hierarchical data structure (such as XML).
| |||||||||||
Serialize and object to a hierarchical data structure (such as XML).
| |||||||||||
Create a DataHolder that can be used to pass data to the converters.
| |||||||||||
Prevents a field from being serialized.
| |||||||||||
Process the annotations of the given type and configure the XStream.
| |||||||||||
Process the annotations of the given types and configure the XStream.
| |||||||||||
Register a local
SingleValueConverter for a field. | |||||||||||
Register a local
Converter for a field. | |||||||||||
Change the ClassLoader XStream uses to load classes.
| |||||||||||
Change mode for dealing with duplicate references.
| |||||||||||
Serialize an object to the given Writer as pretty-printed XML.
| |||||||||||
Serialize an object to the given OutputStream as pretty-printed XML.
| |||||||||||
Serialize an object to a pretty-printed XML String.
| |||||||||||
Deserialize an object from a hierarchical data structure (such as XML).
| |||||||||||
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.
| |||||||||||
Deserialize an object from a hierarchical data structure (such as XML).
| |||||||||||
Use an attribute for a field or a specific type.
| |||||||||||
Use an attribute for an arbitrary type.
| |||||||||||
Use an attribute for a field declared in a specific type.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
This constant is deprecated.
since 1.2, use XPATH_RELATIVE_REFERENCES
or
XPATH_ABSOLUTE_REFERENCES
instead.
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.
XStream.InitializationException | in case of an initialization problem |
---|
Constructs an XStream with a special ReflectionProvider
. The instance will use the XppDriver
as default.
XStream.InitializationException | in case of an initialization problem |
---|
Constructs an XStream with a special HierarchicalStreamDriver
. The instance will tries to determine the best
match for the ReflectionProvider
on its own.
XStream.InitializationException | in case of an initialization problem |
---|
Constructs an XStream with a special HierarchicalStreamDriver
and ReflectionProvider
.
XStream.InitializationException | in case of an initialization problem |
---|
This constructor is deprecated.
As of 1.2, use
XStream(ReflectionProvider, Mapper, HierarchicalStreamDriver)
This constructor is deprecated.
As of 1.2, use
XStream(ReflectionProvider, Mapper, HierarchicalStreamDriver)
and
register classAttributeIdentifier as alias
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
.
XStream.InitializationException | in case of an initialization problem |
---|
Constructs an XStream with a special HierarchicalStreamDriver
and ReflectionProvider
and additionally with a prepared
ClassLoader
to use.
XStream.InitializationException | in case of an initialization problem |
---|
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.
XStream.InitializationException | in case of an initialization problem |
---|
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.
XStream.InitializationException | in case of an initialization problem |
---|
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.
XStream.InitializationException | if no DefaultImplementationsMapper is available
|
---|
Add immutable types. The value of the instances of these types will always be written into the stream even if they appear multiple times.
XStream.InitializationException | if no ImmutableTypesMapper is available
|
---|
Adds implicit collection which is used for all items of the given element name defined by itemFieldName.
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 |
XStream.InitializationException | if no ImplicitCollectionMapper is available
|
---|
Adds a default implicit collection which is used for any unmapped XML tag.
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. |
Adds implicit collection which is used for all items of the given itemType.
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. |
XStream.InitializationException | if no ImplicitCollectionMapper is available
|
---|
Alias a Class to a shorter name to be used in XML elements.
name | Short name |
---|---|
type | Type to be aliased |
defaultImplementation | Default implementation of type to use if no other specified. |
XStream.InitializationException | if no DefaultImplementationsMapper or no ClassAliasingMapper is available
|
---|
Alias a Class to a shorter name to be used in XML elements.
name | Short name |
---|---|
type | Type to be aliased |
XStream.InitializationException | if no ClassAliasingMapper is available
|
---|
Create an alias for an attribute
alias | the alias itself |
---|---|
attributeName | the name of the attribute |
XStream.InitializationException | if no AttributeAliasingMapper is available
|
---|
Create an alias for an attribute.
definedIn | the type where the attribute is defined |
---|---|
attributeName | the name of the attribute |
alias | the alias itself |
XStream.InitializationException | if no AttributeAliasingMapper is available |
---|
Create an alias for a field name.
alias | the alias itself |
---|---|
definedIn | the type that declares the field |
fieldName | the name of the field |
XStream.InitializationException | if no FieldAliasingMapper is available
|
---|
Alias a package to a shorter name to be used in XML elements.
name | Short name |
---|---|
pkgName | package to be aliased |
XStream.InitializationException | if no DefaultImplementationsMapper or no PackageAliasingMapper is available |
---|
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.
alias | the alias itself (may be null ) |
---|---|
systemAttributeName | the name of the system attribute |
XStream.InitializationException | if no SystemAttributeAliasingMapper is available |
---|
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.
name | Short name |
---|---|
type | Type to be aliased |
XStream.InitializationException | if no ClassAliasingMapper is available
|
---|
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!
mode | true if annotations are auto-detected |
---|
Creates an ObjectInputStream that deserializes a stream of objects from a reader using XStream.
IOException |
---|
Creates an ObjectInputStream that deserializes a stream of objects from a reader using XStream.
ObjectInputStream in = xstream.createObjectOutputStream(aReader); int a = out.readInt(); Object b = out.readObject(); Object c = out.readObject();
IOException |
---|
Creates an ObjectInputStream that deserializes a stream of objects from an InputStream using XStream.
IOException |
---|
Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream.
IOException |
---|
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)
.
IOException |
---|
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)
.
IOException |
---|
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.
ObjectOutputStream out = xstream.createObjectOutputStream(aWriter, "things"); out.writeInt(123); out.writeObject("Hello"); out.writeObject(someObject) out.close();
writer | The writer to serialize the objects to. |
---|---|
rootNodeName | The name of the root node enclosing the stream of objects. |
IOException |
---|
Creates an ObjectOutputStream that serializes a stream of objects to the OutputStream using XStream.
IOException |
---|
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)
.
IOException |
---|
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!
XStreamException | if the object cannot be deserialized |
---|
Deserialize an object from an XML Reader.
XStreamException | if the object cannot be deserialized |
---|
Deserialize an object from an XML String.
XStreamException | if the object cannot be deserialized |
---|
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!
XStreamException | if the object cannot be deserialized |
---|
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!
XStreamException | if the object cannot be deserialized |
---|
Deserialize an object from an XML InputStream.
XStreamException | if the object cannot be deserialized |
---|
This method is deprecated.
As of 1.2, use getMapper()
ClassCastException | if mapper is not really a deprecated ClassMapper instance |
---|
Retrieve the Mapper
. This is by default a chain of MapperWrappers
.
Retrieve the ReflectionProvider
in use.
Serialize and object to a hierarchical data structure (such as XML).
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. |
---|
XStreamException | if the object cannot be serialized |
---|
Serialize and object to a hierarchical data structure (such as XML).
XStreamException | if the object cannot be serialized |
---|
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)
.
DataHolder
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.
XStream.InitializationException | if no FieldAliasingMapper is available
|
---|
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.
type | the type with XStream annotations |
---|
Process the annotations of the given types and configure the XStream.
types | the types with XStream annotations |
---|
Register a local SingleValueConverter
for a field.
definedIn | the class type the field is defined in |
---|---|
fieldName | the field name |
converter | the converter to use |
Register a local Converter
for a field.
definedIn | the class type the field is defined in |
---|---|
fieldName | the field name |
converter | the converter to use |
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.
Change mode for dealing with duplicate references. Valid values are
XPATH_ABSOLUTE_REFERENCES
, XPATH_RELATIVE_REFERENCES
,
XStream.ID_REFERENCES
and XStream.NO_REFERENCES
.
IllegalArgumentException | if the mode is not one of the declared types |
---|
Serialize an object to the given Writer as pretty-printed XML. The Writer will be flushed afterwards and in case of an exception.
XStreamException | if the object cannot be serialized |
---|
Serialize an object to the given OutputStream as pretty-printed XML. The OutputStream will be flushed afterwards and in case of an exception.
XStreamException | if the object cannot be serialized |
---|
Serialize an object to a pretty-printed XML String.
XStreamException | if the object cannot be serialized |
---|
Deserialize an object from a hierarchical data structure (such as XML).
XStreamException | if the object cannot be deserialized |
---|
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!
XStreamException | if the object cannot be deserialized |
---|
Deserialize an object from a hierarchical data structure (such as XML).
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. |
XStreamException | if the object cannot be deserialized |
---|
Use an attribute for a field or a specific type.
fieldName | the name of the field |
---|---|
type | the Class of the type to be rendered as XML attribute |
XStream.InitializationException | if no AttributeMapper is available |
---|
Use an attribute for an arbitrary type.
type | the Class of the type to be rendered as XML attribute |
---|
XStream.InitializationException | if no AttributeMapper is available |
---|
Use an attribute for a field declared in a specific type.
definedIn | the Class containing such field |
---|---|
fieldName | the name of the field |
XStream.InitializationException | if no AttributeMapper is available |
---|