users@jersey.java.net

Re: Transform list of xml elements into JSONArray

From: Jakub Podlesak <Jakub.Podlesak_at_Sun.COM>
Date: Tue, 25 Mar 2008 14:08:35 +0100

On Tue, Mar 25, 2008 at 01:41:08PM +0100, Martin Grotzke wrote:
> Hi Jakub,
>
> this already helps, I didn't know that I can provide two methods just
> with different @ProduceMime markers.

Great to hear. In meantime i will look at how to get rid
of the second method.

Thanks,

~Jakub

>
> Thanx && cheers,
> Martin
>
>
> On Tue, 2008-03-25 at 10:51 +0100, Jakub Podlesak wrote:
> > Hi Martin,
> >
> > i am afraid, the closest thing you could get for pure JAXB->JSON is:
> > {"item":["val 1","val 2","val 3"]}
> >
> > i.e. you will need two separate get methods for json and xml:
> >
> > @GET @ProduceMime("application/xml")
> > public Items getXmlList() {
> > return getItems();
> > }
> >
> > @GET @ProduceMime("application/json")
> > public JSONArray getJsonArray() {
> > return new JSONArray(getItems().items);
> > }
> >
> > You will then get:
> >
> > %curl -HAccept:application/json http://localhost:9998/test
> > ["val 1","val 2","val 3"]
> >
> > %curl -HAccept:application/xml http://localhost:9998/test
> > <?xml version="1.0" encoding="UTF-8" standalone="yes"?><items><item>val 1</item><item>val 2</item><item>val 3</item></items>%
> >
> > Hope it helps,
> >
> > ~Jakub
> >
> > On Mon, Mar 24, 2008 at 04:36:42AM +0100, Martin Grotzke wrote:
> > > Hi,
> > >
> > > what would be the best way to transform an Items root element containing
> > > a list of Item elements into a JSONArray?
> > >
> > > This is the (generated) Items bean:
> > >
> > > @XmlAccessorType(XmlAccessType.FIELD)
> > > @XmlType(name = "items", propOrder = {
> > > "items"
> > > })
> > > @XmlRootElement(name = "items")
> > > public class Items {
> > >
> > > @XmlElement(name = "item")
> > > protected List<String> items;
> > >
> > > ...
> > > }
> > >
> > > The expected json is s.th. like that:
> > >
> > > ["val 1", "val 2", "val 3"]
> > >
> > > This is desired to support a firefox suggestion search plugin ([1]).
> > >
> > > It would be nice if I could return an instance of items, so that both
> > > xml and json would be supported...
> > >
> > > Thanx for your help,
> > > cheers,
> > > Martin
> > >
> > >
> > > [1] http://developer.mozilla.org/en/docs/Supporting_search_suggestions_in_search_plugins
> > >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> > For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >