使用 gson 将文档添加到 Firestore 时间戳问题

时间:2021-01-08 10:29:59

标签: android firebase kotlin google-cloud-firestore gson

将文档添加到 Firestore 时,不会跳过空值。 出于这个原因,我尝试使用 gson 序列化对象。

val gson = GsonBuilder()
    .create()

return gson.fromJson(
    gson.toJson(this), object : TypeToken<HashMap<String, Any>>() {}.type
)

这很好用,因为我的对象中有一个 Date 类型的变量。将序列化对象添加到我的 Firestore 时,日期变量将其类型更改为 String,但我要求它的类型为时间戳。如果我不使用 json 序列化对象,则类型符合我的要求,但包括所有值,即使它们为空。

0 个答案:

没有答案