users@jersey.java.net

Re: [Jersey] Using XmlElementWrapper/XmlElement for collections breaks JSON formatting

From: Jakub Podlesak <Jakub.Podlesak_at_Sun.COM>
Date: Thu, 14 Oct 2010 15:27:02 +0200

Hi Scott,

POJO->JSON mapping feature should be working for you.
You just need to set the following ResourceConfig feature:

         initParams.put(JSONConfiguration.FEATURE_POJO_MAPPING, "true");

Please see [1] for an example.

HTH,

~Jakub

[1]http://download.java.net/maven/2/com/sun/jersey/samples/jacksonjsonprovider/1.4/jacksonjsonprovider-1.4-project.zip

On 10/12/2010 05:40 PM, Scott Haynie wrote:
> I wanted to resurrect the thread about getting JSON output for
> lists/arrays to look right:
> https://jersey.dev.java.net/servlets/ReadMsg?list=users&msgNo=6480
>
> My requirement is simple: I'd like my JAXB Beans (auto-generated from
> an XSD) to be output as XML or JSON.
>
> Problem is with the collections/arrays handling. In order to have
> proper nesting in XML, I use @XmlElementWrapper/_at_XmlElement on my JAXB:
>
> @XmlElementWrapper(name = "values")
> @XmlElement(name = "value")
> protected List<String> myValues;
>
> This allows the XML to look like this:
>
> <values>
> <value>foo</value>
> <value>bar</value>
> </values>
>
> When this is formatted as JSON, I would like this to be a simple JSON
> array (essentially ignore XmlElementWrapper/XmlElement):
> {"values":["foo", bar"]}
>
> But instead, it gives me this (with the NATURAL configuration):
> {"values":[{"value":"foo", "value":"bar"}]}
>
> This format is much more difficult to deal with for clients, and
> almost makes the service non-usable for complex, nested schemas (ex.
> maps of maps, lists of lists, etc.).
>
> Any hope of providing this anytime soon?
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>