-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
java.lang.AssertionError: illegal type variable reference #1343
Comments
It doesn't look like a bug. |
@lyubomyr-shaydariv maybe some other config cause this error. i config build.gradle
the problem solve. But when config |
I had the same problem |
@kkmike999 Toggled public static <T> List<T> jsonToList(@NonNull String jsonStr) {
try {
List<T> objList = null;
if (gson != null) {
Type type = new TypeToken<List<T>>() {}.getType();
objList = gson.fromJson(jsonStr, type);
}
return objList;
}catch (Exception e){
Log.i("json",e.getMessage());
return null;
}
} We tried below code to fix it by specified a type. public static <T> List<T> jsonToList(@NonNull String json, Class<T> cls) {
try {
Gson gson = new Gson();
List<T> list = new ArrayList<T>();
JsonArray array = new JsonParser().parse(json).getAsJsonArray();
for(final JsonElement elem : array){
list.add(gson.fromJson(elem, cls));
}
return list;
}catch (Exception e){
Log.i("json",e.getMessage());
return null;
}
} Maybe it was compiled issue, hope it help. Just using Gson in Retrofit converter:
|
@rosuH public static <T> List<T> fromJsonToList(final String json, final Class<T> elementClass) {
return gson.fromJson(json, TypeToken.getParameterized(List.class, elementClass).getType());
} that will do the job. I'd only recommend this method if you provide the element types dynamically, not statically (also note that private static final TypeToken<List<Short>> listOfShorts = new TypeToken<List<Short>>() {};
private static final TypeToken<List<Float>> listOfFloats = new TypeToken<List<Float>>() {};
public static void main(final String... args) {
final String json = "[1,2,3,4,5,6]";
System.out.println(gson.<List<Short>>fromJson(json, listOfShorts.getType()));
System.out.println(gson.<List<Float>>fromJson(json, listOfFloats.getType()));
} that would produce |
Could you please test if you still experience this |
i use
gson 2.8.5
in android project,run app with proguard on real Android Phone. when execute codeType listType = new TypeToken<List<T>>() {}.getType()
,then crash:i have add these code in
proguard.pro
:The text was updated successfully, but these errors were encountered: