users@jersey.java.net

RE: [Jersey] How do I marshal nested lists as JSON? I get an array of nulls or an array of one-element dictionaries containing an array.

From: Stevens, Ian <IStevens_at_globeandmail.com>
Date: Mon, 20 Jul 2009 11:59:27 -0400

Hi Jakub. Thanks for your response.

> > The object I'd like to convert first represented data as a
> > <LinkedList<LinkedList<String>>>, and I figured Jersey
> You needed concrete List implementation, as JAXB refused to
> play with the interface List, right?

Yes, that is correct.

> > After reading that nested objects need to be wrapped, I tried the
> Where did you read that? I presume it is part of JAXB docs?

A few places, most of which I've lost but one of them was in this
thread:
http://n2.nabble.com/Can-a-List%3CInteger%3E-be-serialized-to-JSON-witho
ut-being-wrapped--td2955972.html#a2955972.

> > That code outputs what's below, which is closer to what I want:
> >
> > {"data":[{"data":["one", "two", "three"]},{"data":["a", "b",
> > "c"]}]}
> >
> > I want the first data to be a list of lists, not a list of
> > one-element dictionaries. How do I achieve this?
> I am sorry, but IMHO using the JAXB->JSON way, you will not
> be able to get what you want.
> You might want to use the low-level JSON approach [1] for
> generating the JSON, or Tatu's Java->JSON provider [2].
>
> [1]https://jersey.dev.java.net/documentation/1.1.1-ea/user-gui
de.html#d4e645
> [2]http://www.cowtowncoder.com/blog/archives/2009/03/entry_229.html

This is what I had expected. That really is unfortunate. The code I'm
working on relies heavily on JAX-WS and JAX-RS. It's not my code, and
there doesn't seem to be an easy way to override the JSON output for a
Path. I'll have to look further into that, although it would probably
involve creating a new Provider. It's something to consider as I'm not
happy with the existing code.

Thanks for your help Jakub,
Ian.