我正在处理一种需要反序列化的多态类的情况。
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
}]}发布于 2018-03-20 19:30:50
RuntimeTypeAdapterFactory.class行号-> 203
需要取代
JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName);使用的
JsonElement labelJsonElement = jsonElement.getAsJsonObject().get(typeFieldName);发布于 2016-02-13 02:16:07
我在RuntimeTypeAdapterFactory中发现了错误(第185行,方法create() ),并解决了这个问题,需要替换
if (type.getRawType() != baseType)使用
if (null == type || !baseType.isAssignableFrom(type.getRawType()))那么RuntimeTypeAdapterFactory就像预期的那样工作得很好。
https://stackoverflow.com/questions/35161991
复制相似问题