users@jersey.java.net

Re: [Jersey] Single Element Arrays and JSON

From: Charles Overbeck <coverbec_at_pacbell.net>
Date: Tue, 12 Oct 2010 09:58:48 -0700 (PDT)

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