users@jersey.java.net

Re: [Jersey] RE: Set the root when returning a collection

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 17 Nov 2008 11:26:34 +0100

On Nov 14, 2008, at 9:23 PM, Jeremy Whitlock wrote:

>> in such case, you will not end up with an array, i.e.
>>
>> <person><foo>bar</foo></person><person><foo>baz<</foo></person>
>>
>> gets translated into (omitting quotes):
>>
>> {person:[{foo:bar},{foo:baz}]}
>>
>> while
>>
>> <person><foo>bar</foo></person><animal><bee>cee</bee></animal>
>>
>> gets translated into:
>>
>> {person:{foo:bar},animal:{bee:cee}}
>
> Well, what I'm seeing is that if I had an array of people, the
> people do
> not have a "person" element. That's what I'm saying. So you'd have:
>
> {"people": [{foo:bar}, {foo:baz}]}
>

I disagree on this point (at least from a default option). The reason
is that the JSON object name corresponds to the name of the array
item, it is an array of "person" or List<Person>. A consumer of the
JSON does not have to understand any pluralization aspects and they
don't need to be documented.

The pluralization aspects were introduced mainly for the case of XML
when there is no root XML element available to utilize and one has to
be derived and it is likely that the root element is only important
from the perspective of generating a well-formed XML document, and
from a processing perspective is likely to be ignored.


> There is no "person" designation in the people array/list. That's
> fine
> when you have a generic list/array but if you have mixed list/array,
> you
> could run into a problem:
>
> {"objects": [{foo:bar}, {bee:cee}]}
>
> I guess since I've not actually created code for the last scenario, I
> might be worrying about a scenario that might not manifest the way
> I've
> described.

JAXB has some rules about inheritance so those would be follows in
terms of the base class that is utilized.

Paul.

> I'll work on creating an example of both, testing and if
> required, post to the list.
>


>> Agreed, it is not apparent how xml and json relate in this case.
>> On the other hand, json expression is much more simpler this way,
>> while containing all the info needed, and this (simplicity)
>> is what we wanted to provide.
>> I will make sure an option is available for you to confgure
>> json to get the format you requesting.
>
> Well, to me both support hierarchical data structures and I would
> expect
> them to map the same. As it appears now, they really do map the same
> but with JSON being a little less verbose when it comes to lists,
> there
> might be a problem with having a list of mixed object types if there
> is
> no way to look at a JSON array and tell what the object types are in
> the
> array. Like I mentioned above, let me try to create a working code
> example and I'll post back with my results. :)
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>