users@jersey.java.net

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

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 13 Nov 2008 10:14:17 +0100

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.


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

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
>