Presented byAngelin
 Lightweightand easy-to-use open source Java™ library Usedfor serializing java objects into XML and de-serializing XML b...
 xstream-[version].jarand xpp3-[version].jar are required in the classpath. Instantiate            the XStream class:   ...
 To   Serialize an object to a XML String use:        xstream.toXML(Object obj); ToDeserialize an object from an XML Str...
import com.thoughtworks.xstream.XStream;public class HelloWorld {  public static void main(String[] args) {        XStream...
Output<com.example.Person>       <name>Joe</name>       <age>23</age>       <phone>               <code>123</code>        ...
   Syntax: xstream.useAttributeFor(Class definedIn, String fieldName);
 Aliasingenables us to use different tag or  attribute names in the generated XML. The different types of aliasing that ...
 Xstream  default serialization nature:  fully qualified class name <> element name  corresponding to the class Use Clas...
   Syntax:    xstream.alias(String alias, Class clsName);   Example:    xstream.alias(“Person", Person.class);Before Cla...
   Syntax:    xstream.aliasField(String alias, Class definedIn, String fieldName);   Example:    xstream.aliasField("Nam...
   Syntax:    xstream.aliasAttribute(Class definedIn, String fieldName, String      alias); // makes field as attribute a...
xstream.aliasAttribute(PhoneNumber.class, "code", "AreaCode");xstream.aliasAttribute(PhoneNumber.class, "number", "Number"...
   Syntax:    xstream.aliasPackage(String alias, String packageName);   Example:    xstream.aliasPackage("my.company", "...
   Syntax:    xstream.omitField(Class definedIn, String fieldName);
   Syntax:    xstream.addImplicitCollection(Class definedIn, String collectionName);
 For converting particular types of objects  found in the object graph, to and from XML. Xstream provides converters for...
 To customize the information being serialized  or deserialized. They can be implemented and registered  using the      ...
   Annotations simplify the process of setting aliases and    registering converters etc.   Annotations do not provide m...
 processAnnotation() method to configure  Xstream to process annotations defined in  classes All super types, implemente...
 XStream  can also be run in a lazy mode,  where it auto-detects the annotations while  processing the object graph and c...
Implications of using autodetectAnnotations Deserialization will fail if the type has not  already been processed either ...
   Annotation used to define an XStream class or field value.Xstream method                        Equivalent Xstream    ...
   Defines that a field should be serialized as an attribute.Xstream method                         Equivalent Xstream   ...
   Annotation to declare a converter.Xstream method                  Equivalent Xstream Annotationxstream.registerConvert...
   An annotation for marking a field as an implicit collection.Xstream method                     Equivalent Xstream Anno...
   Declares a field to be omitted.Xstream method                    Equivalent Xstream Annotationxstream.omitField(Person...
   Used by base classes to improve annotation processing when    deserializing.   Example:
   Annotation processing using autodetectAnnotations
   Annotation processing using processAnnotations
XStream
XStream
XStream
XStream
XStream
XStream
XStream
XStream
XStream
XStream
XStream
XStream
XStream
XStream
XStream
XStream
XStream
Upcoming SlideShare
Loading in …5
×

XStream

1,756 views

Published on

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.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,756
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
27
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

XStream

  1. 1. Presented byAngelin
  2. 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
  3. 3.  xstream-[version].jarand xpp3-[version].jar are required in the classpath. Instantiate the XStream class: XStream xstream = new XStream();
  4. 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. 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>
  6. 6. 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: JoeAge: 23Phone:123-123456Fax:123-112233
  7. 7.  Syntax: xstream.useAttributeFor(Class definedIn, String fieldName);
  8. 8.  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
  9. 9.  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);
  10. 10.  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>
  11. 11.  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>
  12. 12.  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>
  13. 13. xstream.aliasAttribute(PhoneNumber.class, "code", "AreaCode");xstream.aliasAttribute(PhoneNumber.class, "number", "Number");Is equivalent toxstream.useAttributeFor(PhoneNumber.class, "code");xstream.aliasField("AreaCode", PhoneNumber.class, "code");xstream.useAttributeFor(PhoneNumber.class, "number");xstream.aliasField("Number", PhoneNumber.class, "number");
  14. 14.  Syntax: xstream.aliasPackage(String alias, String packageName); Example: xstream.aliasPackage("my.company", "com.example");Before Field Aliasing After Field Aliasing<com.example.Person> <my.company.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> </my.company.Person>
  15. 15.  Syntax: xstream.omitField(Class definedIn, String fieldName);
  16. 16.  Syntax: xstream.addImplicitCollection(Class definedIn, String collectionName);
  17. 17.  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
  18. 18.  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.
  19. 19.  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.
  20. 20.  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.
  21. 21.  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); } };
  22. 22. Implications of using autodetectAnnotations Deserialization will fail if the type has not already been processed either by having called XStreams 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.
  23. 23.  Annotation used to define an XStream class or field value.Xstream method Equivalent Xstream Annotationxstream.alias("Person", Person.class); @XStreamAlias("Person") public class Person { ... ... }
  24. 24.  Defines that a field should be serialized as an attribute.Xstream method Equivalent Xstream Annotationxstream.aliasAttribute(Person.class, @XStreamAlias("Person")"company", "Company"); public class Person { ... ... @XStreamAsAttribute @XStreamAlias("Company") private Company company; ... ... }
  25. 25.  Annotation to declare a converter.Xstream method Equivalent Xstream Annotationxstream.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 classXstream method Equivalent Xstream Annotationxstream.registerConverter(new @XStreamAlias("Person")CompanyConverter()); public class Person { ... @XStreamAsAttribute @XstreamAlias("Company") @XStreamConverter(CompanyConverter.class) private Company company; ... }
  26. 26.  An annotation for marking a field as an implicit collection.Xstream method Equivalent Xstream Annotationxstream.addImplicitCollection(Cu @XStreamAlias("Customers")stomers.class, "customers"); public class Customers { @XStreamImplicit private List customers; ... ... }
  27. 27.  Declares a field to be omitted.Xstream method Equivalent Xstream Annotationxstream.omitField(Person.class, @XStreamAlias("Person")"phone"); public class Person { ... @XStreamOmitField private PhoneNumber phone; @XStreamOmitField private PhoneNumber fax; ... }
  28. 28.  Used by base classes to improve annotation processing when deserializing. Example:
  29. 29.  Annotation processing using autodetectAnnotations
  30. 30.  Annotation processing using processAnnotations

×