47
Presented by Angelin

XStream

Embed Size (px)

DESCRIPTION

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.

Citation preview

Page 1: XStream

Presented by

Angelin

Page 2: XStream

Lightweight and easy-to-use open source

Java™ library

Used for serializing java objects into XML and

de-serializing XML back into java objects

Uses reflection API for serialization and

deserialization

Page 3: XStream

xstream-[version].jar and xpp3-[version].jar

are required in the classpath.

Instantiate the XStream class:

XStream xstream = new XStream();

Page 4: XStream

To Serialize an object to a XML String use:

xstream.toXML(Object obj);

To Deserialize an object from an XML String

use:

xstream.fromXML(String xml);

Page 5: XStream

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>

Page 6: XStream
Page 7: XStream
Page 8: XStream
Page 9: XStream
Page 10: XStream

Output

<com.example.Person>

<name>Joe</name>

<age>23</age>

<phone>

<code>123</code>

<number>123456</number>

</phone>

<fax>

<code>123</code>

<number>112233</number>

</fax>

</com.example.Person>

Name: Joe

Age: 23

Phone:123-123456

Fax:123-112233

Page 11: XStream

Syntax: xstream.useAttributeFor(Class definedIn, String fieldName);

Page 12: XStream

Aliasing enables 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

Page 13: XStream

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);

Page 14: XStream

Before Class Aliasing After Class Aliasing

<com.example.Person>

<name>Joe</name>

<age>23</age>

<phone>

<code>123</code>

<number>123456</number>

</phone>

<fax>

<code>123</code>

<number>112233</number>

</fax>

</com.example.Person>

<Person>

<name>Joe</name>

<age>23</age>

<phone>

<code>123</code>

<number>123456</number>

</phone>

<fax>

<code>123</code>

<number>112233</number>

</fax>

</Person>

Syntax:

xstream.alias(String alias, Class clsName);

Example:

xstream.alias(“Person", Person.class);

Page 15: XStream

Syntax:

xstream.aliasField(String alias, Class definedIn, String fieldName);

Example:

xstream.aliasField("Name", Person.class, "name");

Before Field Aliasing After Field Aliasing

<Person>

<name>Joe</name>

<age>23</age>

<phone>

<code>123</code>

<number>123456</number>

</phone>

<fax>

<code>123</code>

<number>112233</number>

</fax>

</Person>

<Person>

<Name>Joe</Name>

<age>23</age>

<phone>

<code>123</code>

<number>123456</number>

</phone>

<fax>

<code>123</code>

<number>112233</number>

</fax>

</Person>

Page 16: XStream

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>

<name>Joe</name>

<age>23</age>

<phone>

<code>123</code>

<number>123456</number>

</phone>

<fax>

<code>123</code>

<number>112233</number>

</fax>

</Person>

<Person>

<Name>Joe</Name>

<age>23</age>

<phone AreaCode="123" Number="123456"/>

<fax AreaCode="123" Number="112233"/>

</Person>

Page 17: XStream

xstream.aliasAttribute(PhoneNumber.class, "code", "AreaCode");

xstream.aliasAttribute(PhoneNumber.class, "number", "Number");

Is equivalent to

xstream.useAttributeFor(PhoneNumber.class, "code");

xstream.aliasField("AreaCode", PhoneNumber.class, "code");

xstream.useAttributeFor(PhoneNumber.class, "number");

xstream.aliasField("Number", PhoneNumber.class, "number");

Page 18: XStream

Syntax:

xstream.aliasPackage(String alias, String packageName);

Example:

xstream.aliasPackage("my.company", "com.example");

Before Field Aliasing After Field Aliasing

<com.example.Person>

<name>Joe</name>

<age>23</age>

<phone>

<code>123</code>

<number>123456</number>

</phone>

<fax>

<code>123</code>

<number>112233</number>

</fax>

</com.example.Person>

<my.company.Person>

<name>Joe</name>

<age>23</age>

<phone>

<code>123</code>

<number>123456</number>

</phone>

<fax>

<code>123</code>

<number>112233</number>

</fax>

</my.company.Person>

Page 19: XStream

Syntax:

xstream.omitField(Class definedIn, String fieldName);

Page 20: XStream
Page 21: XStream
Page 22: XStream
Page 23: XStream

Syntax:

xstream.addImplicitCollection(Class definedIn, String collectionName);

Page 24: XStream
Page 25: XStream

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

Page 26: XStream
Page 27: XStream
Page 28: XStream
Page 29: XStream

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.

Page 30: XStream
Page 31: XStream
Page 32: XStream
Page 33: XStream
Page 34: XStream

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.

Page 35: XStream

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.

Page 36: XStream

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);

}

};

Page 37: XStream

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.

Page 38: XStream

Xstream method Equivalent Xstream

Annotation

xstream.alias("Person", Person.class); @XStreamAlias("Person")

public class Person {

...

...

}

Annotation used to define an XStream class or field value.

Page 39: XStream

Xstream method Equivalent Xstream

Annotation

xstream.aliasAttribute(Person.class,

"company", "Company");

@XStreamAlias("Person")

public class Person {

...

...

@XStreamAsAttribute

@XStreamAlias("Company")

private Company company;

...

...

}

Defines that a field should be serialized as an attribute.

Page 40: XStream

Xstream method Equivalent Xstream Annotation

xstream.registerConverter(new

CompanyConverter());

@XStreamConverter(CompanyConverter.class)

public class Company {

...

}

Annotation to declare a converter.

Xstream method Equivalent Xstream Annotation

xstream.registerConverter(new

CompanyConverter());

@XStreamAlias("Person")

public class Person {

...

@XStreamAsAttribute

@XstreamAlias("Company")

@XStreamConverter(CompanyConverter.class)

private Company company;

...

}

To register the custom converter locally, i.e. only for the

member variable company defined in the Person class

Page 41: XStream

Xstream method Equivalent Xstream Annotation

xstream.addImplicitCollection(Cu

stomers.class, "customers");

@XStreamAlias("Customers")

public class Customers {

@XStreamImplicit

private List customers;

...

...

}

An annotation for marking a field as an implicit collection.

Page 42: XStream

Xstream method Equivalent Xstream Annotation

xstream.omitField(Person.class,

"phone");

@XStreamAlias("Person")

public class Person {

...

@XStreamOmitField

private PhoneNumber phone;

@XStreamOmitField

private PhoneNumber fax;

...

}

Declares a field to be omitted.

Page 43: XStream

Used by base classes to improve annotation processing when

deserializing.

Example:

Page 44: XStream

Annotation processing using autodetectAnnotations

Page 45: XStream

Annotation processing using processAnnotations

Page 46: XStream
Page 47: XStream