users@jersey.java.net

Re: [Jersey] Question on JAXB JSON Marshalling

From: Jakub Podlesak <Jakub.Podlesak_at_Sun.COM>
Date: Fri, 06 Feb 2009 01:31:29 +0100

Hi Farrukh,

please see in line...

On Thu, Feb 05, 2009 at 04:38:01PM -0500, Farrukh Najmi wrote:
> Farrukh Najmi wrote:
>>
>> I have a JAXB object as my response which in normal XML format looks like:
>>
>> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
>> <RegistryObjectList ...>
>> <RegistryObject ...>
>> ....
>> </RegistryObject>
>> </RegistryObjectList>
>>
>> When it is marshalled to JSON I get:
>>
>> {
>> "RegistryObject":{
>> ...
>> }
>> }
>>
>>
>> Question is why is it not like:
>>
>> {
>> "RegistryObjectList":{
>> "RegistryObject":{
>> }
>> }
>> }
>>
>> Why is the root element not being marshalled?
>>
>
> Hi Guys,
>
> Any help on above questions would be terrific.

Please see my previous message.

>
> I have a related question in understanding the mapping to the default JSON
> notation:
>
> Lets say the XML looks like the following where there are no child element
> of the root element:
>
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
> <RegistryObjectList ... />
>
> Why does above not map to an empty list istead of null?
>
> I expect the following empty set (given the earlier issue that root element
> is not mapped):
>
> {
> }
>
> Instead I get the following:
>
> null

I guess, you would like to see simply: []

Presuming, you have the following beans:

@XmlRootElement
public class RegistryObject { ...}

@XmlRootElement
public class RegistryObjectList {
  List<RegistryObject> registryObjects;
  ...
}

RegitryObjectList aList = ... // aList contains an empty registryObjects list


Then currently the only way to get [] is to use NATURAL JSON notation and:

@GET @Produces(MimeType.APPLICATION_JSON)
public List<RegistryObject> getList() {
  return aList.registryObjects;
}

instead of:

@GET @Produces(MimeType.APPLICATION_JSON)
public RegistryObjectList getList() {
  return aList;
}

HTH,

~Jakub
>
> Can someone please explain the mapping behavior in these edge cases. They
> are causing problems downstream because when the URL matches no
> RegistryObjects a null is returned instead of empty set and the AJAX
> toolkit I am using gets an error.
>
> Thanks for your help.
>
>
> --
> Regards,
> Farrukh
>
> Web: http://www.wellfleetsoftware.com
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>