Firebaser这里
这是针对Android的Firebase数据库SDK的某些版本中的已知错误:我们的串行器/解串器仅考虑声明类上的属性/字段.
在Firebase数据库SDK for Android版本9.0至9.6(iirc)中缺少从基类继承属性的序列化.从那以后,它已被添加到版本中.
解决方法
在此期间,您可以使用Jackson(其中使用的Firebase 2.x SDK)使继承模型工作.
更新:这是一个如何从JSON读取到您的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;
}
}
你会注意到,我做了getParentAttribute()public,因为只有公共字段/ getter被考虑.有了这个变化,这个JSON:
{
"childAttribute" : "child",
"parentAttribute" : "parent"
}
变得可读:
ObjectMapper mapper = new ObjectMapper();
GenericTypeIndicator> indicator = new GenericTypeIndicator>() {};
TestChild value = mapper.convertValue(dataSnapshot.getValue(indicator), TestChild.class);
GenericTypeIndicator有点奇怪,但幸运的是,这是一个可以复制/粘贴的魔法咒语.