相关疑难解决方法(0)

在使用GSON解析JSON时使用Enums

这与我之前在此问过的上一个问题有关

使用Gson进行JSON解析

我试图解析相同的JSON,但现在我已经改变了我的类.

{
    "lower": 20,
    "upper": 40,
    "delimiter": " ",
    "scope": ["${title}"]
}
Run Code Online (Sandbox Code Playgroud)

我的课现在看起来像:

public class TruncateElement {

   private int lower;
   private int upper;
   private String delimiter;
   private List<AttributeScope> scope;

   // getters and setters
}


public enum AttributeScope {

    TITLE("${title}"),
    DESCRIPTION("${description}"),

    private String scope;

    AttributeScope(String scope) {
        this.scope = scope;
    }

    public String getScope() {
        return this.scope;
    }
}
Run Code Online (Sandbox Code Playgroud)

此代码抛出异常,

com.google.gson.JsonParseException: The JsonDeserializer EnumTypeAdapter failed to deserialized json object "${title}" given the type class com.amazon.seo.attribute.template.parse.data.AttributeScope
at 
Run Code Online (Sandbox Code Playgroud)

异常是可以理解的,因为根据我之前的问题的解决方案,GSON期望实际上将Enum对象创建为

${title}("${title}"),
${description}("${description}"); …
Run Code Online (Sandbox Code Playgroud)

java json gson

102
推荐指数
5
解决办法
8万
查看次数

使用Proguard Obfuscation时,Gson EnumTypeAdapter中的AssertionError

我的项目在序列化/反序列化期间实现了一个TypeAdapterin Gson来保留对象的多态性状态.无论如何,该项目在开发测试期间工作正常,但是当它通过proguard混淆和测试发布时,它只会崩溃.

03-21 10:06:53.632: E/AndroidRuntime(12441): FATAL EXCEPTION: main
03-21 10:06:53.632: E/AndroidRuntime(12441): java.lang.AssertionError
03-21 10:06:53.632: E/AndroidRuntime(12441):    at com.google.gson.internal.bind.TypeAdapters$EnumTypeAdapter.<init>(SourceFile:724)
03-21 10:06:53.632: E/AndroidRuntime(12441):    at com.google.gson.internal.bind.TypeAdapters$26.create(SourceFile:753)
03-21 10:06:53.632: E/AndroidRuntime(12441):    at com.google.gson.Gson.getAdapter(SourceFile:353)
03-21 10:06:53.632: E/AndroidRuntime(12441):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(SourceFile:82)
03-21 10:06:53.632: E/AndroidRuntime(12441):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(SourceFile:81)
03-21 10:06:53.632: E/AndroidRuntime(12441):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(SourceFile:118)
03-21 10:06:53.632: E/AndroidRuntime(12441):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(SourceFile:72)
03-21 10:06:53.632: E/AndroidRuntime(12441):    at com.google.gson.Gson.getAdapter(SourceFile:353)
03-21 10:06:53.632: E/AndroidRuntime(12441):    at com.google.gson.Gson.toJson(SourceFile:578)
03-21 10:06:53.632: E/AndroidRuntime(12441):    at com.google.gson.Gson.toJsonTree(SourceFile:479)
03-21 10:06:53.632: E/AndroidRuntime(12441):    at com.google.gson.Gson.toJsonTree(SourceFile:458)
03-21 10:06:53.632: E/AndroidRuntime(12441):    at com.google.gson.Gson$3.serialize(SourceFile:137)
Run Code Online (Sandbox Code Playgroud)

我的Gson特定的proguard配置是:

##---------------Begin: proguard …
Run Code Online (Sandbox Code Playgroud)

android proguard gson

65
推荐指数
5
解决办法
1万
查看次数

标签 统计

gson ×2

android ×1

java ×1

json ×1

proguard ×1