如何使用 getValue(Subclass.class) 在 Firebase 中反序列化子类 [英] How to deserialise a subclass in Firebase using getValue(Subclass.class)

查看:25
本文介绍了如何使用 getValue(Subclass.class) 在 Firebase 中反序列化子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用适用于 android 的新 firebase sdk 并使用真正的数据库功能.当我使用 getValue(simple.class) 时,一切都很好.但是当我想解析一个子类的类时,母类的所有属性都是null,并且我有这种类型的错误:

<块引用>

在 uk.edume.edumeapp.TestChild 类中找不到名称的设置器/字段

public class TestChild extends TestMother {私有字符串 childAttribute;公共字符串 getChildAttribute() {返回子属性;}}公共类 TestMother {受保护的字符串motherAttribute;受保护的字符串 getMotherAttribute() {返回母亲属性;}}

这个功能

snapshot.getValue(TestChild.class);

motherAttribute 属性是 null,我得到 <块引用>

在 uk.edume.edumeapp.TestChild 类中找不到用于 MotherAttribute 的设置器/字段

我解析的 Json 是:

<代码>{"childAttribute" : "子类中的属性","motherAttribute" : "母类中的属性"}

解决方案

Firebaser here

这是适用于 Android 的 Firebase 数据库 SDK 的某些版本中的一个已知错误:我们的序列化器/反序列化器仅考虑已声明类的属性/字段.

适用于 Android 的 Firebase 数据库 SDK 的 9.0 至 9.6 (iirc) 版本中缺少从基类继承的属性的序列化.从那时起,它在版本中重新添加.

解决方法

与此同时,您可以使用 Jackson(Firebase 2.x SDK 在幕后使用)来使继承模型起作用.

更新:这里是如何从 JSON 中读取到您的 TestChild 的片段:

公共类TestParent {受保护的字符串 parentAttribute;公共字符串 getParentAttribute() {返回父属性;}}公共类 TestChild 扩展 TestParent {私有字符串 childAttribute;公共字符串 getChildAttribute() {返回子属性;}}

您会注意到我将 getParentAttribute() 设为公开,因为只考虑公共字段/getter.有了这个变化,这个 JSON:

<代码>{childAttribute":孩子","parentAttribute" : "父"}

变得可读:

ObjectMapper mapper = new ObjectMapper();GenericTypeIndicator>指示符 = 新的 GenericTypeIndicator>() {};TestChild 值 = mapper.convertValue(dataSnapshot.getValue(indicator), TestChild.class);

GenericTypeIndicator 有点奇怪,但幸运的是它是一个可以复制/粘贴的魔法咒语.

I'm using the new firebase sdk for android and use the real database feature. When i use the getValue(simple.class) everything is fine. But when i want to parse a class which is a subclass, all the attribute of the mother class are null, and i have this type of error:

No setter/field for name found on class uk.edume.edumeapp.TestChild

public class TestChild  extends TestMother {

    private String childAttribute;

    public String getChildAttribute() {
        return childAttribute;
    }
}

public class TestMother {

    protected String motherAttribute;

    protected String getMotherAttribute() {
        return motherAttribute;
    }
}

this function

snapshot.getValue(TestChild.class);

motherAttribute attribute is null, and I get

No setter/field for motherAttribute found on class uk.edume.edumeapp.TestChild

the Json that i parse is:

{
  "childAttribute" : "attribute in child class",
  "motherAttribute" : "attribute in mother class"
}

解决方案

Firebaser here

This is a known bug in some versions of the Firebase Database SDK for Android: our serializer/deserializer only considers properties/fields on the declared class.

Serialization of inherited properties from the base class, is missing in the in releases 9.0 to 9.6 (iirc) of the Firebase Database SDK for Android. It was added back in versions since then.

Workaround

In the meantime you can use Jackson (which the Firebase 2.x SDKs used under the hood) to make the inheritance model work.

Update: here's a snippet of how you can read from JSON into your TestChild:

public class TestParent {
    protected String parentAttribute;

    public String getParentAttribute() {
        return parentAttribute;
    }
}
public class TestChild  extends TestParent {
    private String childAttribute;

    public String getChildAttribute() {
        return childAttribute;
    }
}

You'll note that I made getParentAttribute() public, because only public fields/getters are considered. With that change, this JSON:

{
  "childAttribute" : "child",
  "parentAttribute" : "parent"
}

Becomes readable with:

ObjectMapper mapper = new ObjectMapper();
GenericTypeIndicator<Map<String,Object>> indicator = new GenericTypeIndicator<Map<String, Object>>() {};
TestChild value = mapper.convertValue(dataSnapshot.getValue(indicator), TestChild.class);

The GenericTypeIndicator is a bit weird, but luckily it's a magic incantation that can be copy/pasted.

这篇关于如何使用 getValue(Subclass.class) 在 Firebase 中反序列化子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆