Deserialzing works in gson-2.1 but not gson-2.4.4

I have this snippet:

HashMap<String, List<ClassOne>> mappingMap = new HashMap<String, List<ClassOne>>();
String key = "pop";
ClassOne c1 = new ClassOne();
c1.setCategoryName("categoryName");
ArrayList<ClassOne> al = new ArrayList<ClassOne>();
al.add(c1);
mappingMap.put(key, al);
Gson gson = new Gson();
String data = gson.toJson(mappingMap);
HashMap<String, List<ClassOne>> dmm = gson.fromJson(data, new TypeToken<Map<String, List<ClassOne>>>() {}.getType());

It when run with the gson-2.1.jar , it works fine. When run with the gson-2.2.4.jar I get the following exception

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to java.util.HashMap at com......

Wondering if anyone could explain, or point out what I am doing incorrectly.

Answers


I don't know about 2.1, but if it's true that it worked, then Gson must have changed their default Map implementation to use when deserializing Map types from HashMap to LinkedTreeMap in gson-2.2.4.

If you want to continue using HashMap, specify it in the TypeToken.

new TypeToken<HashMap<String, List<ClassOne>>>()

Need Your Help

Introducing delay while ajax call in select2 plugin

javascript jquery delay jquery-select2

I am using select 2 example from http://ivaynberg.github.io/select2/

How do you test dependency interaction

c# dependency-injection mocking tdd

I have a class that manipulates dependencies based on other dependency results: