在Firestore文档的Java类中指定序列化名称 [英] Specifying serialized names in java class for firestore document

查看:77
本文介绍了在Firestore文档的Java类中指定序列化名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用document存储在我的android应用程序的firestore中="nofollow noreferrer">自定义对象.如果我使用Proguard构建应用程序,是否可以像Gson使用@SerializedName注释提供的方式那样为类中的字段指定序列化名称?

I am trying to store a document in firestore in my android app using a custom object. If I am using proguard for building my app, is there a way to specify the serialized name for the fields inside my class like the way Gson provides using @SerializedName annotation?

推荐答案

您可以使用

You can specify the name a Java property gets in the JSON of the document with the PropertyName annotation. For example:

public class Data {
    @PropertyName("some_field_name")
    public String someFieldName
}

如果您使用getter和setter(而不是使用上面的公共字段),请确保在getter和setter上都放置注释:

If you use getters and setters (instead of using a public field as above), be sure to put the annotation on both getter and setter:

public class Data {
    private String someFieldName

    @PropertyName("some_field_name")
    public String getSomeFieldName() { return someFieldName; }

    @PropertyName("some_field_name")
    public void getSomeFieldName(String someFieldName) { this.someFieldName = someFieldName; }
}

此注释在Cloud Firestore和较早的Firebase实时数据库之间共享,因此我建议您还检查一些使用Firebase序列化/反序列化的命名约定?

This annotation is shared between Cloud Firestore and the older Firebase Realtime Database, so I recommend also checking out some of the previous questions about PropertyName, sch as Naming convention with Firebase serialization/deserialization?

这篇关于在Firestore文档的Java类中指定序列化名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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