users@jersey.java.net

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

From: Jakub Podlesak <Jakub.Podlesak_at_Sun.COM>
Date: Thu, 13 Nov 2008 15:08:30 -0800

On Thu, Nov 13, 2008 at 10:14:17AM +0100, Paul Sandoz wrote:
>
> On Nov 12, 2008, at 11:15 PM, Jeremy Whitlock wrote:
>
>> For posterity, below is some example output:
>>
>> XML Output
>> ----------
>> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
>> <People>
>> <person>
>> <age>27</age>
>> <firstName>Jeremy</firstName>
>> <lastName>Whitlock</lastName>
>> </person>
>> .....
>>
>> JSON Output
>> -----------
>> {"person":
>> [{
>> "age":"27",
>> "firstName":"Jeremy",
>> "lastName":"Whitlock"
>> },
>> .....
>>
>> And just to summarize what is going on that seems out of place:
>>
>> 1) In XML, Jersey assumes that the root node should be capitalized.
>
> Yes, i think that is a bug, need to investigate.

Fixed in the main trunk. Jersey in fact was taking the name
from the appropriate type and was not capitalizing it at all.

>
>
>>
>> 2) In JSON, there is no "name" for each person object in the array.

Do you mean something like:

[{"person":{"age":"27",...}}, {"person":{"age":...}}]

?

To me it seems a bit redundant, but i agree people should be able
to get it (but not by default)

>> 3) In JSON, the root is not plural.

So that you will end up with:

{"people":[{"person":{"age":"27",...}}, {"person":{"age":...}}]}

right?

I agree, people should be able to configure it.

~Jakub
>>
>
> Some explanation of JSON grammar might help in this respect [1].
>
> For 2 and 3 it is because the root element is not being encoded as a JSON
> object named "Person" (it is always stripped, which is a bug, as you say it
> needs to be configurable), and the the array of JAXB person objects is
> encoded as a JSON object named "person" with a value that is a JSON array
> where each value corresponds to a person, so we attempt to utilize more
> effectively the JSON grammar of arrays so that the JSON is easier to
> consume.
>
> If the root element was not stripped then IIRC you get the following, but
> Jakub would know precisely:
>
> {"People" :
> {"person":
> [{
> "age":"27",
> "firstName":"Jeremy",
> "lastName":"Whitlock"
> } ,
> ...
> ]
> }
> }
>
>
>
>> And finally, what I'm proposing is a configurable way to handle these
>> situations.
>
> Agreed. You might also be interested in a blog here [2] on configuration. I
> am wondering if the JSON mapped configuration suits your needs more. From
> the feedback we have got most developers don't appear to like the mapped
> convention and prefer a more concise convention that is easier to consume
> by the JavaScript client.
>
> Paul.
>
> [1] http://www.json.org/
> [2] http://blogs.sun.com/enterprisetechtips/entry/configuring_json_for_restful_web
>
>> I don't have any implementation suggestions at this time
>> other than to propose some sort of runtime-based configuration like a
>> property or something.
>>
>
>
>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>