简体   繁体   English

从 Google Cloud 端点返回多态对象

[英]Returning polymorphic objects from Google Cloud endpoints

I just tried to use polymorphism for entities for creating classes with hierarchy using objectify.我只是尝试对实体使用多态性,以使用 objectify 创建具有层次结构的类。 For example, I have a base Entity called Animal and have subclasses for this named Mammal and Reptile which is also entities.例如,我有一个名为Animal的基本实体,并有这个名为MammalReptile的子类,它们也是实体。
And when I try to store the data and retrieve it, it works fine and I am able to convert from base Animal class to Mammal or Reptile .当我尝试存储数据并检索它时,它工作正常,我能够从基础Animal类转换为MammalReptile

But when I try to return a list of animals in Google Cloud endpoints this inheritance is not preserved.但是,当我尝试在 Google Cloud 端点中返回动物列表时,不会保留此继承。 And I could not convert from Animal class to Mammal or Reptile on the client side.而且我无法在客户端从Animal类转换为MammalReptile

Actually Google Cloud endpoint is creating another model type which is actually returned as JSON in the endpoints for which is different from our entities and inheritance is not preserved in those model classes.实际上,Google Cloud 端点正在创建另一种模型类型,该模型类型实际上在端点中以 JSON 形式返回,这与我们的实体不同,并且在这些模型类中不保留继承。

Basically I need to query for all the animals and in client side based on the type of the object whether its Mammal or Reptile I will show the UI accordingly.基本上我需要根据对象的类型在客户端查询所有动物,无论是Mammal还是Reptile ,我将相应地显示 UI。

Here is a sample code.这是一个示例代码。

I have two classes Animal and Mammal .我有两个班AnimalMammal And Mammal is a subclass of Animal . MammalAnimal的子类。

Animal annie = new Animal();
annie.name = "Annnnn";
ofy().save().entity(annie).now();

Mammal mam = new Mammal();
mam.name = "Mammmaaaal";
mam.longHair = true;
ofy().save().entity(mam).now();

// This will return the Cat
Animal fetched = ofy().load().type(Animal.class).id(mam.id).now();
return Animal;

I will return that animal object which might be a Mammal instance and I should convert this to Mammal or any other subclass of Animal based on its instance on the client side.我将返回那个可能是Mammal实例的动物对象,并且我应该根据它在客户端的实例将它转换为MammalAnimal的任何其他子类。

Does anyone have any solution or workaround for this problem?有没有人对此问题有任何解决方案或解决方法?

Below is the JSON response for retrieving list of animals.下面是检索动物列表的 JSON 响应。 But I was not able to convert the animal object to Mammal on the Android client side.但我无法在 Android 客户端将动物对象转换为Mammal

{
 "items": [      
  {
   "id": "5631986051842048",
   "name": "mammm",
   "longHair": true,
   "kind": "animalApi#resourcesItem"
  },
  {
   "id": "5697423099822080",
   "name": "Animalll",
   "kind": "animalApi#resourcesItem"
  }
 ],
 "nextPageToken":     "CjwSNmofc35tb3ZpZWFkZGljdC12Mi1kZWJ1Zy1jb2RlaGFyZHITCxIGQW5pbWFsGICAgIDruI8KDBgAIAA",
 "kind": "animalApi#resources",
 "etag": "\"cZTcM4l5N_SjrKcEhGkKJEl9rU4/rs8BjIS52gHgN0B1UGEEi2DERwE\""
}

Unfortunately cloud endpoints don't use your actual classes when serializing/deserializing it's JSONs.不幸的是,云端点在序列化/反序列化它的 JSON 时不使用您的实际类。 It uses a generated "flat" POJO class which is completely unaware of any hierarchy you have in your original class.它使用生成的“平面” POJO 类,它完全不知道原始类中的任何层次结构。 So by the time you get an "Animal" class on your Android client there i not relation whatsoever to the other classes.因此,当您在 Android 客户端上获得“动物”类时,我与其他类没有任何关系。

Your only hope would be to send and additional parameter indicating the object's actual kind and assemble the corresponding instance manually on the Android size.您唯一的希望是发送指示对象实际类型的附加参数,并在 Android 大小上手动组装相应的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM