Adorable use of XStream library for java object serialization
Today at work, Thomas has shown me a very genius tool for handling serialization of java objects to store as xml. As long as i think, i thought JAXB or similar libraries are the state of the art libraries. However XStream is, when the use is pending on pure xml serialization, the best choice, i mention. The small API are very welcome, as we can ad-hoc use this librarys without studying a bunch of tutorials and so on…
But how much must we now pay, for getting the appropriate result? No more than 2 lines of code:
public static class Person {
private String firstname;
private String lastname;
private PhoneNumber phone;
private PhoneNumber fax;
public Person(String a, String b){
this.firstname = a;
this.lastname = b; }
public void setFax(PhoneNumber fax) {
this.fax = fax;
}
public PhoneNumber getFax() {
return fax;
}
public String getFirstname() {
return firstname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public void setPhone(PhoneNumber phone) {
this.phone = phone;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
}
public static class PhoneNumber {
private int code;
private String number;
public PhoneNumber(int code, String n){
this.code = code;this.number = n;
}
public void setCode(int code) {
this.code = code;
}
public void setNumber(String number) {
this.number = number;
}
public int getCode() {
return code;
}
public String getNumber() {
return number;
}
}
//next step is to initialize a person with some values
Person scott = new Person(”Martin”, “Scott”);
scott.setPhone(new PhoneNumber(123, “1234-456″));
scott.setFax(new PhoneNumber(123, “9999-999″));
Here comes the 2 lines of code, we only have to code:
XStream streamer = new XStream(); //use of xpp3; it is a high efficently parser of xml;
streamer.toXML(scott); //rreturns string
The Result should be following:
<Start_-Person> <firstname>Martin</firstname> <lastname>Scott</lastname> <phone> <code>123</code> <number>1234-456</number> </phone> <fax> <code>123</code> <number>9999-999</number> </fax> </Start_-Person>
Sure sure… if you want back your object, you only have to specify and manually cast the object that comes from XML:
Person happyScott = (Person)xstream.fromXML("THE XML STREAM OR FILE")
But there is more than just a library. You can refactor particular elements of your serialization approach as you can see:
xstream.alias(”person”, Person.class);
This operation converts the root tag (Start_-Person) to “person”. And if this not enough, also on attributes there can be this operation applied. Converter and deviant converter for every(!) class can be implemented for support individally approching.
Look at the site http://xstream.codehaus.org/ for detailed and further infos.
Comments
One Response to “Adorable use of XStream library for java object serialization”
Leave a Reply

Sebastian Wenzky works since october 09 as a ecm-consultant at Westernacher in Stuttgart. Alfresco, Spring, Hibernate, JBPM and - to much - coffee are now his companions. The corresponding company the right way to go.
xstream rockz if it has to be quick&dirty, otherwise i’d suggest JAX-B (if a xsd is the contract) for marshalling and unmarshalling