gpt4 book ai didi

java - RuntimeTypeAdapterFactory 说 “type” 未定义

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:21:26 27 4
gpt4 key购买 nike

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

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)

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

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

最佳答案

RuntimeTypeAdapterFactory.class行号 -> 203

需要更换

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

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

关于java - RuntimeTypeAdapterFactory 说 “type” 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35161991/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com