GSON not de-serializing properly nested map with generic key/value

Viewed 130

I'm using GSON to deserialise a JSON string to a JAVA object containing a nested Map with generic key/value.
From my debugging I see GSON converts the first generic type to the correct object. But the second is not converted and therefore acts as a string.

class A {
    public B<C_Enum> b = new B<C_Enum>();
}
class B<T> {
    private Map<T, T> map = new HashMap<T, T>();
}
enum C_Enum {
    VAL1, VAL2;
}
main() {
    String json = "{\"b\": {\"map\": {\"NOT_VALID\": \"NOT_VALID\"}}}";
    GsonBuilder builder = new GsonBuilder();
    Gson customDeserializer = builder.create();
    A a = customDeserializer.fromJson(json, A.class);
}

In this example the map is populated with a null key because NOT_VALID is not a valid C_Enum value, and the value of the map is populated with NOT_VALID.
Note: my real code is a bit different but the problem is the same

0 Answers
Related