-
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
NPE while invoke toJson() or fromJson() for HashMap<String, String> #1778
Comments
Could this possibly be an Android bug ( Could you please debug into |
I also met this issue, but my code calls "toJson" on a HashMap<String, Object>. @Marcono1234 @zewenwang |
As written in my comment above it would probably first be useful to get more information about which variable is |
metoo, is there a fixed method? |
As written above, it would be useful if you could provide more information:
|
Sorry, here is the stacktrace.
And my code is: List<MyCustomClass> list = new ArrayList<>();
mGson.toJson(list); or List<MyCustomClass> list = new ArrayList<>();
mGson.toJson(list, TypeToken.getParameterized(List.class, MyCustomClass.class).getType()); |
it looks like a NULL generic declaration of class cause NPE. @Override
public int hashCode() {
return 31 * getName().hashCode() + getGenericDeclaration().hashCode();
} follow the trace, the private static Type resolve(Type context, Class<?> contextRawType, Type toResolve,
Collection<TypeVariable> visitedTypeVariables) {
// this implementation is made a little more complicated in an attempt to avoid object-creation
while (true) {
if (toResolve instanceof TypeVariable) {
TypeVariable<?> typeVariable = (TypeVariable<?>) toResolve;
if (visitedTypeVariables.contains(typeVariable)) {
// cannot reduce due to infinite recursion
return toResolve;
} else {
visitedTypeVariables.add(typeVariable);
}
toResolve = resolveTypeVariable(context, contextRawType, typeVariable);
if (toResolve == typeVariable) {
return toResolve;
}
}...... And the static Type getSupertype(Type context, Class<?> contextRawType, Class<?> supertype) {
if (context instanceof WildcardType) {
// wildcards are useless for resolving supertypes. As the upper bound has the same raw type, use it instead
context = ((WildcardType)context).getUpperBounds()[0];
}
checkArgument(supertype.isAssignableFrom(contextRawType));
return resolve(context, contextRawType,
$Gson$Types.getGenericSupertype(context, contextRawType, supertype));
} |
Are you able to reproduce this exception when debugging on an emulator, or only for the deployed app? Which Android API Level does the target device have? Are you using a custom ProGuard configuration? If possible could you please create a small example project which only contains the relevant code, which allows reproducing this issue (and describe reproduction steps if necessary). |
Thanks for your reply, we have solved this problem by wrapping a JsonObject on the JsonArray. And this is an online occasional problem which reproted by our user, thus we also don't have more information to identify the reason of the problem. |
It happened to me on version 2.8.2, the same stack trace, and I also wrote the code below:
to deserialize with a type token, I think this may be a clue. |
So your resolvent is to use another serialization tool? I noticed that you are in Beijing too, may I get your wechat or something to communicate with you? |
没,我就直接用jsonObject又包了层,原本是[{"xxx", "yyy"}],现在是 {"data": [{"xxx", "yyy"}]}就暂时解决了。原因就和上面Cyux07说的一样。 |
OK,了解了。但依然可以认为是Gson自己的bug吧?总感觉这样处理不能算是最终方案 |
In this pull request Gson users tried to solve this by modifying their ProGuard rules. Or in general make sure you apply the ProGuard rules shown in |
Hello, does anyone know if doing what @Marcono1234 suggested here the problem is solved? I am going through the same problem but updating the library version would affect many people, so I wanted to be sure before doing it. |
I just mentioned using the latest Gson version to avoid having to debug issues in older versions, which might already be fixed in the latest version. Though this appears to be caused by ProGuard (or R8) so I assume adjusting the ProGuard rules without upgrading Gson might work as well. |
visit the above link, It sames like you have List, Map or Set (ex: ArrayList, HashSet) member in your java bean class, and not all of them using the @SerializedName annotation, so In R8 3.x, generics get stripped out, refer to : https://android.googlesource.com/platform/libcore/+/refs/heads/main/luni/src/main/java/libcore/reflect/TypeVariableImpl.java com.google.gson.internal.$Gson$Types.getCollectionElementType(.java:302) take a look at getCollectionElementType in $Gson$Types, As the method name show you, It's all about Collections. |
the exception:
my code:
environment
Gson version 2.8.5
The probability of this problem happens substantial increases,when gson version upgrades from 2.8.2 to 2.8.5 !
Same problem with #1298 , but not resolved.
The text was updated successfully, but these errors were encountered: