Hi! Say you have: ```@Serializable data class Page...
# serialization
l
Hi! Say you have:
Copy code
@Serializable
data class PagedData<T>(val data: T, val page: Int, val totalPages: Int, val pageSize: Int)
Where you know for sure that
T
is a class that has the annotations
@Serializable
or a list of a type that is annotated with
@Serializable
. Why the serialization runtime is not able to infer what to use? Also, I am using Ktor, it would be kind of pointless to explicitly build a serializer before every response as such.
⬆️ 1
d
The issue here is because of type erasure. Not too long ago even
List<T>
had to have it's serializers explicitly created. This was "fixed" for bult-in types using the
typeOf
api. I believe support is being added to non built-in types is planned.
l
Oh ok, thanks for the reply! Is there some workaround?
d
My current workaround is to get the
String
from ktor the deserialize it myself with the explicitly created serializer.
l
That may be ok for deserialization, but if I have to serialize, how do I handle the
content-type
header if i return just a string?
d
Are you using client or server?
l
Server, the incriminated line is:
Copy code
call.respond(PagedData(data, page, pageSize, totalPages))
d
Ah. I was assuming client.
You could serialize it to a string yourself and respondText with content-type of json.
l
Yeah I'll try:
Copy code
call.respondText(
    deserializer.stringify(
        PagedData.serializer(ElaboratedTweet.serializer().list),
        PagedData(data, page, pageSize, totalPages)
    ),
    ContentType.Application.Json
)
Thanks :)
👌🏼 1
👍 1