-1
class A<T extends Animal>{
  @XmlElement
  T animal;
}

@XmlRootElement(name="animal")
class Animal{
}

//@XmlRootElement(name="Birds")
class Birds extends Animal{
 ArrayList<String> someNames;
relavant fields ..with getter/setter and annotation
}

//@XmlRootElement(name="Fish")
class Fish extends Animal{
relavant fields ..with getter/setter and annotation
}

I could convert Bean to Json String using org.codehaus.jackson . But when I try to convert Json String back to Java Bean using org.codehaus.jackson

JsonFactory jf = new JsonFactory(); 
JsonParser jp = null;

A<Bird> bird = null;
bird = inputMapper.readValue(jp, A.class);

I gets

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "someNames" .

I have put @XmlElement annotations on getter and setters.

Free Coder
  • 419
  • 4
  • 17

1 Answers1

0

Found the solution http://programmerbruce.blogspot.in/2011/05/deserialize-json-with-jackson-into.html

See example 4 on the mentioned link. By putting 2 Annotations @JsonTypeInfo and @JsonSubTypes, the problem is solved.

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        include = JsonTypeInfo.As.PROPERTY,
        property = "type")
@JsonSubTypes({
        @JsonSubTypes.Type(value = Birds.class, name = "birds"),
        @JsonSubTypes.Type(value = Fish.class, name = "fish") })
@XmlRootElement(name="animal")
class Animal{
}

Note the "type" parameter in the annotation @JsonTypeInfo.

1) While serializing, "type" param is introduced by the Jackson framework.

In the case of Birds instance "type" is introduced as "animal":{"type":"birds", "":"" ....}

Similarly in the case of Fish "type" is introduced as "animal":{"type":"fish", "":"" ....}

2) While Deserializing, "type" parameter is used by the Jackson Framework to instantiate polymorphic generic instance bean at run time.

Free Coder
  • 419
  • 4
  • 17