users@jersey.java.net

Re: [Jersey] JSON (possible JAXB?) strangeness

From: Zoltan Arnold NAGY <Zoltan.Nagy_at_Sun.COM>
Date: Thu, 08 Oct 2009 15:08:30 +0200

You're right. How stupid of me. :) Should have looked at the code more
deeply instead
of copy-pasting it from the blog.

Now I get the following jsons:
[{"id":10,"blist":[{"value":0},{"value":0}]},{"id":11,"blist":[{"value":0},{"value":1}]}]
[{"value":0},{"value":1}]

which I assume are correct.

Thanks for the help :)
Zoltan

Paul Sandoz wrote:
> Hi Zoltan,
>
> Your problem is here:
>
>#
> public JAXBContext getContext(Class<?> objectType) {
>#
> return (types[0].equals(objectType)) ? context : null;
>#
> }
>
> You are only using the JSONJAXBContext for the first element in the
> types array, thus the second item in the array never uses that context
> when serialized out as an representation.
>
> Paul.
>
> On Oct 8, 2009, at 2:46 PM, Zoltan Arnold NAGY wrote:
>
>> Hey all,
>>
>> please see the code here: http://nagyz.pastebin.com/m323e406b
>>
>> I'm running it with latest JAXB (2.1.12), and latest Jersey (1.1.2-ea).
>> The JAXBContextResolver code comes from Japod's blog
>> (http://blogs.sun.com/japod/entry/configuring_json_for_restful_web)
>>
>> now, if I fetch http://localhost:4343, I get:
>> {"a":[{"id":"10","blist":[{"value":"0"},{"value":"0"}]},{"id":"11","blist":[{"value":"0"},{"value":"1"}]}]}
>>
>> and if I fetch http://localhost:4343/1, I get:
>> [{"value":0},{"value":1}]
>>
>> and then here's the catch: if we reverse the classes' order in the
>> List (so declaring it like
>> private Class[] types = { A.class, B.class }), we do get a very
>> different JSON back, namely:
>>
>> http://localhost:4343 will give back:
>> [{"id":10,"blist":[{"value":0},{"value":0}]},{"id":11,"blist":[{"value":0},{"value":1}]}]
>>
>> and http://localhost:4343/1 will give back:
>> {"b":[{"value":"0"},{"value":"1"}]}
>>
>> Is this to be expected?
>> If it is, why?
>>
>> I just spend half an hour hunting this behaviour.. :)
>>
>> Thanks,
>> Zoltan
>>
>>
>>
>> ---------------------------------------------------------------------
>> 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>
>>
>