将Firebase数据快照反序列化为Kotlin数据类 [英] Deserialize a Firebase Data-Snapshot to a Kotlin data class

查看:71
本文介绍了将Firebase数据快照反序列化为Kotlin数据类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的Kotlin数据类

Hi I have a Kotlin data class as follows

data class User (
        @get:Exclude val gUser: Boolean,
        @get:Exclude val uid: String,
        @get:PropertyName("display_name") val displayName: String,
        @get:PropertyName("email") val email: String,
        @get:PropertyName("account_picture_url") val accountPicUrl: String,
        @get:PropertyName("provider") val provider: String
)

我能够序列化该对象而没有任何问题.但是在执行Firebase查询时,我无法反序列化对象.目前,这就是我正在获取数据的步骤

I am able to serialize the object without an issues. But i'm having trouble deserializing the object when doing a firebase query. Currently this is what i'm doing to get the data

_firebaseReference.child(getString(R.string.firebase_users_key)).child(user.uid)
        .setValue(user).addOnCompleteListener{
    _firebaseReference.child("users").child(user.uid)
            .addListenerForSingleValueEvent(object : ValueEventListener {
        override fun onCancelled(p0: DatabaseError) {

        }

        override fun onDataChange(p0: DataSnapshot) {
            if (p0.exists()) {
                val userHash = p0.value as HashMap<*, *>
                var currentUser: User
                if (userHash[getString(R.string.provider_key)]
                        != getString(R.string.provider_google)) {
                    currentUser = User(false, p0.key!!, 
                            userHash["display_name"].toString(), 
                            userHash["email"].toString(),
                            userHash["account_picture_url"].toString(), 
                            userHash["provider"].toString())
                } else {
                    currentUser = User(true, p0.key!!, 
                            userHash["display_name"].toString(), 
                            userHash["email"].toString(), 
                            userHash["account_picture_url"].toString(), 
                            userHash["provider"].toString())
                }
            }
        }

    })
}

这只是我正在练习我的Kotlin的测试项目,但这是我想弄清楚的东西.

This is only a test project that i'm working on to practice my Kotlin, but this is something I would like to figure out.

如果我做错了,请告诉我,任何建议将不胜感激

If i'm doing it completely wrong please let me know, any advise would be greatly appreciated

谢谢

推荐答案

Firebase需要一个空的构造函数才能反序列化对象:

Firebase needs an empty constructor to be able to deserialize the objects:

data class User(
        @Exclude val gUser: Boolean,
        @Exclude val uid: String,
        @PropertyName("display_name") val displayName: String,
        @PropertyName("email") val email: String,
        @PropertyName("account_picture_url") val accountPicUrl: String,
        @PropertyName("provider") val provider: String
) {
    constructor() : this(false, "", "", "", "", "")
}

您可以像这样声明它并提供一些默认值以调用主构造函数,也可以为所有参数声明默认值:

You can either declare it like so and provide some default values to be able to call the primary constructor or you can declare default values for all your parameters:

data class User (
        @Exclude val gUser: Boolean = false,
        @Exclude val uid: String = "",
        @PropertyName("display_name") val displayName: String = "",
        @PropertyName("email") val email: String = "",
        @PropertyName("account_picture_url") val accountPicUrl: String = "",
        @PropertyName("provider") val provider: String = ""
)

然后将为您创建各种构造函数,包括一个空的构造函数.

Then various constructors will be created for you, including an empty constructor.

如果序列化存在问题,可能是由于ide生成的getter和setter引起的,请尝试使用@get和@set批注来增强它们:

If there's a problem with serialization there might be because of the getters and setters generated by the ide, try reinforcing them with @get and @set annotations:

data class User (
        @Exclude val gUser: Boolean = false,
        @Exclude val uid: String = "",
        @set:PropertyName("display_name") 
        @get:PropertyName("display_name")
        var displayName: String = "",
        @PropertyName("email") val email: String = "",
        @set:PropertyName("account_picture_url")
        @get:PropertyName("account_picture_url")
        var accountPicUrl: String = "",
        @PropertyName("provider") val provider: String = ""
)

这篇关于将Firebase数据快照反序列化为Kotlin数据类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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