Hello, I’m trying to serialize a generic class `Ap...
# serialization
s
Hello, I’m trying to serialize a generic class 
ApiResponse<T>
 with 
kotlinx.serialization
. So i’ve created a 
ApiResponseSerializer
 class, with help of docs. But now i don’t understand how to use that serializer, since none of provided methods doesn’t seem to suit to me. That is the place where i need a help. My ApiResponse<T> is defined like:
Copy code
data class ApiResponse<T>(
    val data: T? = null,
    val error: ApiError? = null
)
And the regular usage (in KTOR server) is
Copy code
respond(
    HttpStatusCode.Created,
    ApiResponse(data = response.data)
)
I don’t know where i can “attach” my serializer to 
data
 field of my 
ApiResponse
 class. Thank you in advance.
a
for convenience let me create a test class, say
Copy code
@Serializable
data class Movie(val title: String,val year: Int)
if you need a
ApiResponse<Movie>
in
json
you would stringify as
Copy code
Json.stringify(ApiResponse.serializer(Movie.serializer),Move("Matrix",99))
I am not sure how to make it work with content negotiation. But If all the classess are annotated well, I assume it will work out of the box
s
i suppose Json.stringify will work, since since we provide Movie’s serializer. But my goal is to make it done automatically, without typing it every time
n
That has to rely on reflection then.. And defeats some of the reasons for using serialuzation