开发者

Java lib that reads and writes xml or json w/ a simple switch

Ideally writing should simply a matter of switching writers. One write would write json and another xml. The same would be true of the reverse, one factory takes an InputStream of json and another an InputStream of xml. In both cases the binding stuff would ret开发者_StackOverflowurn an object stream given an InputStream.


XStream does this pretty darn well.

Normal instantiation (for XML):

new XStream();

Alternative instantiation (for JSON):

new XStream(new JettisonMappedXmlDriver());
/**************** or ****************/
new XStream(new JsonHierarchicalStreamDriver());


Also, while not a simple lib, JAX-RS implementations (like Jersey, RESTeasy) offer simple way to produce and consume both JSON and xml. They handle choice of backend library transparently based on media type being requested.

Build a facade is rather simple too; I would start by using JAXB for xml and Jackson for JSON; both can take InputStream or Reader, and output using OutputStream or Writer. And both do reasonably good job with data binding.


If you are looking to code this yourself - it sounds like a good case for a Strategy pattern and a Factory Method pattern

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜