|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.thoughtworks.xstream.XStream
public class 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 occurences 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 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 ReflectionConverter
as the initial default converter.
xstream.changeDefaultConverter(new ACustomDefaultConverter());
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 XPath or IDs. The
mode can be changed using setMode()
:
xstream.setMode(XStream.XPATH_REFERENCES); |
(Default) Uses XPath 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 seperate 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.
To avoid the need for special tags for collections, you can define implicit collections using one of the
addImplicitCollection
methods.
Field Summary | |
---|---|
static int |
ID_REFERENCES
|
static int |
NO_REFERENCES
|
static int |
XPATH_REFERENCES
|
Constructor Summary | |
---|---|
XStream()
|
|
XStream(com.thoughtworks.xstream.converters.Converter defaultConverter)
Deprecated. As of XStream 1.1.1, a default Converter is unnecessary as you can register a Converter with an associated priority. Use an alternate constructor. |
|
XStream(com.thoughtworks.xstream.io.HierarchicalStreamDriver hierarchicalStreamDriver)
|
|
XStream(com.thoughtworks.xstream.converters.reflection.ReflectionProvider reflectionProvider)
|
|
XStream(com.thoughtworks.xstream.converters.reflection.ReflectionProvider reflectionProvider,
ClassMapper classMapper,
com.thoughtworks.xstream.io.HierarchicalStreamDriver driver)
|
|
XStream(com.thoughtworks.xstream.converters.reflection.ReflectionProvider reflectionProvider,
ClassMapper classMapper,
com.thoughtworks.xstream.io.HierarchicalStreamDriver driver,
String classAttributeIdentifier)
|
|
XStream(com.thoughtworks.xstream.converters.reflection.ReflectionProvider reflectionProvider,
ClassMapper classMapper,
com.thoughtworks.xstream.io.HierarchicalStreamDriver driver,
String classAttributeIdentifier,
com.thoughtworks.xstream.converters.Converter defaultConverter)
Deprecated. As of XStream 1.1.1, a default Converter is unnecessary as you can register a Converter with an associated priority. Use an alternate constructor. |
|
XStream(com.thoughtworks.xstream.converters.reflection.ReflectionProvider reflectionProvider,
com.thoughtworks.xstream.io.HierarchicalStreamDriver hierarchicalStreamDriver)
|
Method Summary | |
---|---|
void |
addDefaultCollection(Class ownerType,
String fieldName)
Deprecated. Use addImplicitCollection() instead. |
void |
addDefaultImplementation(Class defaultImplementation,
Class ofType)
Associate a default implementation of a class with an object. |
void |
addImmutableType(Class type)
|
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 |
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 |
alias(String name,
Class type)
Alias a Class to a shorter name to be used in XML elements. |
void |
alias(String name,
Class type,
Class defaultImplementation)
Alias a Class to a shorter name to be used in XML elements. |
protected ClassMapper |
buildMapper(String classAttributeIdentifier)
|
void |
changeDefaultConverter(com.thoughtworks.xstream.converters.Converter defaultConverter)
Deprecated. As of 1.1.1 you should register a converter with the appropriate priority. |
ObjectInputStream |
createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader reader)
Creates an ObjectInputStream that deserializes a stream of objects from a reader using XStream. |
ObjectInputStream |
createObjectInputStream(Reader xmlReader)
Creates an ObjectInputStream that deserializes a stream of objects from a reader using XStream. |
ObjectOutputStream |
createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter writer)
Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream. |
ObjectOutputStream |
createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter writer,
String rootNodeName)
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(Writer writer,
String rootNodeName)
Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream. |
Object |
fromXML(Reader xml)
Deserialize an object from an XML Reader, populating the fields of the given root object instead of instantiating a new one. |
Object |
fromXML(Reader xml,
Object root)
Deserialize an object from an XML Reader. |
Object |
fromXML(String xml)
Deserialize an object from an XML String. |
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. |
ClassLoader |
getClassLoader()
Change the ClassLoader XStream uses to load classes. |
ClassMapper |
getClassMapper()
|
com.thoughtworks.xstream.converters.ConverterLookup |
getConverterLookup()
|
void |
marshal(Object obj,
com.thoughtworks.xstream.io.HierarchicalStreamWriter writer)
Serialize and object to a hierarchical data structure (such as XML). |
void |
marshal(Object obj,
com.thoughtworks.xstream.io.HierarchicalStreamWriter writer,
com.thoughtworks.xstream.converters.DataHolder dataHolder)
Serialize and object to a hierarchical data structure (such as XML). |
com.thoughtworks.xstream.converters.DataHolder |
newDataHolder()
|
void |
registerConverter(com.thoughtworks.xstream.converters.Converter converter)
|
void |
registerConverter(com.thoughtworks.xstream.converters.Converter converter,
int priority)
|
void |
setClassLoader(ClassLoader classLoader)
Change the ClassLoader XStream uses to load classes. |
void |
setMarshallingStrategy(com.thoughtworks.xstream.MarshallingStrategy marshallingStrategy)
|
void |
setMode(int mode)
Change mode for dealing with duplicate references. |
protected void |
setupAliases()
|
protected void |
setupConverters()
|
protected void |
setupDefaultImplementations()
|
protected void |
setupImmutableTypes()
|
String |
toXML(Object obj)
Serialize an object to a pretty-printed XML String. |
void |
toXML(Object obj,
Writer writer)
Serialize an object to the given Writer as pretty-printed XML. |
Object |
unmarshal(com.thoughtworks.xstream.io.HierarchicalStreamReader reader)
Deserialize an object from a hierarchical data structure (such as XML). |
Object |
unmarshal(com.thoughtworks.xstream.io.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(com.thoughtworks.xstream.io.HierarchicalStreamReader reader,
Object root,
com.thoughtworks.xstream.converters.DataHolder dataHolder)
Deserialize an object from a hierarchical data structure (such as XML). |
protected MapperWrapper |
wrapMapper(MapperWrapper next)
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int NO_REFERENCES
public static final int ID_REFERENCES
public static final int XPATH_REFERENCES
Constructor Detail |
---|
public XStream()
public XStream(com.thoughtworks.xstream.converters.Converter defaultConverter)
public XStream(com.thoughtworks.xstream.io.HierarchicalStreamDriver hierarchicalStreamDriver)
public XStream(com.thoughtworks.xstream.converters.reflection.ReflectionProvider reflectionProvider)
public XStream(com.thoughtworks.xstream.converters.reflection.ReflectionProvider reflectionProvider, com.thoughtworks.xstream.io.HierarchicalStreamDriver hierarchicalStreamDriver)
public XStream(com.thoughtworks.xstream.converters.reflection.ReflectionProvider reflectionProvider, ClassMapper classMapper, com.thoughtworks.xstream.io.HierarchicalStreamDriver driver)
public XStream(com.thoughtworks.xstream.converters.reflection.ReflectionProvider reflectionProvider, ClassMapper classMapper, com.thoughtworks.xstream.io.HierarchicalStreamDriver driver, String classAttributeIdentifier)
public XStream(com.thoughtworks.xstream.converters.reflection.ReflectionProvider reflectionProvider, ClassMapper classMapper, com.thoughtworks.xstream.io.HierarchicalStreamDriver driver, String classAttributeIdentifier, com.thoughtworks.xstream.converters.Converter defaultConverter)
Method Detail |
---|
protected ClassMapper buildMapper(String classAttributeIdentifier)
protected MapperWrapper wrapMapper(MapperWrapper next)
protected void setupAliases()
protected void setupDefaultImplementations()
protected void setupConverters()
protected void setupImmutableTypes()
public void setMarshallingStrategy(com.thoughtworks.xstream.MarshallingStrategy marshallingStrategy)
public String toXML(Object obj)
public void toXML(Object obj, Writer writer)
public void marshal(Object obj, com.thoughtworks.xstream.io.HierarchicalStreamWriter writer)
public void marshal(Object obj, com.thoughtworks.xstream.io.HierarchicalStreamWriter writer, com.thoughtworks.xstream.converters.DataHolder dataHolder)
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.public Object fromXML(String xml)
public Object fromXML(Reader xml)
public Object fromXML(String xml, Object root)
public Object fromXML(Reader xml, Object root)
public Object unmarshal(com.thoughtworks.xstream.io.HierarchicalStreamReader reader)
public Object unmarshal(com.thoughtworks.xstream.io.HierarchicalStreamReader reader, Object root)
public Object unmarshal(com.thoughtworks.xstream.io.HierarchicalStreamReader reader, Object root, com.thoughtworks.xstream.converters.DataHolder dataHolder)
root
- If present, the passed in object will have its fields populated, as opposed to XStream creating a
new instance.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.public void alias(String name, Class type)
name
- Short nametype
- Type to be aliasedpublic void alias(String name, Class type, Class defaultImplementation)
name
- Short nametype
- Type to be aliaseddefaultImplementation
- Default implementation of type to use if no other specified.public void addDefaultImplementation(Class defaultImplementation, Class ofType)
defaultImplementation
- ofType
- public void addImmutableType(Class type)
public void changeDefaultConverter(com.thoughtworks.xstream.converters.Converter defaultConverter)
public void registerConverter(com.thoughtworks.xstream.converters.Converter converter)
public void registerConverter(com.thoughtworks.xstream.converters.Converter converter, int priority)
public ClassMapper getClassMapper()
public com.thoughtworks.xstream.converters.ConverterLookup getConverterLookup()
public void setMode(int mode)
XStream.XPATH_REFERENCES
,
XStream.ID_REFERENCES
and XStream.NO_REFERENCES
.
XPATH_REFERENCES
,
ID_REFERENCES
,
NO_REFERENCES
public void addDefaultCollection(Class ownerType, String fieldName)
public void addImplicitCollection(Class ownerType, String fieldName)
ownerType
- class owning the implicit collectionfieldName
- name of the field in the ownerType. This filed must be an java.util.ArrayList
.public void addImplicitCollection(Class ownerType, String fieldName, Class itemType)
ownerType
- class owning the implicit collectionfieldName
- name of the field in the ownerType. This filed must be an java.util.ArrayList
.itemType
- type of the items to be part of this collection.public void addImplicitCollection(Class ownerType, String fieldName, String itemFieldName, Class itemType)
ownerType
- class owning the implicit collectionfieldName
- name of the field in the ownerType. This filed must be an java.util.ArrayList
.itemFieldName
- element name of the implicit collectionitemType
- item type to be aliases be the itemFieldNamepublic com.thoughtworks.xstream.converters.DataHolder newDataHolder()
public ObjectOutputStream createObjectOutputStream(Writer writer) throws IOException
To change the name of the root element (from <object-stream>), use
createObjectOutputStream(java.io.Writer, String)
.
IOException
createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String)
,
createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader)
public ObjectOutputStream createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter writer) throws IOException
To change the name of the root element (from <object-stream>), use
createObjectOutputStream(java.io.Writer, String)
.
IOException
createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String)
,
createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader)
public ObjectOutputStream createObjectOutputStream(Writer writer, String rootNodeName) throws IOException
IOException
createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String)
,
createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader)
public ObjectOutputStream createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter writer, String rootNodeName) throws IOException
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
createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader)
public ObjectInputStream createObjectInputStream(Reader xmlReader) throws IOException
IOException
createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader)
,
createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String)
public ObjectInputStream createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader reader) throws IOException
ObjectInputStream in = xstream.createObjectOutputStream(aReader); int a = out.readInt(); Object b = out.readObject(); Object c = out.readObject();
IOException
createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String)
public void setClassLoader(ClassLoader classLoader)
public ClassLoader getClassLoader()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |