【发布时间】:2020-04-26 21:23:42
【问题描述】:
我正在使用
implementation 'com.google.firebase:firebase-firestore:21.3.1'
用于在 1.3.50 版中使用 Kotlin 测试 Firestore-Database SDK。
对于读/写测试,我创建了一个简单的数据类:
data class Location(
val uuid: String? = null,
val name: String? = null
)
正如预期的那样运行良好且流畅。
但是用 Kotlin 的密封类更深入地扩展数据类会导致序列化问题:
data class Location(
val uuid: String? = null,
val name: String? = null,
val locationProperty: LocationProperty? = null
)
sealed class LocationProperty {
data class TextProperty(
val text: String? = null
) : LocationProperty()
}
虽然写入数据库仍然可以正常工作,但应用程序在序列化结果时会引发异常:
document.toObject(Location::class.java)
java.lang.RuntimeException: No properties to serialize found
on class com.abc.def.LocationProperty
在 Firestore 前端,我看到我的密封类在 NoSQL 数据库中保存为 Map,收到的 DocumentSnapshot 的原始数据如下所示
(locationProperty =>ArraySortedMap{(text=>My entered text)}
所以这可能会导致错误。
有什么办法可以解决这个问题,如果可能的话,可以通过在向 Firestore 读取或写入数据时提供自定义序列化程序吗?
【问题讨论】:
标签: android firebase kotlin google-cloud-firestore