Hello,

I have an abstract class, with a field which can be either a Simple Object 
(such as an Integer, a String, etc) or an Array.

Deserialization fails to handle the Array part, with the following stack:
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize 
instance of java.util.ArrayList out of START_OBJECT token
 at [Source: java.io.StringReader@4f209819; line: 1, column: 110] (through 
reference chain: my.package.DeserializationXmlTest$Pojo["field"]->my.package
.DeserializationXmlTest$ListValue["value"])


I have no issue deserializing Arrays when they are out of the Abstract 
class.

What am I missing ?

Thanks

Here is a test reproducing the issue (the deserializeXml_list_inAbstract test 
fails):  

package my.package;

public class DeserializationXmlTest {

 @Test
 public void deserializeXml_simple() throws IOException {
   ObjectMapper objectMapper = new XmlMapper();

   Pojo readValue = objectMapper.readValue("<root>" +
     "<name>MyName</name>" +
     "<field>" +
     " <value>MyValue</value>" +
     " <type>SIMPLE</type>" +
     "</field>" +
     "</root>", Pojo.class);

   assertThat(readValue.name).isEqualTo("MyName");
   assertThat(readValue.field).isInstanceOf(SimpleValue.class);
   assertThat(readValue.field.type).isEqualTo("SIMPLE");
   assertThat(readValue.field.value).isEqualTo("MyValue");
 }

 @Test
 public void deserializeXml_list_inAbstract() throws IOException {
   ObjectMapper objectMapper = new XmlMapper();

   Pojo readValue = objectMapper.readValue("<root>" +
     "<name>MyName</name>" +
     "<field>" +
     " <value>" +
     "   <item>Value1</item>" +
     "   <item>Value2</item>" +
     " </value>" +
     " <type>LIST</type>" +
     "</field>" +
     "</root>", Pojo.class);

   assertThat(readValue.name).isEqualTo("MyName");
   assertThat(readValue.field).isInstanceOf(ListValue.class);
   assertThat(readValue.field.type).isEqualTo("LIST");
   assertThat(readValue.field.value).isEqualTo(Arrays.asList("Value1", 
"Value2"));
 }

 @Test
 public void deserializeXml_list_no_abstract() throws IOException {
   ObjectMapper objectMapper = new XmlMapper();

   Pojo readValue = objectMapper.readValue("<root>" +
     "<name>MyName</name>" +
     "<listValue>" +
     " <value>" +
     "   <item>Value1</item>" +
     "   <item>Value2</item>" +
     " </value>" +
     "</listValue>" +
     "</root>", Pojo.class);

   assertThat(readValue.name).isEqualTo("MyName");
   assertThat(readValue.listValue).isInstanceOf(NoAbstractListValue.class);
   assertThat(readValue.listValue.value).isEqualTo(Arrays.asList("Value1", 
"Value2"));
 }

 @JacksonXmlRootElement(localName = "root")
 @JsonIgnoreProperties(ignoreUnknown = true)
 public static class Pojo {
   @JsonProperty
   private String name;
   @JsonProperty
   private AbstractValue field;
   @JsonProperty(required = false)
   private NoAbstractListValue listValue;
 }

 @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = 
JsonTypeInfo.As.EXISTING_PROPERTY, property = "type")
 @JsonSubTypes({
   @JsonSubTypes.Type(value = SimpleValue.class, name = "SIMPLE"),
   @JsonSubTypes.Type(value = ListValue.class, name = "LIST")
 })
 public static class AbstractValue<T> {
   protected String type;
   protected T value;
   public AbstractValue(String type) {
     this.type = type;
   }
 }

 public static class SimpleValue extends AbstractValue<String> {
   public SimpleValue() {
     super("SIMPLE");
   }

   @JsonProperty
   public void setValue(String value) {
     this.value = value;
   }
 }

 public static class ListValue extends AbstractValue<List<String>> {
   public ListValue() {
     super("LIST");
   }

   @JsonProperty
   @JacksonXmlElementWrapper(localName = "value")
   @JacksonXmlProperty(localName = "item")
   public void setValue(List<String> value) {
     this.value = value;
   }
 }

 public static class NoAbstractListValue {
   protected List<String> value;

   public NoAbstractListValue() {
   }

   @JsonProperty
   @JacksonXmlElementWrapper(localName = "value")
   @JacksonXmlProperty(localName = "item")
   public void setValue(List<String> value) {
     this.value = value;
   }
 }
}

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to