users@jersey.java.net

[Jersey] Re: Jaxb problem with lists.

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Tue, 29 Mar 2011 13:01:50 -0700

Hi Tarjei,

please try to switch to using the JSON POJO mapping feature.
See [1] for config details.

HTH,

~Jakub

[1]http://jersey.java.net/nonav/documentation/latest/json.html#d4e894

On 03/29/2011 12:47 PM, tarjei wrote:
> Hi, I got a class, something like:
>
> @XmlRootElement(name="myClass")
> class MyClass {
> public List<Article> related = new ArrayList<Article>();
> }
>
> @XmlRootElement(name="Article")
> class Article {
> public String title;
> }
>
> now, if I try to use JAXB to serialize this object, I get :
>
> {myClass : { related: { 'title' : "some title" }}
>
> If the related field contains one article and
>
> {myClass : { related: [{ 'title' : "some title" }, { 'title' : "some
> title" }]}
>
> if there are two or more. Notice the [] around the related items.
>
> Now, this is more than a bit annoying - it is very inconsistent and IMHO
> illogical - you cannot depend on the representation being close to
> correct across various datasets. Also it becomes a big problem when
> dealing with languages like PHP that do not differ between a Hash and a
> List which makes it very hard to detect the difference and handle it.
>
> So, what should I do?
>
> Regards,
> Tarjei
>