[英]Firebase @PropertyName doesn't work
THE STORY故事
I am using Firebase Realtime Database in my app.我在我的应用程序中使用 Firebase 实时数据库。 I have a model something like this.我有一个 model 这样的东西。
class Item {
int mItemName;
// Simplified for brevity
}
Now, this stores the field as itemName
in my real time database.现在,这会将字段作为itemName
存储在我的实时数据库中。 But I don't want to use that naming convention.但我不想使用那个命名约定。 I want the naming pattern to be this, item_name
.我希望命名模式是这样的, item_name
。
WHAT I DID我做了什么
I used the @PropertyName("item_name") above the field like this,我像这样在字段上方使用了@PropertyName("item_name"),
class Item {
@PropertyName("item_name")
int mItemName;
// Simplified for brevity
}
THE PROBLEM问题
Firebase seems to just ignore the annotation completely. Firebase 似乎完全忽略了注释。 There is no way I am able to change the property names for serialization and deserialization.我无法更改序列化和反序列化的属性名称。
Any help would be highly appreciated.任何帮助将不胜感激。
EDIT编辑
Here is the complete model class in concern,这里是关注的完整model class,
public class FileModel {
@PropertyName("file_id")
String mFileId;
@PropertyName("file_name")
String mOriginalFileName;
@PropertyName("file_path")
String mFilePath;
@PropertyName("file_type")
String mFileType;
@PropertyName("last_modified")
Long mFileLastModified;
@PropertyName("file_size")
String mFileSize;
@Exclude
private boolean mIsSelected;
/**
* Must have empty constructor for JSON deserialization by Firebase
*/
public FileModel() {
}
public FileModel(String fileId, String originalFileName,
String filePath, String fileType, Long fileLastModified, String fileSize) {
this.mFileId = fileId;
this.mOriginalFileName = originalFileName;
this.mFilePath = filePath;
this.mFileType = fileType;
this.mFileLastModified = fileLastModified;
this.mFileSize = fileSize;
}
public String getFileId() {
return mFileId;
}
public void setFileId(String fileId) {
this.mFileId = fileId;
}
public String getOriginalFileName() {
return mOriginalFileName;
}
public void setOriginalFileName(String originalFileName) {
this.mOriginalFileName = originalFileName;
}
public String getFilePath() {
return mFilePath;
}
public void setFilePath(String filePath) {
this.mFilePath = filePath;
}
public String getFileType() {
return mFileType;
}
public void setFileType(String fileType) {
this.mFileType = fileType;
}
public Long getFileLastModified() {
return mFileLastModified;
}
public void setFileLastModified(Long fileLastModified) {
this.mFileLastModified = fileLastModified;
}
public String getFileSize() {
return mFileSize;
}
public void setFileSize(String fileSize) {
this.mFileSize = fileSize;
}
public boolean getIsSelected() {
return mIsSelected;
}
public void setIsSelected(boolean isSelected) {
this.mIsSelected = isSelected;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FileModel model = (FileModel) o;
if (mIsSelected != model.mIsSelected) return false;
if (mFileId != null ? !mFileId.equals(model.mFileId) : model.mFileId != null) return false;
if (mOriginalFileName != null ? !mOriginalFileName.equals(model.mOriginalFileName) : model.mOriginalFileName != null)
return false;
if (mFilePath != null ? !mFilePath.equals(model.mFilePath) : model.mFilePath != null)
return false;
if (mFileType != null ? !mFileType.equals(model.mFileType) : model.mFileType != null)
return false;
if (mFileLastModified != null ? !mFileLastModified.equals(model.mFileLastModified) : model.mFileLastModified != null)
return false;
return mFileSize != null ? mFileSize.equals(model.mFileSize) : model.mFileSize == null;
}
@Override
public int hashCode() {
int result = mFileId != null ? mFileId.hashCode() : 0;
result = 31 * result + (mOriginalFileName != null ? mOriginalFileName.hashCode() : 0);
result = 31 * result + (mFilePath != null ? mFilePath.hashCode() : 0);
result = 31 * result + (mFileType != null ? mFileType.hashCode() : 0);
result = 31 * result + (mFileLastModified != null ? mFileLastModified.hashCode() : 0);
result = 31 * result + (mFileSize != null ? mFileSize.hashCode() : 0);
result = 31 * result + (mIsSelected ? 1 : 0);
return result;
}
@Override
public String toString() {
return "FileModel{" +
"mFileId='" + mFileId + '\'' +
", mOriginalFileName='" + mOriginalFileName + '\'' +
", mFilePath='" + mFilePath + '\'' +
", mFileType='" + mFileType + '\'' +
", mFileLastModified=" + mFileLastModified +
", mFileSize='" + mFileSize + '\'' +
", mIsSelected=" + mIsSelected +
'}';
}
}
Kotlin 数据类的解决方案:
data class Pojo (@get:PropertyName("fieldName") @set:PropertyName("fieldName") var field: String = "")
Finally got a chance to solve this problem.终于有机会解决这个问题了。 Thanks to @hatboysam for the suggestion.感谢@hatboysam的建议。
The only problem was, @PropertyName
annotation was not properly documented in Firebase.唯一的问题是,@ @PropertyName
注释在 Firebase 中没有正确记录。
The first thing that is necessary is the the field must be public otherwise the annotation will not work, which is quite obvious/首先需要的是字段必须是公共的,否则注释将不起作用,这是很明显的/
Now the annotation takes into account both the field name as well as the getter/setter names to serialize.现在,注释同时考虑了要序列化的字段名称和 getter/setter 名称。 I also had the problem where the fields as well as the getter/setters were getting serialized, resulting in duplicate key/value pairs .我也遇到了字段以及 getter/setter 被序列化的问题,导致重复的键/值对。
I solved the problem by using the annotation on the field name which were public and ignoring the getter/setters .我通过在公共字段名称上使用注释并忽略 getter/setters解决了这个问题。 This solved the problem perfectly.这样就完美解决了问题。 Not the data was properly serialized with the property name I wanted and there was no duplicate data problem as well.数据没有使用我想要的属性名称正确序列化,也没有重复数据问题。
Here is a simple example,这是一个简单的例子,
class Item {
@PropertyName("item_no")
int mItemNo;
// Simplified for brevity
@Exclude
public int getItemNo(){
return mItemNo;
}
@Exclude
public void setItemNo(int itemNo){
this.mItemNo = itemNo;
}
}
Alternatively just mark your getters with @PropertyName
instead of annotating properties themselves - this way you can keep properties private while providing custom names:或者,只需使用@PropertyName
标记您的 getter 而不是注释属性本身 - 这样您就可以在提供自定义名称的同时保持属性私有:
public class User extends Object {
private String mDisplayName;
@PropertyName("userName")
public String getDisplayName() {
return mDisplayName;
}
@PropertyName("userName")
public void setDisplayName(String displayName) {
mDisplayName = displayName;
}
}
The Kotlin solution for data classes with immutable fields:具有不可变字段的数据类的Kotlin解决方案:
data class Item(
@get:PropertyName("item_name")
val mItemName: Int = 0 // Simplified for brevity
)
The Kotlin solution for data classes with mutable fields:具有可变字段的数据类的 Kotlin 解决方案:
data class Item(
@set:PropertyName("item_name")
@get:PropertyName("item_name")
var mItemName: Int = 0 // Simplified for brevity
)
THE STORY故事
I am using Firebase Realtime Database in my app.我在我的应用中使用 Firebase 实时数据库。 I have a model something like this.我有一个这样的模型。
class Item {
int mItemName;
// Simplified for brevity
}
Now, this stores the field as itemName
in my real time database.现在,这将字段作为itemName
存储在我的实时数据库中。 But I don't want to use that naming convention.但我不想使用那个命名约定。 I want the naming pattern to be this, item_name
.我希望命名模式是这样的, item_name
。
WHAT I DID我做了什么
I used the @PropertyName("item_name") above the field like this,我像这样在字段上方使用了@PropertyName("item_name"),
class Item {
@PropertyName("item_name")
int mItemName;
// Simplified for brevity
}
THE PROBLEM问题
Firebase seems to just ignore the annotation completely. Firebase 似乎完全忽略了注释。 There is no way I am able to change the property names for serialization and deserialization.我无法更改序列化和反序列化的属性名称。
Any help would be highly appreciated.任何帮助将不胜感激。
EDIT编辑
Here is the complete model class in concern,这是所关注的完整模型类,
public class FileModel {
@PropertyName("file_id")
String mFileId;
@PropertyName("file_name")
String mOriginalFileName;
@PropertyName("file_path")
String mFilePath;
@PropertyName("file_type")
String mFileType;
@PropertyName("last_modified")
Long mFileLastModified;
@PropertyName("file_size")
String mFileSize;
@Exclude
private boolean mIsSelected;
/**
* Must have empty constructor for JSON deserialization by Firebase
*/
public FileModel() {
}
public FileModel(String fileId, String originalFileName,
String filePath, String fileType, Long fileLastModified, String fileSize) {
this.mFileId = fileId;
this.mOriginalFileName = originalFileName;
this.mFilePath = filePath;
this.mFileType = fileType;
this.mFileLastModified = fileLastModified;
this.mFileSize = fileSize;
}
public String getFileId() {
return mFileId;
}
public void setFileId(String fileId) {
this.mFileId = fileId;
}
public String getOriginalFileName() {
return mOriginalFileName;
}
public void setOriginalFileName(String originalFileName) {
this.mOriginalFileName = originalFileName;
}
public String getFilePath() {
return mFilePath;
}
public void setFilePath(String filePath) {
this.mFilePath = filePath;
}
public String getFileType() {
return mFileType;
}
public void setFileType(String fileType) {
this.mFileType = fileType;
}
public Long getFileLastModified() {
return mFileLastModified;
}
public void setFileLastModified(Long fileLastModified) {
this.mFileLastModified = fileLastModified;
}
public String getFileSize() {
return mFileSize;
}
public void setFileSize(String fileSize) {
this.mFileSize = fileSize;
}
public boolean getIsSelected() {
return mIsSelected;
}
public void setIsSelected(boolean isSelected) {
this.mIsSelected = isSelected;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FileModel model = (FileModel) o;
if (mIsSelected != model.mIsSelected) return false;
if (mFileId != null ? !mFileId.equals(model.mFileId) : model.mFileId != null) return false;
if (mOriginalFileName != null ? !mOriginalFileName.equals(model.mOriginalFileName) : model.mOriginalFileName != null)
return false;
if (mFilePath != null ? !mFilePath.equals(model.mFilePath) : model.mFilePath != null)
return false;
if (mFileType != null ? !mFileType.equals(model.mFileType) : model.mFileType != null)
return false;
if (mFileLastModified != null ? !mFileLastModified.equals(model.mFileLastModified) : model.mFileLastModified != null)
return false;
return mFileSize != null ? mFileSize.equals(model.mFileSize) : model.mFileSize == null;
}
@Override
public int hashCode() {
int result = mFileId != null ? mFileId.hashCode() : 0;
result = 31 * result + (mOriginalFileName != null ? mOriginalFileName.hashCode() : 0);
result = 31 * result + (mFilePath != null ? mFilePath.hashCode() : 0);
result = 31 * result + (mFileType != null ? mFileType.hashCode() : 0);
result = 31 * result + (mFileLastModified != null ? mFileLastModified.hashCode() : 0);
result = 31 * result + (mFileSize != null ? mFileSize.hashCode() : 0);
result = 31 * result + (mIsSelected ? 1 : 0);
return result;
}
@Override
public String toString() {
return "FileModel{" +
"mFileId='" + mFileId + '\'' +
", mOriginalFileName='" + mOriginalFileName + '\'' +
", mFilePath='" + mFilePath + '\'' +
", mFileType='" + mFileType + '\'' +
", mFileLastModified=" + mFileLastModified +
", mFileSize='" + mFileSize + '\'' +
", mIsSelected=" + mIsSelected +
'}';
}
}
If you're using JAVA and the other solutions not working for you then this will definitely work!如果您使用的是JAVA而其他解决方案对您不起作用,那么这肯定会起作用!
I wanted like this where every variable's first letter started with Capital.我想要这样,每个变量的第一个字母都以大写开头。
public class CommentModel {
@PropertyName("BuyerID")
Integer buyerId;
@PropertyName("Message")
String message;
@PropertyName("Name")
String name;
@PropertyName("Photo")
String photo;
@PropertyName("SellerID")
Integer sellerId;
public CommentModel() {
}
@PropertyName("BuyerID")
public Integer getBuyerId() {
return buyerId;
}
@PropertyName("BuyerID")
public void setBuyerId(Integer buyerId) {
this.buyerId = buyerId;
}
@PropertyName("Message")
public String getMessage() {
return message;
}
@PropertyName("Message")
public void setMessage(String message) {
this.message = message;
}
@PropertyName("Name")
public String getName() {
return name;
}
@PropertyName("Name")
public void setName(String name) {
this.name = name;
}
@PropertyName("SellerID")
public Integer getSellerId() {
return sellerId;
}
@PropertyName("SellerID")
public void setSellerId(Integer sellerId) {
this.sellerId = sellerId;
}
@PropertyName("Photo")
public String getPhoto() {
return photo;
}
@PropertyName("Photo")
public void setPhoto(String photo) {
this.photo = photo;
}
}
Note that , here @PropertyName("")
is com.google.firebase.database.PropertyName
请注意,这里的@PropertyName("")
是com.google.firebase.database.PropertyName
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.