Firebase serialization names

后端 未结 3 1522
轻奢々
轻奢々 2021-01-12 18:52

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 Str         


        
3条回答
  •  孤独总比滥情好
    2021-01-12 19:45

    The Firebase SDK uses the annotation it finds for the property whenever it gets or sets its value. That means you need to consider how Firebase gets/sets the value, and annotate each place it looks.

    Since you're declaring a getter method, Firebase will use that to get the value of the property. It will use the field for setting the value. So the annotation needs to be on both:

    public class Pojo {
       @PropertyName("Guid")
       public String guid;
    
       @PropertyName("Name")
       public String name;
    
       @PropertyName("Guid")
       public String getPojoGuid() {
           return guid;
       }
    
       @PropertyName("Guid")
       public void setPojoGuid(String guid) {
           this.guid = guid;
       }
    }
    

    If you'd have getters and setters, the annotation would need to be on those, but not on the fields anymore:

    public class Pojo {
       private String guid;
       private String name;
    
       @PropertyName("Guid")
       public String getPojoGuid() {
           return guid;
       }
    
       @PropertyName("Guid")
       public void setPojoGuid(String value) {
           guid = value;
       }
    
       @PropertyName("Name")
       public void setPojoGuid(String guid) {
           this.guid = guid;
       }
    
       @PropertyName("Name")
       public void setPojoGuid(String value) {
           name = value;
       }
    }
    

提交回复
热议问题