Gson 实体类解析问题

Gson 实体类解析问题,第1张

Gson 实体类解析问题 混淆规则(解决Relase包无法正常解析)
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
-keep class sun.misc.Unsafe { *; }
//这里很重要不可忽略
-keep class com.google.gson.stream.** { *; }

#实体类跳过混淆
# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }
//这里是自定义的实体类
-keep class com.xxx.bean.** { *; }
-keep class com.xxx.bean.*
Type (解决 gson cannot be cast to xxx 问题)
//两种方法获取Type
1 如果是确定的实体类型就用这种
Type type = new TypeToken() {}.getType();
Gson().fromJson(data, type)

2 如果是带有泛型类型的解析就用这种
Type type = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
Gson().fromJson(data, type)
如果还有问题 那么尝试将Kotlin data类转为Java类

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5685034.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存