users@jersey.java.net

[Jersey] Re: NullPointerException in Jackson2StaxReader

From: Jeff Ramsdale <jeff.ramsdale_at_gmail.com>
Date: Sun, 05 Dec 2010 13:24:26 -0800

I had a chance to narrow down the problem with the Natural Builder. I'm
seeing it with an empty nested List, for instance: {"things":[]}

Note that unmarshalling this string works with the Mapped Builder.

Code below reproduces bug:

import java.io.StringReader;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.api.json.JSONJAXBContext;
import com.sun.jersey.json.impl.BaseJSONUnmarshaller;

public class JsonBug {

        @XmlRootElement
        @XmlAccessorType(XmlAccessType.FIELD)
        public static class Thing {
                public String id;
                public List<SubThing> things;
        }

        @XmlRootElement
        public static class SubThing {
                public String value;
        }

        public static void main(String[] args) throws JAXBException {
                final String json = "{\"things\":[]}";
                final JAXBContext jaxbContext = JSONJAXBContext.newInstance(Thing.class);
                System.out.println(json);
                final Thing thing = new BaseJSONUnmarshaller(jaxbContext,
JSONConfiguration.natural().build())
                        .unmarshalFromJSON(new StringReader(json), Thing.class);
                System.out.println(thing);
        }

}