users@jersey.java.net

[Jersey] Re: Jaxb problem with lists.

From: tarjei <tarjei_at_nu.no>
Date: Wed, 30 Mar 2011 10:06:46 +0200

On 03/29/2011 10:01 PM, Jakub Podlesak wrote:
> Hi Tarjei,
>
> please try to switch to using the JSON POJO mapping feature.
> See [1] for config details.
\o/
Perfect!

There have been a lot of improvements in Jersey with regard to JSON
handling as well as other areas. You should consider posting a
"suggested defaults" document since the defaults cannot be changed due
to the JAX-RS spec.

Thanks for the quick reply.
T
>
> 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
>>
>