Tweaking the Output
Out of the box, XStream is able to serialize most objects without the need for custom mappings to be setup. The XML produced is clean, however sometimes it's desirable to make tweaks to it. The most common use for this is when using XStream to read configuration files and some more human-friendly XML is needed.
Modification by configuration
A big part of the standard output of XStream can be configured. It is possible to set aliases for class types and field names that are mapped to XML tag or attribute names. Objects that can be represented as simple string value can be written as attributes. It is possible to omit fields or to flatten the structure for collections.
Aliases
Aliases offer a simple way to use different tag or attribute names in the XML. The simplest and most commonly used tweak in XStream is to alias a fully qualified class to a shorter name. Another use case is a different field name for a class member. Aliases can be set for following elements:
- Class types mapped to XML tags
- Field names mapped to XML tags
- Internal attributes names of XStream
The fat elements in the following example are affected:
<cat> <age>4</age> <name>Garfield</name> <owner type="StandardPerson"> <name>Jon Arbuckle</name> </owner> </cat>
Have a look at the Alias Tutorial for examples.
Attributes
XML is quite clumsy to read for fields in separate tags that can represent their content in a short single string value. In such a case attributes can help to shorten the XML and increase readability:
<cat age="4" name="Garfield"> <owner class="StandardPerson" name="Jon Arbuckle"/> </cat>
Attributes are also presented in the Alias Tutorial.
Omitted fields
For a proper deserialization XStream has to write the complete object graph into XML that is referenced by a single object. Therefore XStream has to find a representation that contains all aspects to recreate the objects.
However, some parts might be superfluous e.g. if a member field is lazy initialized and its content can be easily recreated. In such a case a field can be omitted using XStream.omitField(Class, String).
Implicit collections
Another use case are collections. If a class has a field that is a collection, by default all elements of a collection are embedded in an element that represents the collection itself. By configuring the XStream with the XStream.addImplicitCollection() methods it is possible to keep the elements directly as child of the class and the surrounding tag for the collection is omitted. It is even possible to declare more than one implicit collection for a class, but the elements must be distinguishable to populate the different collections correctly at deserialization.
In the following example the Java type representing the farm may have two collections, one for cats and one for dogs:
<farm> <cat>Garfield</cat> <cat>Arlene</cat> <cat>Nermal</cat> <dog>Odie</dog> </farm>
Field order
XStream is delivered with a lot of converters for standard types. Nevertheless most objects are processed by converters based on reflection. They will write the fields of a class in the sequence they are defined. It is possible to implement an algorithm for a different sequence or use an implementation that allows to define the sequence for each type separately.
Enhancing XStream
Sometimes customization is simply not enough to tweak the output. Depending on the use case it is fortunate to use specialized converters for own types, mapper implementations that control naming of things more globally or use specialized writers to influence the complete output.
Custom Converters
Sometimes the object to serialize contains fields or elements, that have no friendly representation for human beings e.g. if a long value represents in reality a time stamp. In such cases XStream supports custom converters for arbitrary types. Have a look at the Converter Tutorial for advanced possibilities.
Custom Mappers
In case of global adjustments it can be helpful to implement an own mapper. A mapper is used to name things and map between the name in the Java world to the name used in the XML representation. The alias mechanism described above is implemented as such a mapper that can be configured. A typical use case is dropping all prefixes for field names like underscores in the resulting XML or omitting the package part of class names.
However, keep in mind that the algorithm must work in both directions to support deserialization.
Custom Writer
A custom writer can be used to affect the output completely. XStream itself delivers solutions for different use cases like the CompactWriter that does not insert any white spaces between the XML tags or the JSON writer that produces a complete different output format.
Another use case for such a writer is to drop unwanted XML elements that XStream omits on its own. Especially if the written XML is not used for deserialization it can be useful to ignore internal attributes by a custom writer
Tweaking the own implementation
As shown, XStream can be configured and enhanced in multiple way, but sometimes it is easier to tweak the implementation of the serialized classes:
- Declaring a field transparent will omit it automatically from the processing
- Implementing a readResolve method that can be used to initialize additional fields
- Usage of annotations
Preprocessing or postprocessing
XML Transformations
Never forget, you're dealing with XML! It is easy to transform XML with an XSLT. XStream is delivered with a SAXSource implementation, that allows an XStream instance to be the source of a XML transformer.
Example
Look at the following stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" omit-xml-declaration="yes" indent="no"/> <xsl:template match="/cat"> <xsl:copy> <xsl:apply-templates select="mName"/> </xsl:copy> </xsl:template> </xsl:stylesheet>
It is used here to remove the age of the cat on the fly (assuming XSLT is a string with the stylesheet above):
XStream xstream = new XStream(); xstream.alias("cat", Cat.class); TraxSource traxSource = new TraxSource(new Cat(4, "Garfield"), xstream); Writer buffer = new StringWriter(); Transformer transformer = TransformerFactory.newInstance().newTransformer( new StreamSource(new StringReader(XSLT))); transformer.transform(traxSource, new StreamResult(buffer));
The result in the buffer:
<cat> <mName>Garfield</mName> </cat>