George
07/29/2023, 12:13 PMsuspend inline fun <reified T : Any> ServerRequest.awaitAndReceive(): T {
val body = awaitBodyOrNull<String>()
requireNotNull(body) { bodyTypeErrorMessage<T>() }
return deserializeBody(body)
}
inline fun <reified T : Any> deserializeBody(body: String): T {
try {
return Json.decodeFromString<T>(body)
} catch (e: IllegalArgumentException) {
throw IllegalArgumentException(bodyTypeErrorMessage<T>())
}
}
Question is: do i deserialize the request two times in this case? 1 for awaitBodyOrNull<String>() and 1 manually?George
07/29/2023, 12:52 PMKotlinSerializationStringDecoder
, also deserializes it in two steps (like my code pretty much), though this is a current limitation from kotlinx.serialization
and will probably lifted in future.