users@jersey.java.net

RE: [Jersey] Differences in json and xml responses

From: <james.lorenzen_at_accenture.com>
Date: Fri, 17 Sep 2010 11:47:53 -0400

@Charles
Well I cheated a little bit in my example, by just providing a pseudo example. But your right. I went back and hardcoded it to a single item in the list and the JSON did not return it as an array like you said. I thought I did notice yesterday in the mailing list when searching for json arrays, a user talking about setting some global jersey setting that lets you define which names are always arrays. So in this instance I would tell jersey that everytime they see "persons" to automatically make it an array. That is kind of a hack but would get you by until this was fixed.

Looking deeper on the mailing lists, looks like I may have found possible solutions for both problems. See https://jersey.dev.java.net/servlets/ReadMsg?list=users&msgNo=8514

Looks like jersey now supports a JSON Natural configuration: https://jersey.dev.java.net/documentation/1.1.3-ea/user-guide.html#d4e557. See section 3.1.2.2. Natural notation.
I'm going to give this a shot and see if it works.

________________________________________
From: Charles Overbeck [coverbec_at_pacbell.net]
Sent: Thursday, September 16, 2010 5:04 PM
To: users_at_jersey.dev.java.net
Subject: Re: [Jersey] Differences in json and xml responses

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<mailto:users-unsubscribe_at_jersey.dev.java.net>
For additional commands, e-mail: users-help_at_jersey.dev.java.net<mailto:users-help_at_jersey.dev.java.net>



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.