This is a presentation about XStream, the lightweight and easy-to-use open source Java™ library, used for serializing java objects into XML and de-serializing XML back into java objects.
2. Lightweightand easy-to-use open source
Java™ library
Usedfor serializing java objects into XML and
de-serializing XML back into java objects
Usesreflection API for serialization and
deserialization
4. To Serialize an object to a XML String use:
xstream.toXML(Object obj);
ToDeserialize an object from an XML String
use:
xstream.fromXML(String xml);
5. import com.thoughtworks.xstream.XStream;
public class HelloWorld {
public static void main(String[] args) {
XStream xstream = new XStream();
String salutation = "Hello, World!";
String xml = xstream.toXML(salutation);
System.out.print(xml);
}
}
Output
<string>Hello, World!</string>
12. Aliasingenables us to use different tag or
attribute names in the generated XML.
The different types of aliasing that Xstream
supports are:
Class aliasing
Field aliasing
Attribute aliasing
Package aliasing
Omitting fields and root tag of collection
13. Xstream default serialization nature:
fully qualified class name <> element name
corresponding to the class
Use Class Aliasing to get the class name
(without the package name) as the XML
element name
To create alias for any class' name, use
xstream.alias(String alias, Class clsname);
14. Syntax:
xstream.alias(String alias, Class clsName);
Example:
xstream.alias(“Person", Person.class);
Before Class Aliasing After Class Aliasing
<com.example.Person> <Person>
<name>Joe</name> <name>Joe</name>
<age>23</age> <age>23</age>
<phone> <phone>
<code>123</code> <code>123</code>
<number>123456</number> <number>123456</number>
</phone> </phone>
<fax> <fax>
<code>123</code> <code>123</code>
<number>112233</number> <number>112233</number>
</fax> </fax>
</com.example.Person> </Person>
15. Syntax:
xstream.aliasField(String alias, Class definedIn, String fieldName);
Example:
xstream.aliasField("Name", Person.class, "name");
Before Field Aliasing After Field Aliasing
<Person> <Person>
<name>Joe</name> <Name>Joe</Name>
<age>23</age> <age>23</age>
<phone> <phone>
<code>123</code> <code>123</code>
<number>123456</number> <number>123456</number>
</phone> </phone>
<fax> <fax>
<code>123</code> <code>123</code>
<number>112233</number> <number>112233</number>
</fax> </fax>
</Person> </Person>
16. Syntax:
xstream.aliasAttribute(Class definedIn, String fieldName, String
alias); // makes field as attribute and sets an alias name for it
Example:
xstream.aliasAttribute(PhoneNumber.class, "code", "AreaCode");
xstream.aliasAttribute(PhoneNumber.class, "number", "Number");
Before Attribute Aliasing After Attribute Aliasing
<Person> <Person>
<name>Joe</name> <Name>Joe</Name>
<age>23</age> <age>23</age>
<phone> <phone AreaCode="123" Number="123456"/>
<code>123</code> <fax AreaCode="123" Number="112233"/>
<number>123456</number> </Person>
</phone>
<fax>
<code>123</code>
<number>112233</number>
</fax>
</Person>
25. For converting particular types of objects
found in the object graph, to and from XML.
Xstream provides converters for primitives,
String, File, Collections, arrays, and Dates.
Types:
Converters for converting common basic types in
Java into a single String, with no nested
elements
Converters for converting items in collections
such as arrays, Lists, Sets and Maps into nested
elements
26.
27.
28.
29. To customize the information being serialized
or deserialized.
They can be implemented and registered
using the
XStream.registerConverter() method
Converters for objects that can store all
information in a single value should
implement SingleValueConverter.
30.
31.
32.
33.
34. Annotations simplify the process of setting aliases and
registering converters etc.
Annotations do not provide more functionality, but may
improve convenience.
Annotation Types Description
@XStreamAlias Annotation used to define an XStream class or
field value.
@XStreamAsAttribute Defines that a field should be serialized as an
attribute.
@XStreamConverter Annotation to declare a converter.
@XStreamImplicit An annotation for marking a field as an
implicit collection.
@XStreamInclude Annotation to force automated processing of
further classes.
@XStreamOmitField Declares a field to be omitted.
35. processAnnotation() method to configure
Xstream to process annotations defined in
classes
All super types, implemented interfaces, the
class types of the members and all their generic
types will be processed.
For e.g. the statement
xstream.processAnnotations(Person.class);
will also process the annotations of its member
of type Company. So there is no need to
explicitly configure processAnnotation() for the
class Company.
36. XStream can also be run in a lazy mode,
where it auto-detects the annotations while
processing the object graph and configures
the XStream instance on-the-fly.
Example:
XStream xstream = new XStream() {
{
autodetectAnnotations(true);
}
};
37. Implications of using autodetectAnnotations
Deserialization will fail if the type has not
already been processed either by having called
XStream's processAnnotations method or by
already having serialized this type. However,
@XStreamAlias is the only annotation that may
fail in this case
May cause thread-unsafe operation
will slow down the marshalling process until all
processed types have been examined once.
38. Annotation used to define an XStream class or field value.
Xstream method Equivalent Xstream
Annotation
xstream.alias("Person", Person.class); @XStreamAlias("Person")
public class Person {
...
...
}
39. Defines that a field should be serialized as an attribute.
Xstream method Equivalent Xstream
Annotation
xstream.aliasAttribute(Person.class, @XStreamAlias("Person")
"company", "Company"); public class Person {
...
...
@XStreamAsAttribute
@XStreamAlias("Company")
private Company company;
...
...
}
40. Annotation to declare a converter.
Xstream method Equivalent Xstream Annotation
xstream.registerConverter(new @XStreamConverter(CompanyConverter.class)
CompanyConverter()); public class Company {
...
}
To register the custom converter locally, i.e. only for the
member variable company defined in the Person class
Xstream method Equivalent Xstream Annotation
xstream.registerConverter(new @XStreamAlias("Person")
CompanyConverter()); public class Person {
...
@XStreamAsAttribute
@XstreamAlias("Company")
@XStreamConverter(CompanyConverter.class)
private Company company;
...
}
41. An annotation for marking a field as an implicit collection.
Xstream method Equivalent Xstream Annotation
xstream.addImplicitCollection(Cu @XStreamAlias("Customers")
stomers.class, "customers"); public class Customers {
@XStreamImplicit
private List customers;
...
...
}
42. Declares a field to be omitted.
Xstream method Equivalent Xstream Annotation
xstream.omitField(Person.class, @XStreamAlias("Person")
"phone"); public class Person {
...
@XStreamOmitField
private PhoneNumber phone;
@XStreamOmitField
private PhoneNumber fax;
...
}
43. Used by base classes to improve annotation processing when
deserializing.
Example:
44. Annotation processing using autodetectAnnotations
45. Annotation processing using processAnnotations