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 10:51:16 +0100

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
>