users@jersey.java.net

Re: [Jersey] java to json and back

From: Tatu Saloranta <cowtowncoder_at_yahoo.com>
Date: Tue, 20 Jan 2009 09:25:32 -0800 (PST)

--- On Tue, 1/20/09, Jakub Podlesak <Jakub.Podlesak_at_Sun.COM> wrote:

...
> > So far, I am keeping my method's arguments and return types as plain String,
> > but that implies a 'boring' layer of manual serialization.
> >
> > I'm currently experimenting with the following APIs:
> > - flexjson (best so far java to json, but could not
> get Deserializer to work
> > yet)
> > - json-lib http://json-lib.sourceforge.net/
> > - http://code.google.com/p/json-simple/
>
> I would add jackson
> (http://www.cowtowncoder.com/hatchery/jackson/index.html)
> but there might be yet another libs.

Thanks Jakub! Yes, latest Jackson version (0.9.6) handles automatic bean serialization (java->json) and deserialization (json->java), based on Bean method introspection, including full support for generic java.util container types. I am biased, but my take on Jackson's benefits is that it is both actively developed and adopted by frameworks. It also happens to be significantly faster than the alternatives, especially when using data binding (latest comments from users gave over 5x speed up, compared to one of alternatives mentioned aboved, YMMV).
The best way to evaluate its goodness is of course try it out and see if it works for you.

Here's sample Jackson data binding usage:
---
// first, write Bean as Json
MyBean bean;
Object mapper = new ObjectMapper();
StringWriter out = new StringWriter();
mapper.writeValue(out, bean);
// and then read it back
String json = out.toString();
MyBean bean2 = mapper.readValue(new StringReader(json), MyBean.class);
---
There are also plans to start recognizing JAXB annotations optionally as additional help, to try to minimize amount of additional configuration one has to do.
To use Jackson would mean (for now?) parsing/writing enclosed String value. But maybe better integration will be possible in future -- either by allowing alternative binding libs to be used directly with Jersey, or by adding specific converters to connect via JAXB extension hooks.
-+ Tatu +-