Firebase 序列化名称

问题描述 投票:0回答:4

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

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

我想对发送到 firebase 的属性名称进行编码。目前密钥是使用变量名称发送的。我想对像

Useraname
Email
这样的键进行编码,就像
Gson
正在做的那样。我不想更改变量名称。

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

我使用了

@SerializateName()
,但不起作用。与 Firebse 使用的
@PropertyName
相同,但不起作用。我可以使用什么来序列化自定义密钥?

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

如图所示,它根据变量名称保存键。我从一个字段的注释中更改了属性名称,当我保存它时,它会忽略它,但是当我更改变量名称时,它会使用该新变量名称的键保存为新条目。

this文档中是一个方法

toMap()
。如果我确实喜欢那样,正在工作(对我来说不方便),但不与
@PropertyName
一起工作。

更新2

如果我用

@Exclude
标记 getter 和 setter,并且使用
@IgnoreExtraProperties
的类正在工作。我不必使用文档中的
toMap()
方法示例。正在使用
@PropertyName
中的指定名称。在我看来这不是一件好事,制造混乱。

android firebase firebase-realtime-database
4个回答
21
投票

Firebase SDK 每当获取或设置属性值时都会使用它为该属性找到的注释。这意味着您需要考虑 Firebase 如何获取/设置该值,并注释它看起来的每个位置。

由于您声明了

getter
方法,Firebase 将使用它来获取属性的值。它将使用该字段来设置值。所以注释需要在两者上:

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

如果您有 getter 和 setter,则注释需要位于它们上,但不再位于字段上:

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

3
投票

您正在寻找的是 SDK 版本 9.2 的功能,您现在可以使用新的

@PropertyName
属性来指定将字段从 Java 模型类序列化到数据库时要使用的名称。这取代了
@JsonProperty
属性。

@PropertyName("Username")
public String username;
@PropertyName("Email")
public String email;

另请参阅这篇post,其中 Frank van Puffelen 非常清楚地解释了这个概念。


2
投票

@属性名称:

标记序列化时要重命名的字段。 链接

您必须将

@PropertyName
与公共字段一起使用,并且不需要 getter/setter


0
投票

对于 Kotlin 数据类

实际上,您可以在 java/kotlin 中注释普通类的 getter 和 setter,但问题在于 data class,因为我们还必须注释成员,而简单的

@PropertyName("your-serialized-name")
无法按预期工作。

假设您有一个像这样的数据类:

data class User(
    val name: String,
    val email: String
) {
    constructor(): this("", "")
}

现在有两个问题:

问题1.我们无法注释数据类的构造函数参数化成员。那么我们如何使用注解呢?
解决方案:我们必须使用

@get:PropertyName("your-serialized-name)
@set:PropertyName("your-serialized-name)

以不同的方式进行注释

问题2.现在你可以看到,如果你使用setter注释,那么你必须将数据类的成员设置为

var
而不是
val
,但这不是你想要的,因为你可能想要成员为
non-mutable
.
解决方案:我没有明确的想法,因为这是错误或其他东西,但如果我们使用简单的注释
@PropertyName("your-serialized-name")
和getter 1
@PropertyName("your-serialized-name")
,那么getter和setter注释功能都可以实现。

所以最终你的数据类应该是这样的:

data class User(
    @PropertryName("Name")
    @get:PropertryName("Name")
    val name: String,

    @PropertryName("Email")
    @get:PropertryName("Email")
    val email: String
) {
    constructor(): this("", "")
}
© www.soinside.com 2019 - 2024. All rights reserved.