Gson LinkedTreeMap

Problem

Given a Gson object with format LinkedTreeMap<String, String>, e.g.:

1
2
3
4
5
6
LinkedTreeMap<String, String> misc = {
misc1: misc_site,
misc2: misc_sector,
misc3: null,
misc4: null
}

I got the HashMap with

1
2
3
4
5
{
misc: null
}
and
{}

And pass case assertThat(misc).isNull(); is what I expected. Unfortunately both were deleted not Null.
I try to transform the type into HashTable but got:

1
ERROR: com.google.gson.internal.LinkedTreeMap cannot be cast to java.util.Hashtable

Solution

Construct an Iterator to get each value in map in sequence (LinkedTreeMap has already did it).

1
2
3
4
5
6
7
Iterator it = misc.keySet().iterator();
Integer cnt = 0;
while (it.hasNext()) {
String key = (String) it.next();
if (misc.get(key) != null) cnt++;
}
assertThat(cnt).isEqualTo(0);

不怎么好看不过能用。。

Ref: Gson解析时对于不确定泛型的处理