Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

android - Firebase serialization names

I created an object to send some data to firebase. As an example, I use firebase user example:

public class User {
    public String username;
    public String email;

    public User() {
        // Default constructor required for calls to DataSnapshot.getValue(User.class)
    }

    public User(String username, String email) {
        this.username = username;
        this.email = email;
    }
}

I want to encode property names that are sent to firebase. Currently keys are sent using variable names. I want to encode keys something like Useraname and Email, like Gson is doing. I don't want to change variable names.

@SerializateName("Username")
public String username;
@SerializateName("Username")
public String email;

I used @SerializateName(), but is not working. Same with @PropertyName that is used by Firebse, is not working. What I can use in order to serializare custom keys?

Update 1

public class Pojo {
    @PropertyName("Guid")
    public String guid;

   @PropertyName("Name")
   public String name;

   public String getPojoGuid() {
       return guid;
   }

   public void setPojoGuid(String guid) {
       this.guid = guid;
   }
}

As you can see in the image, it saves keys based on variable names. I changed property name from annotation for one field and when i save it, it ignores it, but when i change variable name, it save as new entry with key for that new varialbe name.

enter image description here

In this documentation is a method toMap(). If i do like that, is working (is not convenient for me), but is not working with @PropertyName.

Update 2

If i mark getters and setters with @Exclude and class with @IgnoreExtraProperties is working. I don't have to use toMap() method example from documetation. Is using specified name from @PropertyName. Not a good thing in my opinion, create confuses.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...