In GSON to get a list of objects you do
Gson gson = new Gson();
Type token = new TypeToken>(){}.getType();
return gson.fromJson(json, token);
It works great, but I want to go further and have MyType parametrized so I can have a common function to parse list of objects with this code
// the common function
public List fromJSonList(String json, Class type) {
Gson gson = new Gson();
Type collectionType = new TypeToken>(){}.getType();
return gson.fromJson(json, collectionType);
}
// the call
List myTypes = parser.fromJSonList(jsonString, MyType.class);
Sadly returns an array of StringMaps, not the type. T is being interpreted as another generic type, not my type. Any workaround ?
解决方案
Generics work at compile-time. The reason super-type tokens work, is because (anonymous) inner classes can access the type arguments to their generic superclasses (superinterfaces), which in turn are stored directly in the bytecode metadata.
Once your .java source file is compiled, the type parameter is obviously thrown away. Since it is not known at compile time, it cannot be stored in bytecode, so it's erased and Gson can't read it.
UPDATE
After newacct's answer, I tried to implement what he suggested in his option 2, ie implementing a ParameterizedType. The code looks like this (here is a basic test):
class ListOfSomething implements ParameterizedType {
private Class> wrapped;
public ListOfSomething(Class wrapped) {
this.wrapped = wrapped;
}
public Type[] getActualTypeArguments() {
return new Type[] {wrapped};
}
public Type getRawType() {
return List.class;
}
public Type getOwnerType() {
return null;
}
}
the purpose of this code, is to be used inside getFromJsonList():
public List fromJsonList(String json, Class klass) {
Gson gson = new Gson();
return gson.fromJson(json, new ListOfSomething(klass));
}
Even if the technique works and is indeed very clever (I didn't know it and I would have never thinked of it), this is the final accomplishment:
List list = new Factory()
.getFromJsonList(text, Integer.class)
instead of
List list = new Gson().fromJson(text,
new TypeToken>(){}.getType());
To me, all this wrapping in useless, even if I agree that TypeTokens make the code look nasty :P