Specifying serialized names in java class for firestore document

会有一股神秘感。 提交于 2019-12-12 19:23:49

问题


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?


回答1:


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
}

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; }
}

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?



来源:https://stackoverflow.com/questions/51417744/specifying-serialized-names-in-java-class-for-firestore-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!