问题描述
我正在尝试使用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?
推荐答案
您可以使用 PropertyName 批注.例如:
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实时数据库之间共享,因此我建议您还检查一些的问题,例如
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类中指定序列化名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!