问题
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