users@jersey.java.net

Re: [Jersey] unmarshalling JSON empty list

From: NBW <emailnbw_at_gmail.com>
Date: Mon, 12 Jul 2010 11:30:58 -0400

This is a common complaint when using JAXB. To get around it we use a class
that wraps / extends ArrayList<T> and has a getElements method that returns
an Object[] then we use that to wrap the class what we want to serialize
into JSON. Then in our JAXBContextResolver we add the class to our
JSONJAXBContext and add the XmlRootElement name to an array that is set as
the JSONJAXBContext.JSON_ARRAYS property.

On Mon, Jul 12, 2010 at 11:11 AM, Matthew M. Boedicker <
matthewm_at_boedicker.org> wrote:

> When I send this JSON to my web service:
>
> {"input":[]}
>
> I get in my class a List<String> with one element that is an empty string.
> What I would like is a List<String> with 0 elements. Is there any way to
> make it unmarshal to that?
>
> This is the class that represents the JSON:
>
> @XmlRootElement
> public class ListTest {
>
> public void setInput(List<String> val) {
> this.input = val;
> }
> public List<String> getInput() {
> return this.input;
> }
>
> private List<String> input = new ArrayList<String>();
> }
>
>