1

I have the following 3 classes :

public class BaseClass<T>{
   private List<T> someList;
   ... constructor.....
}

public class Class1 extends BaseClass<BasePerson>{ .. constructor.. } 

public class Class2 extends BaseClass<Person> { ... constructor... }

When deserializing one of thew classes (Class1 or Class2) I get the following error:

java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.somepackage.BasePerson

I saw some answers regarding this error, all advising using TypeReference or TypeFactory here: answer and here : another answer

My problem is that i'm using jackson older version (1.9.2) and can't move to 2.0. Both TypeReference and TypeFactory were added in 2.0.0 API.

Anyone can advise what to do?

user1386966
  • 3,302
  • 13
  • 43
  • 72

1 Answers1

0

These classes are defined in Jackson 1.9. Pertinent docs TypeFactory and TypeReference

You have here the case of a non generic class Class1 extending a generic with a type BaseClass<BasePerson>. This is how I believe you would handle this with a TypeFactory

ObjectMapper mapper = new ObjectMapper();
TypeFactory typeFactory = mapper.getTypeFactory();
JavaType baseType = typeFactory.constructParametricType(BaseClass.class, BasePerson.class);
JavaType type = typeFactory.constructSpecializedType(baseType, Class1.class);

and deserialize like this:

Class1 class1 = mapper.readValue(json, type);
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82