如何使用getValue(Subclass.class)反序列化Firebase中的子类

如何使用getValue(Subclass.class)反序列化Firebase中的子类

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

在类uk.edume.edumeapp.TestChild上找不到名称的setter / field

public class TestChild  extends TestMother {

    private String childAttribute;

    public String getChildAttribute() {
        return childAttribute;
    }}public class TestMother {

    protected String motherAttribute;

    protected String getMotherAttribute() {
        return motherAttribute;
    }}

这个功能

snapshot.getValue(TestChild.class);

motherAttribute属性是null,我得到

在类uk.edume.edumeapp.TestChild上找不到motherAttribute的setter / field

我解析的Json是:

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


ABOUTYOU
浏览 510回答 3
3回答

侃侃尔雅

Firebaser在这里这是某些版本的Firebase Database SDK for Android中的已知错误:我们的序列化程序/反序列化程序仅考虑声明的类上的属性/字段。Firebase Database SDK for Android版本9.0到9.6(iirc)中缺少基类继承属性的序列化。从那时起它被添加回版本中。解决方法在此期间,您可以使用Jackson(Firebase 2.x SDK在引擎盖下使用)来使继承模型工作。更新:这里有一个如何从JSON&nbsp;读取到您的内容的片段TestChild:public&nbsp;class&nbsp;TestParent&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;String&nbsp;parentAttribute; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;String&nbsp;getParentAttribute()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;parentAttribute; &nbsp;&nbsp;&nbsp;&nbsp;}}public&nbsp;class&nbsp;TestChild&nbsp;&nbsp;extends&nbsp;TestParent&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;String&nbsp;childAttribute; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;String&nbsp;getChildAttribute()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;childAttribute; &nbsp;&nbsp;&nbsp;&nbsp;}}你会注意到我getParentAttribute()公开了,因为只考虑公共领域/吸气者。有了这个改变,这个JSON:{ &nbsp;&nbsp;"childAttribute"&nbsp;:&nbsp;"child", &nbsp;&nbsp;"parentAttribute"&nbsp;:&nbsp;"parent"}变得可读:ObjectMapper&nbsp;mapper&nbsp;=&nbsp;new&nbsp;ObjectMapper();GenericTypeIndicator<Map<String,Object>>&nbsp;indicator&nbsp;=&nbsp;new&nbsp;GenericTypeIndicator<Map<String,&nbsp;Object>>()&nbsp;{};TestChild&nbsp;value&nbsp;=&nbsp;mapper.convertValue(dataSnapshot.getValue(indicator),&nbsp;TestChild.class);这GenericTypeIndicator有点奇怪,但幸运的是它是一个可以复制/粘贴的神奇咒语。

明月笑刀无情

这显然在版本9.6中得到了修复。修复了将派生类传递给DatabaseReference#setValue()未正确保存超类属性的问题。

萧十郎

对于:在类uk.edume.edumeapp.TestChild上找不到motherAttribute的setter / field为TestChild类设置setter:&nbsp;public&nbsp;class&nbsp;&nbsp;TestMother&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;String&nbsp;motherAttribute; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;String&nbsp;getMotherAttribute()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;motherAttribute; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//set &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;setMotherAttribute(String&nbsp;motherAttribute)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.motherAttribute=&nbsp;motherAttribute; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java
Android