Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
84 views
in Technique[技术] by (71.8m points)

android - Firebase @PropertyName doesn't work

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 +
                    '}';
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...