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 {@link com.thoughtworks.xstream.converters.Converter} instances, each of which acts as astrategy 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 {@link com.thoughtworks.xstream.converters.extended} package and you can create your own byimplementing the {@link com.thoughtworks.xstream.converters.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 {@link com.thoughtworks.xstream.converters.reflection.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.SINGLE_NODE_XPATH_RELATIVE_REFERENCES); | Uses XPath relative references to signify duplicate references. The XPath expression ensures that a single node only is selected always. |
xstream.setMode(XStream.SINGLE_NODE_XPATH_ABSOLUTE_REFERENCES); | Uses XPath absolute references to signify duplicate references. The XPath expression ensures that a single node only is selected always. |
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|