users@jersey.java.net

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

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 12 Nov 2008 22:22:57 +0100

Hi Jeremy,

Just curious if you have only one person item in your array.

By default the JSON support always strips out the root element, so it
is never encoded in JSON.

So when you see "person" in JSON it corresponds to the "person" JAXB
object.

Below is the code for writing out a collection:

     public final void writeList(Class<?> elementType, Collection<?>
t, MediaType mediaType, Charset c, Marshaller m, OutputStream
entityStream) throws JAXBException, IOException {
         final OutputStreamWriter osw = new
OutputStreamWriter(entityStream, c);
         // TODO: should reuse customization options from the
marshaller (if it is JSONMarshaller)
         // TODO: should force the elementType being treated as array
(for 1-elem lists)
         final XMLStreamWriter jxsw =
JsonXmlStreamWriter.createWriter(osw, true);
         try {
             jxsw.writeStartElement(getRootElementName(elementType));
             for (Object o : t) {
                     m.marshal(o, jxsw);
             }
             jxsw.writeEndElement();
             jxsw.writeEndDocument();
             jxsw.flush();
         } catch (XMLStreamException ex) {
              
Logger
.getLogger(JSONListElementProvider.class.getName()).log(Level.SEVERE,
null, ex);
             throw new JAXBException(ex.getMessage(), ex);
         }
     }

but the line:

             jxsw.writeStartElement(getRootElementName(elementType));

has no effect because the following line:

         final XMLStreamWriter jxsw =
JsonXmlStreamWriter.createWriter(osw, true);

as "true" as the second parameter, which means to strip out the root
element.

The fix would be as required in the "TODO" so that options can be
passed to not strip the root element.

Paul.

On Nov 12, 2008, at 9:46 PM, Jakub Podlesak wrote:

>
> Hi Jeremy,
>
> On Wed, Nov 12, 2008 at 10:45:05AM -0800, Jeremy Whitlock wrote:
>> Hi All,
>> I've been trying to figure out a good way to return both JSON
>> and XML for my resources. Based on the examples, it's easy but I
>> have a
>> little nit that I'd like to solve. Whenever my resource returns a
>> collection, List<Person> for example, the roots for the XML/JSON is
>> never what I want it to be. For XML, I end up with "People" as the
>> root
>> and with JSON, I get "person". As you can see, XML automatically
>> capitalizes the root node and with JSON, the "root" isn't pluralized.
>> I'm using the usual JAXB annotations and using jersey-json to do the
>> JAXB->JSON stuff but I've done anything custom. So...here are my two
>> questions:
>>
>> 1) Can Jersey be made to handle it properly so that both XML and JSON
>> end up with the same root? (So instead of "People" and "person", I
>> get
>> "people"?)
>
> In this case i would like to see Jersey generating JSON array
> instead of JSON object for java collections. Then the root element
> would not
> exist. Would this approach work for you?
>
>
>> 2) If not, what options do I have to get this more like I want it?
>
> I will need to have a more detailed look into this case.
> (Attending a conference and do not have much time for it this week).
> Please feel free to file a RFE at [1], so that i can keep track on it.
>
>>
>> I don't mind having to do the XML and JSON myself using utility
>> classes
>> or something but with Jersey providing so much, I figured I'd get
>> some
>> advice before writing more generic serializers.
>
>
> I will be happy to update current JSON functionality to better suit
> your needs,
> so there is no need for you to add special utilities to your
> application for this.
>
> ~Jakub
>
>
>
>>
>> Take care,
>>
>> Jeremy Whitlock | Software Engineer | CollabNet, Inc.
>> 8000 Marina Blvd. Suite 600 | Brisbane, CA 94005 | USA
>> O 650.228.2516 | C 970.988.8822 | jwhitlock_at_collab.net
>>
>>
>> ---------------------------------------------------------------------
>> 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
>