XStream is a simple library to serialize objects to XML and back again.
Features of XStream
A. Ease of use. A high level facade is supplied that simplifies common use cases.
No mappings required. Most objects can be serialized without need for specifying mappings.
B. Performance. Speed and low memory footprint are a crucial part of the design, making it suitable for large object graphs or systems with high message throughput.
C. Clean XML. No information is duplicated that can be obtained via reflection. This results in XML that is easier to read for humans and more compact than native Java serialization.
D. Requires no modifications to objects. Serializes internal fields, including private and final. Supports non-public and inner classes. Classes are not required to have default constructor.
E. Full object graph support. Duplicate references encountered in the object-model will be maintained. Supports circular references.
F. Integrates with other XML APIs. By implementing an interface, XStream can serialize directly to/from any tree structure (not just XML).
G. Customizable conversion strategies. Strategies can be registered allowing customization of how particular types are represented as XML.
H. Error messages. When an exception occurs due to malformed XML, detailed diagnostics are provided to help isolate and fix the problem.
I. Alternative output format. The modular design allows other output formats. XStream ships currently with JSON support and morphing.
No mappings required. Most objects can be serialized without need for specifying mappings.
B. Performance. Speed and low memory footprint are a crucial part of the design, making it suitable for large object graphs or systems with high message throughput.
C. Clean XML. No information is duplicated that can be obtained via reflection. This results in XML that is easier to read for humans and more compact than native Java serialization.
D. Requires no modifications to objects. Serializes internal fields, including private and final. Supports non-public and inner classes. Classes are not required to have default constructor.
E. Full object graph support. Duplicate references encountered in the object-model will be maintained. Supports circular references.
F. Integrates with other XML APIs. By implementing an interface, XStream can serialize directly to/from any tree structure (not just XML).
G. Customizable conversion strategies. Strategies can be registered allowing customization of how particular types are represented as XML.
H. Error messages. When an exception occurs due to malformed XML, detailed diagnostics are provided to help isolate and fix the problem.
I. Alternative output format. The modular design allows other output formats. XStream ships currently with JSON support and morphing.
Below is the small POC to serialize/de-serialize non-serializable objects. Have to include XStream jar into classpath.
Foo Object
public class Foo {
private String foo ;
public void setFoo(String s) {
foo = s;
}
public String getFoo() {
return foo;
}
}
Object Helper
public class FooHelper {
public static void write(Object f, String filename) throws Exception {
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(filename)));
encoder.writeObject(f);
encoder.close();
}
public static Object read(String filename) throws Exception {
XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream(filename)));
Object o = decoder.readObject();
decoder.close();
return o;
}
}
XStream Wrapper
public class FooWraper implements Serializable{
String xstreamStr;
public String getXstreamStr() {
return xstreamStr;
}
public void setXstreamStr(String xstreamStr) {
this.xstreamStr = xstreamStr;
}
}
Object Helper
public class ObjectHelper {
private ObjectHelper(){ }
public static Object fromBytes(byte[] aBBytes) {
Object mOReadObj = null;
try {
final ObjectInputStream mVOis = new ObjectInputStream(
new ByteArrayInputStream(aBBytes));
mOReadObj = mVOis.readObject();
mVOis.close();
} catch (Exception ex) {
ex.getStackTrace();
}
return mOReadObj;
}
public static byte[] toBytes(Object aOObject) throws IOException {
final ByteArrayOutputStream mVBaos = new ByteArrayOutputStream();
final ObjectOutputStream mVOos = new ObjectOutputStream(mVBaos);
mVOos.writeObject(aOObject);
mVOos.close();
final byte[] mBVal = mVBaos.toByteArray();
mVBaos.close();
return mBVal;
}
}
Caller Class
public class FooTest {
public static void main(String[] args) throws Exception {
Foo f1 = new Foo();
f1.setFoo("Crédito de cuerdas");
//Using Xtream ..............
byte[] b = new byte[4048];
XStream xstream = new XStream();
long stime = System.currentTimeMillis();
String xml = xstream.toXML(f1);
FooWraper wrap = new FooWraper();
wrap.setXstreamStr(xml);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("c:/foo.txt")));
byte[] f = ObjectHelper.toBytes(wrap);
bos.write(f);
bos.close();
long ltime = System.currentTimeMillis();
System.out.println("Elapsed time for Serialization =>" + (ltime - stime));
stime = System.currentTimeMillis();
System.out.print(xml);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("c:/foo.txt"));
bis.read(b);
bis.close();
FooWraper f2 = (FooWraper) ObjectHelper.fromBytes(b);
Foo foo = (Foo) xstream.fromXML(f2.getXstreamStr());
System.out.println("\n" + foo.getFoo());
ltime = System.currentTimeMillis();
System.out.println("Elapsed time for deserialization =>" + (ltime - stime));
System.exit(0);
}
}
0 comments:
Post a Comment