Firebase序列化名称 [英] Firebase serialization names

查看:201
本文介绍了Firebase序列化名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个对象来发送一些数据到firebase。作为一个例子,我使用firebase用户的例子:

pre $ public class User {
public String username;
public String email;
$ b public User(){
//调用DataSnapshot.getValue(User.class)所需的默认构造函数
}

public User用户名,字符串电子邮件){
this.username = username;
this.email = email;




$ b我想编码发送到firebase的属性名称。目前密钥是使用变量名称发送的。我想对 Useraname Email 这样的密钥进行编码,例如 Gson 正在做。我不想更改变量名称。

  @SerializateName(用户名)
public String username;
@SerializateName(用户名)
public String email;

我使用了 @SerializateName()不管用。与Firebse使用的 @PropertyName 相同,不起作用。我可以使用什么来串行化自定义键?

更新1

<$ p公共类Pojo {
@PropertyName(Guid)
public String guid;

@PropertyName(Name)
public String name;

public String getPojoGuid(){
return guid;
}

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






正如你在图片中看到的那样,基于变量名称。我改变了一个字段的注解的属性名称,当我保存它,它忽略它,但是当我改变变量名称,它保存为新的条目与新的varialbe名称的键。





这个文档是一个方法 toMap()。如果我这样做,工作(对我来说不方便),但不是与 @PropertyName



<如果我用 @Exclude 和class标记getters和setter与 @IgnoreExtraProperties 正在工作。我不必使用documetation中的 toMap()方法示例。使用 @PropertyName 中的指定名称。在我看来,这不是一件好事,会造成混淆。

解决方案

Firebase SDK使用它为属性找到的注释或设置其值。这意味着您需要考虑Firebase如何获取/设置值,并注释每个地方。



由于您声明了一个 getter 方法,Firebase将使用该方法获取该属性的值。它将使用该字段来设置值。所以这个注解需要同时在这两个上面:

pre preublic $ p $ {
@PropertyName(Guid)
public String guid;

@PropertyName(Name)
public String name;
$ b $ @PropertyName(Guid)
public String getPojoGuid(){
return guid;

$ b @PropertyName(Guid)
public void setPojoGuid(String guid){
this.guid = guid;






如果你有getter和setter,注解我们需要在那些,但不是在字段:

  public class Pojo {
private String guid;
私人字符串名称;
$ b $ @PropertyName(Guid)
public String getPojoGuid(){
return guid;

$ b @PropertyName(Guid)
public void setPojoGuid(String value){
guid = value;

$ b @PropertyName(Name)
public void setPojoGuid(String guid){
this.guid = guid;

$ b @PropertyName(Name)
public void setPojoGuid(String value){
name = value;
}
}


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.

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.

解决方案

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;
   }
}

这篇关于Firebase序列化名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆