savrov
08/04/2020, 2:33 PMApiResponse<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:
data class ApiResponse<T>(
val data: T? = null,
val error: ApiError? = null
)
And the regular usage (in KTOR server) is
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.andylamax
08/04/2020, 4:37 PM@Serializable
data class Movie(val title: String,val year: Int)
if you need a ApiResponse<Movie>
in json
you would stringify as
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 boxsavrov
08/04/2020, 4:46 PMNikky
08/06/2020, 10:26 AM