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 13:33:51 +0100

Hi Paul,

On Tue, Mar 25, 2008 at 12:22:59PM +0100, Paul Sandoz wrote:
> Hi Jakub,
>
> A simple workaround, but would it be possible to choose not to
> serialize the "item" name for arrays?

Then we would also need to get rid of the closing "{}",
i.e. to enable what Martin is looking for: serializing directly
(embedded) JSON arrays.
I think it would be somehow possible to implement, but not
as a simple/quick workaround. I need to think more about this.

>
> I suppose one could configure "please don't serialize the 'item'

We are already asking: please do not serialize the 'items' element.
Raw XML infoset to JSON mapping (Jettison mapped notation) would look like:
{"items":{"item":["val 1", "val 2", "val 3"]}}

> element just the children". JSON-based annotations would be ideal
> rather than a separate configuration. I guess the problem is that
> since we are currently operating at the level of the XML infoset we
> loose additional semantic information that JAXB knows about. So until/
> if we utilize more JAXB knowledge there are certain restrictions on
> the JSON format when using JAXB beans.

Exactly. I think the direction should be in finding a way how to plug
into JAXB so that we can

1) leverage the existing (or enriched -- see bellow) JAXB runtime model
   in JSON serialization/deserialization
2) enrich the JAXB model -- it would mean we had
   an extensible JAXB model (allowing us to introduce
   JSON/Jersey related annotations for things like the above
   mentioned array and have them processed by something
   like a Jersey/JAXB plugin)
Notice: 2 is not needed, may be it is not a good idea at all

I hope that we will meet and discus with Kohsuke at the Java One timeframe.

~Jakub

>
> Paul.
>
> On Mar 25, 2008, at 10:51 AM, 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
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>