users@jersey.java.net

Re: [Jersey] Differences in json and xml responses

From: Charles Overbeck <coverbec_at_pacbell.net>
Date: Thu, 16 Sep 2010 15:04:10 -0700 (PDT)

This isn't an answer (sorry!), but I'm curious how you got single element to
come back as a JSON array:

{"person": ["name": "James"]}

I posted to this newsgroup a couple of days ago, subject "Single Element Arrays
and JSON" about this (no reply yet), and the above is not the default behavior.

I'm curious to see what replies you get, as it may impact the solution mentioned
in my post. I like the way I have it now, where the XML returns
<persons><person>..., but then the singular-named JSON bothers me, and it seems
like I might be more likely to have names that are arrays in some cases but not
in others, which makes the solution I cite in my earlier post a little more
problematic.

Charles






________________________________
From: "james.lorenzen_at_accenture.com" <james.lorenzen_at_accenture.com>
To: users_at_jersey.dev.java.net
Cc: james.lorenzen_at_accenture.com; ronald.alleva_at_accenture.com;
thomas.black_at_accenture.com; jeremy.thorn_at_accenture.com
Sent: Thu, September 16, 2010 1:18:43 PM
Subject: [Jersey] Differences in json and xml responses

We are writing some simple jersey services written in groovy that return basic
lists of objects and are getting different results between json and xml.

Here is our Person model

@XmlAccessorType( XmlAccessType.FIELD )
@XmlRootElement
class Person {
    String name
}

Here is our Service

@GET
@Produces(["application/json", "application/xml"])
public List<Person> getPersons() {
    return Manager.getPersons()
}

For XML this returns what we want:

<persons>
    <person>
        <name>James</name>
    </person>
</persons>

But for JSON the named array is singular which is not what we want:

{"person": ["name": "James"]}

So to get around the JSON response not having a plural named array we introduced
the Persons model object:

@XmlAccessorType( XmlAccessType.FIELD )
@XmlRootElement
class Persons {
    List<Person> persons
}

We update our Service to:

public Persons getPersons()

The result is now JSON has the correct plural named array, but the XML response
now has duplicate persons elements, which is not what we want.
<persons>
    <persons>
        <name>James</name>
    </persons>
</persons>

So it appears that we can't get consistent results for XML and JSON. Ideally I'd
love not to have to create a Persons object. In fact I think the easiest thing
would be to support an annotation on the Service that lets us define the root
element. Something like this:

@GET
@Produces(["application/json", "application/xml"])
@XmlRootElement(name="persons")
public List<Person> getPersons() {
    return Manager.getPersons()
}


This message is for the designated recipient only and may contain privileged,
proprietary, or otherwise private information. If you have received it in
error, please notify the sender immediately and delete the original. Any other
use of the email by you is prohibited.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
For additional commands, e-mail: users-help_at_jersey.dev.java.net