users@jersey.java.net

Re: [Jersey] Single Element Arrays and JSON

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

Hi Charles,

i apologize for late response.

You have two options:

1) try to use natural notation, JSONConfiguration.natural().build(),
instead of the mapped one
     then information on arrays should be propagated automatically from
JAXB to the JSON processing layer
     see [1] for an example

2) use POJO->JSON direct mapping (i.e. no JAXB involved), where the
format you get is probably even more "natural"
     configuration is done using a ResourceConfig feature:
         initParams.put(JSONConfiguration.FEATURE_POJO_MAPPING, "true");
     see [2] for an example

i hope one of the above will work for you, if not, please let me know.

Thanks,

~Jakub

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

On 10/12/2010 06:58 PM, Charles Overbeck wrote:
> I never got a reply to this. I'm not complaining; I realize nobody
> owes me a reply, and I do appreciate all the great support I've gotten
> here for my other questions. But I'm hoping this got overlooked due to
> JavaOne. :)
>
> Is there really no other solution than to manually and explicitly
> specify which fields should be arrays via the
> JSONConfiguration.mapped.arrays()?
>
> Thanks in advance,
>
> Charles
>
> P.S. Apologies in advance if this gets posted twice. It seemed like
> the newsgroup was down for several days; my first message from last
> Friday bounced back. So I'm posting a new one here.
>
> ------------------------------------------------------------------------
> *From:* Charles Overbeck <coverbec_at_pacbell.net>
> *To:* users_at_jersey.dev.java.net
> *Sent:* Tue, September 14, 2010 2:00:45 PM
> *Subject:* [Jersey] Single Element Arrays and JSON
>
> Hello,
>
> Using Jersey/JAXB (Jersey version 1.1.5.1), I ran across an issue
> where the JSON representation of a l element List doesn't come back as
> an array, but if there are 2 or more elements, it does. In researching
> it, I came across these two links that discuss it:
>
> http://forums.netbeans.org/post-68036.html
>
> http://blogs.sun.com/enterprisetechtips/entry/configuring_json_for_restful_web
>
> Is there any solution other than what is described in the above links?
> I currently have over 50 JAXB beans (they are actually generated from
> an XSD schema, but same difference). It seems sort of painful to have
> to go through them all and figure out which fields I have to specify
> as parameters to JSONConfiguration.mapped().arrays().
>
> In addition what happens if I have the same field name used more than
> once, once where I want it to be an array, and once where I don't?
> Here is one of my generated JAXB beans:
>
> @XmlType(name = "BillToLocationsType", propOrder = {
> "billToLocation"
> })
> public class BillToLocationsType {
>
> protected List<LocationType> billToLocation;
>
> I guess I should name that field (or element, in the XSD)
> billToLocations. But since I don't, what happens if some other JAXBean
> with "protected LocationType billToLocation"? And then I have
> JSONConfiguration.mapped().arrays("billToLocation")?
>
> BTW, the reason I have it this way is that my generated xml does come
> out how I want it:
>
>
> <billToLocations><billToLocation>...</billToLocation><billToLocation>...</billToLocation></billToLocations>
>
>
> I'll look into the naming some more.
>
> I sort of digressed. The thing I most want to know is if there is
> another, easier way to have a single element JSON array generated,
> without having to manually look at every JAXB bean.
>
> Thanks,
>
> Charles
>