Hi guys, can you please help me understand where I...
# serialization
v
Hi guys, can you please help me understand where I am wrong? Hi, all. Maybe someone can help me understand what is the problem with my code for class serialization? Not to polute the channel posted question on stackoverflow. Thanks in advance! https://stackoverflow.com/questions/71988144/serializer-for-class-is-not-found-mark-the-class-as-serializable-or-prov thanks
p
ApplicationCall.respond
takes a reified type parameter which is captured and used for serialization in the ContentNegotiation plugin. As such, the compile-time (not run-time) type of the parameter value is used to fully resolve the serializer to use. If you're passing the body to the
.respond
method with only the type
BaseResponse<List<User>>
known, only that type information is captured, which isn't enough to pick the right serializer. You can work around this either by casting the value to
PaginatedResponse<T>
in the call (i.e.
call.respond(response as PaginatedResponse<T>)
or use an extension to take care of resolving all possible types of
BaseResponse<T>
:
Copy code
suspend inline fun <reified T> ApplicationCall.respond(message: BaseResponse<T>) = when(message) {
    is PaginatedResponse -> _respond(message)
    else -> _respond(message)
}
or possibly better yet would be to change
BaseResponse<T>
to be a
sealed interface
which might well solve the resolution problem for you.
❤️ 1