0

I'm having problem with Json.encodeToString . Because it's required reified type . But i can't provide reified type on my program. So, how i can get it happen for below code...

// I don't want to use reified
fun <I> encodeToString(model: I): String {
    return Json.encodeToString(model) // EROR: type mismatch
}

Please help to get rid of this problem. Any argument based solution or reflection would be good instead of reified

Thanks

1 Answers1

0

After doing some research i've converted the KType to SerializationStrategy<T>.

fun <I> encodeToString(type: KType, model: I): String {
    return Json.Default.encodeToString(Json.serializersModule.serializer(type), model)
}

And it works.

  • If you have access to the `KType`, then just doing `Json.encodeToString(serializer(type), model)` should work too. – Sweeper Jan 11 '23 at 04:59