By reading this article you can learn what is the standard way of using XStream in profeessional projects to generate XML or JSON or Objects. I have worked for Fortune-500 company project and learnt this method.

Some important rules :

1.  Use only POJO class to generate XML or JSON.
2.  Use one single class XMLGenerator.java to generate All XML  or objects.
3.  Use one single class JSONGenerator.java to generate All JSON or objects.
4.  Whenever you want to generate XML or JSON or Object you should be able to generate by single line of code.
5.  If you want to build web service then you should be careful when using XStream and don't feed it XML retrieved from untrusted sources. http://blog.diniscruz.com/2013/12/xstream-remote-code-execution-exploit.html

Prepare Pojo Classes

@XStreamAlias annotation is to define XML or JSON tag. Add @XStreamAlias annotation to classes and variables.
Square.java
import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("square")
public class Square {
 @XStreamAlias("size")
 int size;

 public int getSize() {
  return size;
 }

 public void setSize(int size) {
  this.size = size;
 }

}
Rectangle.java
import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("rectangle")
public class Rectangle {
 @XStreamAlias("width")
 int width;
        @XStreamAlias("height")
 int height;

 public int getWidth() {
  return width;
 }

 public void setWidth(int width) {
  this.width = width;
 }

        public int getHeight() {
  return height;
 }

 public void setHeight(int width) {
  this.height = height;
 }
}

XMLGenerator.java

A common class for XML conversion

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public final class XMLGenerator {
/*
 * this class is for generating XML 
 */
 
 /*
  * initialization of XStream class 
  */
 private static XStream xstream = new XStream(new DomDriver())
 {{
  processAnnotations(Square.class);
  processAnnotations(Rectangle.class);
 }};
 
 /*
  * This class is for generating XML from MODEL class
  * @param Object
  * @return String 
  */
 public static String generateXML(Object to) {
  return null == to ? "" : xstream.toXML(to);
 }

 /*
  * Generates the transfer object from the given XML using XStream.
  * 
  * @param String
  * @return transfer object
  */
 public static Object generateTOfromXML(String xml) {
  return xstream.fromXML(xml);
 } 
}

Usage :

Convert Object to XML 
Square sq = new Square();
sq.setSize(5);
String resultXML = XMLGenerator.generateXML(sq); 

Convert XML to Object
Square sq1 = (Square)XMLGenerator.generateTOfromXML(inputXML);

JSONGenerator.java

A common class for JSON conversion
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;

public final class JSONGenerator {
/*
 * this class is for generating JSON 
 */
 
 /*
  * initialization of XStream class 
  */
 private static XStream xstream = new XStream(new  JettisonMappedXmlDriver())
 {{
  processAnnotations(Square.class);
  processAnnotations(Rectangle.class);
 }};
 
 /*
  * This class is for generating JSON from MODEL class
  * @param Object
  * @return String 
  */
 public static String generateJSON(Object to) {
  return null == to ? "" : xstream.toXML(to);
 }

/*
  * Generates the transfer object from the given JSON using XStream.
  * 
  * @param String
  * @return transfer object
  */
 public static Object generateTOfromJSON(String json) {
  return xstream.toXML(json);
 } 
 
}

Usage :

Convert Object to JSON
Square sq = new Square();
sq.setSize(5);
String resultJSON = JSONGenerator.generateJSON(sq); 

Convert JSON to Object
Square sq = (Square)JSONGenerator.generateTOfromJSON(inputJSON);

0 comments:

Blogroll

Follow this blog by Email

Popular Posts