users@jersey.java.net

Re: Transform list of xml elements into JSONArray

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 25 Mar 2008 12:22:59 +0100

Hi Jakub,

A simple workaround, but would it be possible to choose not to
serialize the "item" name for arrays?

I suppose one could configure "please don't serialize the 'item'
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.

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
>