I recently started a project where I am exploring whether or not to use GWT-Ext with GWT in order to leverage some of the widgets therein. One of the first things I ran up against, which seems to hit everyone at some point in this same process, is that GWT-Ext doesn't support GWT-RPC for data feeds out of the box (though they do offer a non-free "Plus" package that can do it - I didn't want to drop the cash though just for an evaluation - maybe more on that later).
So what do they use typically, if not GWT-RPC you ask? Well, JSON of course. JSON is actually pretty nice, and might come in handy for more interop as well in my case, so I decided to see how long it would take to turn a few of my server side POJO data objects into JSON - in order to later work with them with GWT-Ext (the GWT-Ext evaluation I haven't completed yet).
Using
XStream it is extremely easy to serialize POJOs to XML, and back again. Now I know there are many libraries that do this, but this is the first one that literally took me 2 minutes to have it working like I expected. Big props to XStream (and Jettison, and XPP3). Below is an example (see the XStream site for more info):
import com.mycompany.proto.gwt.client.model.AssetPojo;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
public class XStreamExample {
public static void main(String[] args) {
// first a POJO (this one is defined in another class,
// but you get the idea, it's a plain getter/setter idiom bean)
AssetPojo ap = new AssetPojo();
ap.setAlpha(0.22);
ap.setBeta(0.12);
ap.setCash(false);
ap.setComposite(false);
ap.setCusip("CUSIP");
ap.setDescription("description");
ap.setId("123E47W");
ap.setTicker("JAVA");
// create a plain jane xstream with an alias (requires xstream and xpp3 on the classpath)
XStream xstream = new XStream();
xstream.alias("assetpojo", AssetPojo.class);
// turn it into XML and display it - this is how it should be my friends, this easy
String xml = xstream.toXML(ap);
System.out.println("XML - \n" + xml);
// turn it back into an object, from the XML
AssetPojo backToObjectXml = (AssetPojo) xstream.fromXML(xml);
System.out.println("\nbackToObjectXml id - " + backToObjectXml.getId());
// now turn it into JSON instead of XML (requires jettison on the classpath)
XStream xstreamJson = new XStream(new JettisonMappedXmlDriver());
xstreamJson.alias("assetpojo", AssetPojo.class);
String json = xstreamJson.toXML(ap);
System.out.println("\nJSON - \n" + json);
// and back to an object again, this time from the JSON
AssetPojo backToObjectJson = (AssetPojo) xstreamJson.fromXML(json);
System.out.println("\nbackToObjectJson id - " + backToObjectJson.getId());
}
}
And the output looks like this:
XML -
<assetpojo>
<id>123E47W</id>
<description>description</description>
<isCash>false</isCash>
<isComposite>false</isComposite>
<alpha>0.22</alpha>
<beta>0.12</beta>
<cusip>CUSIP</cusip>
<ticker>JAVA</ticker>
</assetpojo>
backToObjectXml id - 123E47W
JSON -
{"assetpojo":{"id":"123E47W","description":"description","isCash":false,"isComposite":false,
"alpha":0.22,"beta":0.12,"cusip":"CUSIP","ticker":"JAVA"}}
backToObjectJson id - 123E47W
Comments
Cool
Very cool find. Thanks!!
Yes, seems pretty handy
It's an old project though, been around since 2003 or so. ;) That said, I had never used it before, and only stumbled across it because it's in CXF (and I was looking at Jettison). Old or not though, it's been very useful for me thus far.