THE STORY
I am using Firebase Realtime Database in my app. 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. But I don't want to use that naming convention. I want the naming pattern to be this, item_name
.
WHAT I DID
I used the @PropertyName("item_name") above the field like this,
class Item { @PropertyName("item_name") int mItemName; // Simplified for brevity }
THE PROBLEM
Firebase seems to just ignore the annotation completely. 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 + '}'; } }
Solution for Kotlin data class:
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.
The only problem was, @PropertyName
annotation was not properly documented in 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. I also had the problem where the fields as well as the getter/setters were getting serialized, resulting in duplicate key/value pairs.
I solved the problem by using the annotation on the field name which were public and ignoring the 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; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With