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: Martin Probst <mail_at_martin-probst.com>
Date: Mon, 20 Jul 2009 19:01:46 +0200

>> I am sorry, but IMHO using the JAXB->JSON way, you will not
>> be able to get what you want.
>
> This is what I had expected. That really is unfortunate.

I ran into very similar problems with JAXB, JSON and XML. I wonder
what a potential solution would look like. The more I think about it,
the more I think one would need something between a configuration file
and a template mechanism to handle JSON and XML generation in a nice
way. Which of course leaves the deserialization problem entirely
uncovered.

I actually wrote a trivial - about than 150 SLOC - reflection-based
serializer for Java->JSON as the other possibilities were not what I
wanted, but that also makes some assumptions that might not be
generally correct (works on the field level, not methods, ignores
synthetic fields containing "$", ...", more or less relies on
toString() for Map keys, i.e. doesn't support arbitrary map keys).

The way it looks currently, I'll end up writing manual serialization
code for the XML part of the game (maybe (ab-)using XQuery as an XML
templating engine), as JAXB with its Beans requirement is not an
option at all.

Does anyone know of a better solution? This is quite frustrating, and
I'd swear that there are other Java developers that want to serialize
stuff and don't use beans.

Martin