users@jersey.java.net

Re: [Jersey] RE: array handling in JAXB to JSON conversion

From: Andrew Ochsner <aochsner_at_cs.stanford.edu>
Date: Tue, 10 Mar 2009 09:48:52 -0600

So, a little update because I'm running into the same issue, except I'm
using the natural mapping.

Various combinations of required=true/false and nillable = true/false have
not helped. In fact, when getting as XML, the <items/> element is not
getting written out. So that's a JAXB thing.

So I'm trying the XmlElementWrapper, and it's close. The XML does spit out
<items/> which is good, but the JSON is { "items":[null] } which doesn't
seem to be the same as { "items":[] } which is what I want.

HTH
Andy O

On Mon, Mar 9, 2009 at 10:39 AM, Jakub Podlesak <Jakub.Podlesak_at_sun.com>wrote:

> On Mon, Mar 09, 2009 at 05:10:19PM +0100, Paul Sandoz wrote:
> > Hi Jakub,
> >
> > Do you mean that JAXB is not writing out any start element for "items"
> > because the value of the items field is null or empty?
>
> Hmmm, you are right, jaxb should generate at least <items/>, which should
> IIRC get
> translated into "items":null for the default mapped convention.
>
> Need to check this out, and then will report back here.
>
> ~Jakub
>
>
> >
> > There may be an annotation to force JAXB to always write out "items":
> >
> > 1) using XmlElement(name="items", nillable=true); or
> >
> > 1) using XmlElementWrapper, the JavaDoc states:
> >
> >
> http://java.sun.com/javase/6/docs/api/javax/xml/bind/annotation/XmlElementWrapper.html
> > The two serialized XML forms allow a null collection to be represented
> > either by absence or presence of an
> > element with a nillable attribute.
> >
> > or it may be possible to utilize XmlJavaTypeAdapter.
> >
> > Paul.
> >
> > On Mar 9, 2009, at 4:47 PM, Jakub Podlesak wrote:
> >
> >>
> >> Hi Moiz,
> >>
> >> You have hit an edge case, and i am afraid i have no good news for you.
> >> Jersey JSON provider takes what JAXB gives out, and the information
> >> about the emtpy array is obviously missing.
> >>
> >> One option would be to try out the provider mentioned by Tatu.
> >> Have you had a chance to test it yet?
> >>
> >> Another option would be to use the low-level JSONObect/JSONArray
> >> providers (used in bookmark example [1].
> >>
> >> ~Jakub
> >>
> >> [1]
> http://download.java.net/maven/2/com/sun/jersey/samples/bookmark/1.0.3-SNAPSHOT/bookmark-1.0.3-SNAPSHOT-project.zip
> >>
> >>
> >> On Fri, Mar 06, 2009 at 07:21:51AM -0800, Moiz Dohadwala wrote:
> >>> I have tried that too, but hasn't worked either.
> >>>
> >>> -Moiz
> >>>
> >>> From: Wilhelmsen Tor Iver [mailto:TorIverW_at_arrive.no]
> >>> Sent: Friday, March 06, 2009 5:01 AM
> >>> To: users_at_jersey.dev.java.net
> >>> Subject: Re: [Jersey] RE: array handling in JAXB to JSON conversion
> >>>
> >>> Try adding required=true to this:
> >>>
> >>> @XmlElement(name="items")
> >>> List<Item> items;
> >>>
> >>> or else tell JAXB to treat nulls and empty collections differently. We
> >>> ran into the same issue where null/empty elements were absent from the
> >>> XML and thus a JAXB client would generate classes missing these
> >>> properties.
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> >> For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >>
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> > For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>