Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a Kotlin data class object to map?

Is there any easy way or any standard library method to convert a Kotlin data class object to a map/dictionary of its properties by property names? Can reflection be avoided?

like image 953
Gulshan Avatar asked Apr 16 '18 15:04

Gulshan


People also ask

Can data class be open Kotlin?

Kotlin Data Class Requirements The parameters of the primary constructor must be marked as either val (read-only) or var (read-write). The class cannot be open, abstract, inner or sealed. The class may extend other classes or implement interfaces.

What is data object in Kotlin?

Data class is a simple class which is used to hold data/state and contains standard functionality. A data keyword is used to declare a class as a data class.


2 Answers

I was using the jackson method, but turns out the performance of this is terrible on Android for first serialization (github issue here). And its dramatically worse for older android versions, (see benchmarks here)

But you can do this much faster with Gson. Conversion in both directions shown here:

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

val gson = Gson()

//convert a data class to a map
fun <T> T.serializeToMap(): Map<String, Any> {
    return convert()
}

//convert a map to a data class
inline fun <reified T> Map<String, Any>.toDataClass(): T {
    return convert()
}

//convert an object of type I to type O
inline fun <I, reified O> I.convert(): O {
    val json = gson.toJson(this)
    return gson.fromJson(json, object : TypeToken<O>() {}.type)
}

//example usage
data class Person(val name: String, val age: Int)

fun main() {

    val person = Person("Tom Hanley", 99)

    val map = mapOf(
        "name" to "Tom Hanley", 
        "age" to 99
    )

    val personAsMap: Map<String, Any> = person.serializeToMap()

    val mapAsPerson: Person = map.toDataClass()
}
like image 123
Tom Hanley Avatar answered Oct 11 '22 22:10

Tom Hanley


This extension function uses reflection, but maybe it'll help someone like me coming across this in the future:

inline fun <reified T : Any> T.asMap() : Map<String, Any?> {
    val props = T::class.memberProperties.associateBy { it.name }
    return props.keys.associateWith { props[it]?.get(this) }
}
like image 27
Ken Fehling Avatar answered Oct 11 '22 21:10

Ken Fehling