users@jersey.java.net

Single Element Arrays and JSON

From: Charles Overbeck <coverbec_at_pacbell.net>
Date: Tue, 14 Sep 2010 14:00:45 -0700 (PDT)

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