Search code examples
javajsonweb-servicesjacksonpojo

Error in JSON list deserialize


I'm developing a GWT web application with a RESTful web service. The web service's results are deserializing to POJO with Jackson 1.8. It's work fine with simple fields. However, it fails when it try to deserialize a list of POJO. This is the POJO with list to deserialize:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class DatosIndicadoresSVclaveDTO implements Serializable {
    ...
    @XmlAttribute
    @JsonDeserialize(contentAs = IdeologicoVOXDTO.class)
    public List<IdeologicoVOXDTO> ideologicoVox;
    ...

    //getter/setters
}

And this is the POJO that contains the list

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class IdeologicoVOXDTO implements Serializable {
    @XmlAttribute
    private Integer numeroPalabra;
    @XmlAttribute
    private String palabra;
    @XmlAttribute
    private Integer categoria;
    ...

    //getter/setters
}

The JSON have this structure:

{datosIndicadoresSVclave: {
        ...
        "ideologicoVox":[
            {
                "categoria":"1", 
                "numeroPalabra":"1", 
                "palabra":"abandonado", 
                ...
            },
            {
                "categoria":"2", 
                "numeroPalabra":"3", 
                "palabra":"hambre", 
                ...
            }
        ],
        ...
    }
}

When it's running, the web service's results works fine, but the deserialize print this error:

SEVERE: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: java.io.StringReader@10b61ad; line: 1, column: 580] (through reference chain: org.ull.etsii.client.dto.DatosIndicadoresSVclaveDTO["ideologicoVox"]) at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163) at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:219) at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:212)

Any idea?? Thanks!!


Solution

  • I've solved!!

    The problem is the size list is variable, and it fails if it has one element. The Jackson's version is 1.7, that it can't accept arrays single value. My solution is GSON with a custom register type, and I've used the Joshi's adviced. It works great!! Thanks!!