data-class

Deserialize a Firebase Data-Snapshot to a Kotlin data class

六月ゝ 毕业季﹏ 提交于 2019-11-27 18:40:46
问题 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

Kotlin Data Class from Json using GSON

穿精又带淫゛_ 提交于 2019-11-27 09:43:36
问题 I have Java POJO class like this: class Topic { @SerializedName("id") long id; @SerializedName("name") String name; } and I have a Kotlin data class Like this data class Topic(val id: Long, val name: String) How to provide the json key to any variables of the kotlin data class like the @SerializedName annotation in java variables ? 回答1: Data class: data class Topic( @SerializedName("id") val id: Long, @SerializedName("name") val name: String, @SerializedName("image") val image: String,

Extend data class in Kotlin

旧城冷巷雨未停 提交于 2019-11-27 07:01:49
Data classes seem to be the replacement to the old-fashioned POJOs in Java. It is quite expectable that these classes would allow for inheritance, but I can see no convenient way to extend a data class. What I need is something like this: open data class Resource (var id: Long = 0, var location: String = "") data class Book (var isbn: String) : Resource() The code above fails because of clash of component1() methods. Leaving data annotation in only one of classes does not do the work, too. Perhaps there is another idiom to extend data classes? UPD: I might annotate only child child class, but

Kotlin data class: how to read the value of property if I don't know its name at compile time?

心已入冬 提交于 2019-11-27 02:15:10
问题 How can I read the value of property in a Kotlin data class instance if the property name is only known at runtime? 回答1: Here is a function to read a property from an instance of a class given the property name ( throws exception if property not found, but you can change that behaviour ): import kotlin.reflect.KProperty1 import kotlin.reflect.full.memberProperties @Suppress("UNCHECKED_CAST") fun <R> readInstanceProperty(instance: Any, propertyName: String): R { val property = instance::class

Extend data class in Kotlin

假装没事ソ 提交于 2019-11-26 12:09:55
问题 Data classes seem to be the replacement to the old-fashioned POJOs in Java. It is quite expectable that these classes would allow for inheritance, but I can see no convenient way to extend a data class. What I need is something like this: open data class Resource (var id: Long = 0, var location: String = \"\") data class Book (var isbn: String) : Resource() The code above fails because of clash of component1() methods. Leaving data annotation in only one of classes does not do the work, too.