Android firebase 9.0.0 setValue 序列化枚举 [英] Android firebase 9.0.0 setValue to serialize enums

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

问题描述

我有这个类使用枚举.解析到 Firebase json 过去工作得很好 - 使用 Jackson.迁移到 firebase-database:9.0.0 时,我得到以下信息:

I have this class making use of enums. Parsing to Firebase json used to work just fine - using Jackson. While migrating to firebase-database:9.0.0 I get the following:

com.google.firebase.database.DatabaseException:在类上找不到要序列化的属性.... MyClass$Kind

com.google.firebase.database.DatabaseException: No properties to serialize found on class .... MyClass$Kind

其中 Kind 是枚举声明的类型.班级:

where Kind is the enum declared type. The class:

public class MyClass {

    public enum Kind {W,V,U};

    private Double value;
    private Kind kind;

    /* Dummy Constructor */
    public MyClass() {}

    public Double getValue() {
        return value;
    }

    public void setValue(Double value) {
        this.value = value;
    }

    public Kind getKind() {
        return kind;
    }

    public void setKind(Kind kind) {
        this.kind = kind;
    }
}

我怀疑 Firebase 在序列化 setValue(new MyClass()) 时不再使用 Jackson.

I suspect Firebase no longer uses Jackson when serializing setValue(new MyClass()).

有没有办法让枚举类型序列化?或者对于序列化器方法有什么明确的方法?

Is there a way to get enum types serialized? Or some means to be explicit with respect to the serializer method?

推荐答案

你说得对,Firebase 数据库不再使用 Jackson 进行序列化或反序列化.

You are right, Firebase Database no longer uses Jackson for serialization or deserialization.

通过执行以下操作,您可以在与 Firebase 交谈时将您的枚举表示为字符串:

You can represent your enum as a String when talking to Firebase by doing this:

public class MyClass {

    public enum Kind {W,V,U};

    private Double value;
    private Kind kind;

    /* Dummy Constructor */
    public MyClass() {}

    public Double getValue() {
        return value;
    }

    public void setValue(Double value) {
        this.value = value;
    }

    // The Firebase data mapper will ignore this
    @Exclude
    public Kind getKindVal() {
      return kind;
    }

    public String getKind() {
        // Convert enum to string
        if (kind == null) {
          return null;
        } else {
          return kind.name();
        }
    }

    public void setKind(String kindString) {
        // Get enum from string
        if (kindString == null) {
          this.kind = null;
        } else {
          this.kind = Kind.valueOf(kindString);
        }
    }
}

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

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