| java.lang.Object com.thoughtworks.xstream.XStream
All known Subclasses: com.thoughtworks.xstream.InitializationException,
XStream | public class XStream (Code) | | 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
com.thoughtworks.xstream.converters.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
com.thoughtworks.xstream.converters.Converter interface.
Example
xstream.registerConverter(new SqlTimestampConverter());
xstream.registerConverter(new DynamicProxyConverter());
The default converter, ie the converter which will be used if no other registered converter is
suitable, can be configured by either one of the constructors or can be changed using the
changeDefaultConverter() method. If not set, XStream uses
com.thoughtworks.xstream.converters.reflection.ReflectionConverter as the initial default
converter.
Example
xstream.changeDefaultConverter(new ACustomDefaultConverter());
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.
Implicit collections
To avoid the need for special tags for collections, you can define implicit collections using one
of the addImplicitCollection methods.
author: Joe Walnes author: Jörg Schaible author: Mauro Talevi author: Guilherme Silveira |
Inner Class :public static class InitializationException extends XStreamException | |
Constructor Summary | |
public | XStream() Constructs a default XStream. | public | XStream(ReflectionProvider reflectionProvider) Constructs an XStream with a special
ReflectionProvider . | public | XStream(HierarchicalStreamDriver hierarchicalStreamDriver) Constructs an XStream with a special
HierarchicalStreamDriver . | public | XStream(ReflectionProvider reflectionProvider, HierarchicalStreamDriver hierarchicalStreamDriver) Constructs an XStream with a special
HierarchicalStreamDriver and
ReflectionProvider . | public | XStream(ReflectionProvider reflectionProvider, ClassMapper classMapper, HierarchicalStreamDriver driver) | public | XStream(ReflectionProvider reflectionProvider, ClassMapper classMapper, HierarchicalStreamDriver driver, String classAttributeIdentifier) | public | XStream(ReflectionProvider reflectionProvider, Mapper mapper, HierarchicalStreamDriver driver) Constructs an XStream with a special
HierarchicalStreamDriver and
ReflectionProvider and additionally with a prepared
Mapper . | 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. | 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. | 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 . |
Method Summary | |
public void | addDefaultImplementation(Class defaultImplementation, Class ofType) Associate a default implementation of a class with an object. | public void | addImmutableType(Class type) Add immutable types. | 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 Parameters: fieldName - name of the field in the ownerType. | 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 Parameters: fieldName - name of the field in the ownerType. | 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 Parameters: fieldName - name of the field in the ownerType. | public void | alias(String name, Class type) Alias a Class to a shorter name to be used in XML elements. | public void | alias(String name, Class type, Class defaultImplementation) Alias a Class to a shorter name to be used in XML elements. | public void | aliasAttribute(String alias, String attributeName) | public void | aliasAttribute(Class definedIn, String attributeName, String alias) Create an alias for an attribute. | public void | aliasField(String alias, Class definedIn, String fieldName) Create an alias for a field name. | public void | aliasType(String name, Class type) Alias a type to a shorter name to be used in XML elements. | public void | autodetectAnnotations(boolean mode) Set the auto-detection mode of the AnnotationMapper. | public ObjectInputStream | createObjectInputStream(Reader xmlReader) Creates an ObjectInputStream that deserializes a stream of objects from a reader using
XStream. | public ObjectInputStream | createObjectInputStream(InputStream in) Creates an ObjectInputStream that deserializes a stream of objects from an InputStream using
XStream. | public ObjectInputStream | createObjectInputStream(HierarchicalStreamReader reader) Creates an ObjectInputStream that deserializes a stream of objects from a reader using
XStream. | public ObjectOutputStream | createObjectOutputStream(Writer writer) Creates an ObjectOutputStream that serializes a stream of objects to the writer using
XStream. | public ObjectOutputStream | createObjectOutputStream(HierarchicalStreamWriter writer) Creates an ObjectOutputStream that serializes a stream of objects to the writer using
XStream. | public ObjectOutputStream | createObjectOutputStream(Writer writer, String rootNodeName) Creates an ObjectOutputStream that serializes a stream of objects to the writer using
XStream. | public ObjectOutputStream | createObjectOutputStream(OutputStream out) Creates an ObjectOutputStream that serializes a stream of objects to the OutputStream using
XStream. | public ObjectOutputStream | createObjectOutputStream(OutputStream out, String rootNodeName) Creates an ObjectOutputStream that serializes a stream of objects to the OutputStream using
XStream. | public ObjectOutputStream | createObjectOutputStream(HierarchicalStreamWriter writer, String rootNodeName) Creates an ObjectOutputStream that serializes a stream of objects to the writer using
XStream. | public Object | fromXML(String xml) Deserialize an object from an XML String. | public Object | fromXML(Reader xml) Deserialize an object from an XML Reader. | public Object | fromXML(InputStream input) Deserialize an object from an XML InputStream. | 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. | 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. | 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. | public ClassLoader | getClassLoader() Change the ClassLoader XStream uses to load classes. | public ClassMapper | getClassMapper() | public ConverterLookup | getConverterLookup() | public Mapper | getMapper() Retrieve the
Mapper . | public ReflectionProvider | getReflectionProvider() Retrieve the
ReflectionProvider in use. | public void | marshal(Object obj, HierarchicalStreamWriter writer) Serialize and object to a hierarchical data structure (such as XML). | 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. | public DataHolder | newDataHolder() Create a DataHolder that can be used to pass data to the converters. | public void | omitField(Class definedIn, String fieldName) Prevents a field from being serialized. | public void | processAnnotations(Class[] types) Process the annotations of the given types and configure the XStream. | public void | processAnnotations(Class type) Process the annotations of the given type and configure the XStream. | public void | registerConverter(Converter converter) | public void | registerConverter(Converter converter, int priority) | public void | registerConverter(SingleValueConverter converter) | public void | registerConverter(SingleValueConverter converter, int priority) | public void | registerLocalConverter(Class definedIn, String fieldName, Converter converter) Register a local
Converter for a field. | public void | registerLocalConverter(Class definedIn, String fieldName, SingleValueConverter converter) Register a local
SingleValueConverter for a field. | public void | setClassLoader(ClassLoader classLoader) Change the ClassLoader XStream uses to load classes. | public void | setMarshallingStrategy(MarshallingStrategy marshallingStrategy) | public void | setMode(int mode) Change mode for dealing with duplicate references. | protected void | setupAliases() | protected void | setupConverters() | protected void | setupDefaultImplementations() | protected void | setupImmutableTypes() | public String | toXML(Object obj) Serialize an object to a pretty-printed XML String. | public void | toXML(Object obj, Writer out) Serialize an object to the given Writer as pretty-printed XML. | public void | toXML(Object obj, OutputStream out) Serialize an object to the given OutputStream as pretty-printed XML. | public Object | unmarshal(HierarchicalStreamReader reader) Deserialize an object from a hierarchical data structure (such as XML). | 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. | 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 toXStream creating a new instance. | public void | useAttributeFor(String fieldName, Class type) Use an attribute for a field or a specific type. | public void | useAttributeFor(Class definedIn, String fieldName) Use an attribute for a field declared in a specific type. | public void | useAttributeFor(Class type) Use an attribute for an arbitrary type. | protected boolean | useXStream11XmlFriendlyMapper() | protected MapperWrapper | wrapMapper(MapperWrapper next) |
ID_REFERENCES | final public static int ID_REFERENCES(Code) | | |
NO_REFERENCES | final public static int NO_REFERENCES(Code) | | |
PRIORITY_LOW | final public static int PRIORITY_LOW(Code) | | |
PRIORITY_NORMAL | final public static int PRIORITY_NORMAL(Code) | | |
PRIORITY_VERY_HIGH | final public static int PRIORITY_VERY_HIGH(Code) | | |
PRIORITY_VERY_LOW | final public static int PRIORITY_VERY_LOW(Code) | | |
XPATH_ABSOLUTE_REFERENCES | final public static int XPATH_ABSOLUTE_REFERENCES(Code) | | |
XPATH_RELATIVE_REFERENCES | final public static int XPATH_RELATIVE_REFERENCES(Code) | | |
addDefaultImplementation | public void addDefaultImplementation(Class defaultImplementation, Class ofType)(Code) | | 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.
Parameters: defaultImplementation - Parameters: ofType - throws: InitializationException - if no DefaultImplementationsMapper is available |
addImmutableType | public void addImmutableType(Class type)(Code) | | Add immutable types. The value of the instances of these types will always be written into
the stream even if they appear multiple times.
throws: InitializationException - if no ImmutableTypesMapper is available |
addImplicitCollection | public void addImplicitCollection(Class ownerType, String fieldName)(Code) | | Adds a default implicit collection which is used for any unmapped xml tag.
Parameters: ownerType - class owning the implicit collection Parameters: fieldName - name of the field in the ownerType. This field must be anjava.util.ArrayList . |
addImplicitCollection | public void addImplicitCollection(Class ownerType, String fieldName, Class itemType)(Code) | | Adds implicit collection which is used for all items of the given itemType.
Parameters: ownerType - class owning the implicit collection Parameters: fieldName - name of the field in the ownerType. This field must be anjava.util.ArrayList . Parameters: itemType - type of the items to be part of this collection. throws: InitializationException - if no ImplicitCollectionMapper is available |
addImplicitCollection | public void addImplicitCollection(Class ownerType, String fieldName, String itemFieldName, Class itemType)(Code) | | Adds implicit collection which is used for all items of the given element name defined by
itemFieldName.
Parameters: ownerType - class owning the implicit collection Parameters: fieldName - name of the field in the ownerType. This field must be anjava.util.ArrayList . Parameters: itemFieldName - element name of the implicit collection Parameters: itemType - item type to be aliases be the itemFieldName throws: InitializationException - if no ImplicitCollectionMapper is available |
aliasAttribute | public void aliasAttribute(Class definedIn, String attributeName, String alias)(Code) | | Create an alias for an attribute.
Parameters: definedIn - the type where the attribute is defined Parameters: attributeName - the name of the attribute Parameters: alias - the alias itself throws: InitializationException - if no AttributeAliasingMapper is available since: 1.2.2 |
aliasField | public void aliasField(String alias, Class definedIn, String fieldName)(Code) | | Create an alias for a field name.
Parameters: alias - the alias itself Parameters: definedIn - the type that declares the field Parameters: fieldName - the name of the field throws: InitializationException - if no FieldAliasingMapper is available |
aliasType | public void aliasType(String name, Class type)(Code) | | 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 Parameters: type - Type to be aliased since: 1.2 throws: InitializationException - if no ClassAliasingMapper is available |
autodetectAnnotations | public void autodetectAnnotations(boolean mode)(Code) | | 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 since: 1.3 |
createObjectOutputStream | public ObjectOutputStream createObjectOutputStream(HierarchicalStreamWriter writer, String rootNodeName) throws IOException(Code) | | 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. Parameters: rootNodeName - The name of the root node enclosing the stream of objects. See Also: XStream.createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader) since: 1.0.3 |
fromXML | public Object fromXML(String xml, Object root)(Code) | | 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 |
fromXML | public Object fromXML(Reader xml, Object root)(Code) | | 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 |
fromXML | public Object fromXML(InputStream xml, Object root)(Code) | | 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 |
getClassLoader | public ClassLoader getClassLoader()(Code) | | Change the ClassLoader XStream uses to load classes.
since: 1.1.1 |
marshal | public void marshal(Object obj, HierarchicalStreamWriter writer, DataHolder dataHolder)(Code) | | 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. Ifnot present, XStream shall create one lazily as needed. throws: XStreamException - if the object cannot be serialized |
omitField | public void omitField(Class definedIn, String fieldName)(Code) | | 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.
since: 1.1.3 throws: InitializationException - if no FieldAliasingMapper is available |
processAnnotations | public void processAnnotations(Class[] types)(Code) | | Process the annotations of the given types and configure the XStream.
Parameters: types - the types with XStream annotations since: 1.3 |
processAnnotations | public void processAnnotations(Class type)(Code) | | 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 since: 1.3 |
registerConverter | public void registerConverter(Converter converter, int priority)(Code) | | |
registerLocalConverter | public void registerLocalConverter(Class definedIn, String fieldName, Converter converter)(Code) | | Register a local
Converter for a field.
Parameters: definedIn - the class type the field is defined in Parameters: fieldName - the field name Parameters: converter - the converter to use since: 1.3 |
registerLocalConverter | public void registerLocalConverter(Class definedIn, String fieldName, SingleValueConverter converter)(Code) | | Register a local
SingleValueConverter for a field.
Parameters: definedIn - the class type the field is defined in Parameters: fieldName - the field name Parameters: converter - the converter to use since: 1.3 |
setClassLoader | public void setClassLoader(ClassLoader classLoader)(Code) | | Change the ClassLoader XStream uses to load classes.
since: 1.1.1 |
setupAliases | protected void setupAliases()(Code) | | |
setupConverters | protected void setupConverters()(Code) | | |
setupDefaultImplementations | protected void setupDefaultImplementations()(Code) | | |
setupImmutableTypes | protected void setupImmutableTypes()(Code) | | |
toXML | public void toXML(Object obj, Writer out)(Code) | | Serialize an object to the given Writer as pretty-printed XML.
throws: XStreamException - if the object cannot be serialized |
unmarshal | public Object unmarshal(HierarchicalStreamReader reader, Object root)(Code) | | 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 |
unmarshal | public Object unmarshal(HierarchicalStreamReader reader, Object root, DataHolder dataHolder)(Code) | | 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 toXStream 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! Parameters: dataHolder - Extra data you can use to pass to your converters. Use this as you want. Ifnot present, XStream shall create one lazily as needed. throws: XStreamException - if the object cannot be deserialized |
useAttributeFor | public void useAttributeFor(String fieldName, Class type)(Code) | | Use an attribute for a field or a specific type.
Parameters: fieldName - the name of the field Parameters: type - the Class of the type to be rendered as XML attribute throws: InitializationException - if no AttributeMapper is available since: 1.2 |
useAttributeFor | public void useAttributeFor(Class definedIn, String fieldName)(Code) | | Use an attribute for a field declared in a specific type.
Parameters: fieldName - the name of the field Parameters: definedIn - the Class containing such field throws: InitializationException - if no AttributeMapper is available since: 1.2.2 |
useAttributeFor | public void useAttributeFor(Class type)(Code) | | Use an attribute for an arbitrary type.
Parameters: type - the Class of the type to be rendered as XML attribute throws: InitializationException - if no AttributeMapper is available since: 1.2 |
useXStream11XmlFriendlyMapper | protected boolean useXStream11XmlFriendlyMapper()(Code) | | |
|
|