【问题标题】:Specifying serialized names in java class for firestore document在 java 类中为 firestore 文档指定序列化名称
【发布时间】:2018-12-27 07:15:02
【问题描述】:

我正在尝试使用custom objectdocument 存储在我的Android 应用程序的firestore 中。如果我使用 proguard 来构建我的应用程序,有没有办法像 Gson 使用 @SerializedName 注释提供的那样为我的类中的字段指定序列化名称?

【问题讨论】:

    标签: firebase serialization google-cloud-firestore proguard


    【解决方案1】:

    您可以使用PropertyName 注释指定Java 属性在文档的JSON 中获取的名称。例如:

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

    如果您使用 getter 和 setter(而不是像上面那样使用公共字段),请务必将注解放在 getter 和 setter 上:

    public class Data {
        private String someFieldName;
    
        @PropertyName("some_field_name")
        public String getSomeFieldName() { return someFieldName; }
    
        @PropertyName("some_field_name")
        public void setSomeFieldName(String someFieldName) { this.someFieldName = someFieldName; }
    }
    

    此注释在 Cloud Firestore 和旧版 Firebase 实时数据库之间共享,因此我建议您同时查看一些 previous questions about PropertyName,例如 Naming convention with Firebase serialization/deserialization?

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    相关资源
    最近更新 更多