Deserialize a Firebase Data-Snapshot to a Kotlin d

2019-03-31 20:08发布

问题:

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
)

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())
                }
            }
        }

    })
}

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

Thanks

回答1:

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.

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 = ""
)