I'd like to construct an object from an instance of KClass and a Map<String, String> where I map fieldnames to values.
An example would be:
val testVal = mapOf("title" to "Foo", "description" to "bar")
data class Article(val title: String, val description)
fun <T> deserialize(clazz: KClass<T>, Map<String,String>) : T? = //magic
val article = deserialize(Article::class, testVal)
// here article should be Article{title: "Foo", description: "Bar"}
My question is how would I do this with Kotlin reflection? How do I build an object from the available data? I think it is enough if you point me to the right direction or give an idea. I mean there is definitely a way because I think this is what json serializers do (I just want a simpler version of that with much less functionality, on different dataformat).
I am happy if I can make this work with data classes that can only store primitive data types (int, string, boolean).
I know I could use something like Jackson and just build a JsonNode and deserialize it, but that is not waht I look for. I'd like to build my own version of this with reflection.