Page 1 of 1

REST gives JSONObject output when it has only one entry

Posted: Tue May 07, 2013 9:12 pm
by meghraj27
I am using resttemplate with Gson to deserialize the json data.
I am using List<Object> wherever it can send JSONArray output.
But the problem is whenever there is single entry/item it gives output in the form of JSONObject.
I such cases my deserializer gives exception as "Expected BEGIN_ARRAY but was BEGIN_OBJECT".
Please tell me how to solve this problem. Is there any good rest client library to use with android which can handle such kind of JSON data.

JSONArray example (for more than one item)
----------------------
{
"product":
[
{
"id": "11",
"name": "Bull Snort Cowboy Cayenne Pepper Hot Sauce",
"activeStartDate": "2013-05-07T14:09:39.197+05:30",
"manufacturer": "Brazos Legends",
"defaultSku":
{
"id": "11",
"activeStartDate": "2013-05-07T14:09:39.197+05:30",
"name": "Bull Snort Cowboy Cayenne Pepper Hot Sauce",
"retailPrice":
{
"amount": "3.99",
"currency": "USD"
}
},
"productOptions": null
},
{
"id": "12",
"name": "Cafe Louisiane Sweet Cajun Blackening Sauce",
"activeStartDate": "2013-05-07T14:09:39.198+05:30",
"manufacturer": "Garden Row",
"defaultSku":
{
"id": "12",
"activeStartDate": "2013-05-07T14:09:39.198+05:30",
"name": "Cafe Louisiane Sweet Cajun Blackening Sauce",
"retailPrice":
{
"amount": "4.99",
"currency": "USD"
}
},
"productOptions": null
}
]
}




JSONObject output (for single item)
----------------------
{
"product":
{
"id": "11",
"name": "Bull Snort Cowboy Cayenne Pepper Hot Sauce",
"activeStartDate": "2013-05-07T14:09:39.197+05:30",
"manufacturer": "Brazos Legends",
"defaultSku":
{
"id": "11",
"activeStartDate": "2013-05-07T14:09:39.197+05:30",
"name": "Bull Snort Cowboy Cayenne Pepper Hot Sauce",
"retailPrice":
{
"amount": "3.99",
"currency": "USD"
}
},
"productOptions": null
}
}

//note:- here square brackets removed for single item

Re: REST gives JSONObject output when it has only one entry

Posted: Wed May 08, 2013 8:41 am
by phillipuniverse
Hm interesting issue, thanks for reporting. Gson with RestTemplate is what I would recommend on Android, so I think your technologies are solid.

Arguably, our REST API should be changed such that it will always return an array. I would think that this happens by default, because the API returns a List<ProductWrapper> (it looks like that's the one you're using). From CatalogEndpoint:

Code: Select all

public List<ProductWrapperfindProductsByName(HttpServletRequest request,
            
String name,
            
int limit,
            
int offset) {
     ...
}
 


I'm afraid that there might not be much we can do about the serialization as that might be something deep down in Jackson (which is what we're using to serialize/deserialize). However, one of our team members is currently refactoring some of our REST APIs so I'll have him take a look at your issue.

What version of Broadleaf are you on?

As a workaround, it looks like someone posted a solution in the form of a custom Gson TypeAdapter: http://sachinpatil.com/blog/2012/07/03/gson/ so that will probably be your best workaround for the time being. We'll update this post when we get a bit more information.

Thanks for the report!

Re: REST gives JSONObject output when it has only one entry

Posted: Tue May 14, 2013 11:21 am
by ktisdell
You might try adding this to your Jersey servlet configuration:

Code: Select all

<init-param>
     <param-name>com.sun.jersey.config.property.packages</param-name>
     <param-value>org.codehaus.jackson.jaxrs</param-value>
</init-param>


I haven't tested this thoroughly, but an initial test proves that this returns an array of 1 object. You should already have the correct jar(s) on your classpath.