Meika
01/07/2024, 7:19 AMfun tryDecode(someKClass: KClass<*>, jsonString: String) {
...
// throw JsonDecodingException
val result = json.decodeFromString(someKClass.serializer(), jsonString)
...
}
someKClass
in the actual scenario is retrieved from Ktor's TypeInfo.type
. This works on most cases but got stuck when the type is something like Map
or List
. It throws JsonDecodingException
(Polymorphic serializer was not found for missing class discriminator ('null')).
is there any way to solve this or the only thing I can do is check whether the someKClass
is List
or Map
and pass the appropriate Serializer
such as ListSerializer
or MapSerializer
? my goal is trying to make it as generic as possible so I want to avoid any type checking as much as possible.