I have a problem with the serialization of a class in Kotlin.
build.gradle.kt
...
plugins {
application
kotlin("jvm") version "1.6.21"
kotlin("plugin.serialization").version("1.6.21")
}
...
dependencies{
...
implementation("io.ktor:ktor-server-content-negotiation:$ktor_version")
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktor_version")
}
Response.kt
import kotlinx.serialization.*
...
interface BaseResponse<T>
@Serializable
data class PaginatedResponse<T>(
val prev: Int?,
val next: Int?,
val totalCount: Int = 0,
val totalPages: Int = 0,
val data: T? = null,
val message: String? = null
) : BaseResponse<T>
usage
...
return PaginatedResponse<List<User>>(
prev,
next,
totalCount,
totalPages,
users
)
The data I am passing looks quite healthy and the error that is thrown when the return is reached is:
kotlinx.serialization.SerializationException: Serializer for class 'PaginatedResponse' is not found. Mark the class as @Serializable or provide the serializer explicitly.
Doing call.respond(User(...))
will not throw any error
so if I remove generic and make the PaginatedResponse non generic it will work but is not reusable anymore
@Serializable
data class PaginatedResponse(
val prev: Int?,
val next: Int?,
val totalCount: Long = 0,
val totalPages: Long = 0,
val data: List<User>? = null,
val message: String? = null
) : BaseResponse<User>
id 'kotlinx-serialization'
to the app module's build.gradle file fixed for me. – Kurbash