I have a POJO like this
class POJO {
Map<Integer, Integer> map;
//Getter and Setter for map here
}
And I have a json (list of POJO)
[
{
"200": 10
},
{
"20": 100,
"30": 400
}
]
During Jackson deserialization if I do
String s = ... //s has the above json
List<POJO> pojoList = mapper.readValue(s, new TypeReference<List<POJO>>() {});
System.out.println(pojoList.get(0).getMap().get(20)); //prints 100
then there is no problem
But if I use generic List like
List<POJO> pojoList = mapper.readValue(s, List.class);
then in the System.out.println
it throws ClassCastException
java.util.LinkedHashMap cannot be cast to mycode.model.POJO
I understand that if I just tell Jackson List
it deserializes each object as Map and not as type POJO. So when I try to access pojoList.get(0).getMap()
it throws exception
(Note: Printing pojoList.get(0)
gives no problem and prints {"200":10}
)
My question is why didn't it throw exception during deserialization itself. Did the type of object on LHS ignored?
Thanks..