RuntimeTypeAdapterFactory表示未定义“类型”

时间:2016-02-02 19:16:14

标签: java android gson

我正在处理我需要反序列化的多态类的情况。

Class Pen{
  String name;
  List<Animal> animals;
}

//Animal can be an interface or parent class: I am flexible

Class Animal{
  AnimalType type;//enum
  int legs;
}

enum AnimalType{
  dog,cat,pig,chicken;
}

Class AnimalDog extends Animal{
  //…
}

Class AnimalCat extends Animal{
  //…
}


Class AnimalPig extends Animal{
  //…
}

然后我用

创建我的Gson实例
public static Gson instanceUpperCamelCaseWithTypeAdapterFactory() {
    if (null == sGsonUpperCamelCase) {
        final RuntimeTypeAdapterFactory<Animal> typeFactory = RuntimeTypeAdapterFactory
                .of(Animal.class, “type")
                .registerSubtype(AnimalDog.class, “dog”)
                .registerSubtype(AnimalCat.class, “cat”)
                .registerSubtype(AnimalPig.class, “pig”);

        sGsonUpperCamelCase = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
                .registerTypeAdapterFactory(typeFactory).create();
    }//the naming policy is because server sends me upper case fields whereas Java fields are lowercase.
    return sGsonUpperCamelCase;
}

为了从包含动物清单的json中获取动物,我做

List<Animal> animals = gson.fromJson(json, new TypeToken<List<Animal>>() {}.getType());

我完全是Gson的新手。完全。所以不要太困惑我,我怎么能解决这个问题呢?

错误追踪:

com.google.gson.JsonParseException: cannot deserialize class com.company.appname.data.model.Animal because it does not define a field named type
com.company.appname.utils.RuntimeTypeAdapterFactory$1.read(RuntimeTypeAdapterFactory.java:204)
com.google.gson.TypeAdapter$1.read(TypeAdapter.java:199)
com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:117)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:217)
com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
com.google.gson.Gson.fromJson(Gson.java:861)
com.google.gson.Gson.fromJson(Gson.java:826)
com.google.gson.Gson.fromJson(Gson.java:775)

我通过在线验证器运行json,没有问题。它有很多项目。我在这里展示两个。

{“Animals”:[{  
           “id":9,
           “type”:”dog”,
           “name”:”maximus”
        },
        {  
           “id":10,
           “type”:”cat”,
           “name”:”meowy”,
           “yarns”:5,
           “nice”:true
        }]}

2 个答案:

答案 0 :(得分:4)

RuntimeTypeAdapterFactory.class 行号 - &gt; 203

需要替换

JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName);

<强>与

JsonElement labelJsonElement = jsonElement.getAsJsonObject().get(typeFieldName);

答案 1 :(得分:-1)

我在RuntimeTypeAdapterFactory中发现了错误(第185行,方法create(),并解决了这个问题,需要替换

if (type.getRawType() != baseType)

if (null == type || !baseType.isAssignableFrom(type.getRawType()))

然后RuntimeTypeAdapterFactory按预期运行良好。

请参阅链接registered issue with suggested fix