Java – runtimetypeadapterfactory says “type” is undefined

I am dealing with the case where I need to deserialize a polymorphic class

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{
  //…
}

Then I create my gson instance

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;
}

To get the animals from the JSON that contains the animal list, I do this

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

I'm completely new to gson. So don't confuse me too much. How can I solve this problem?

Error tracking:

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)

I run JSON through the online validator, no problem. It has many projects. I show two here

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

resolvent:

Runtimetypeadapterfactory.class line number – > 203

Replacement required

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

with

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>