dany giguere
05/08/2022, 3:30 PMobject Cars: IntIdTable() {
val name = varchar("name", 128)
val year = integer("year")
}
class Car(id: EntityID<Int>): IntEntity(id) {
companion object : IntEntityClass<Car>(Cars)
var name by Cars.name
var year by Cars.year
}
//@Serializable
class SerializedCar(id: EntityID<Int>, name: String, year: Int): IntEntity(id)
then :
class CarDAO {
private fun resultRowToCar(row: ResultRow) = SerializedCar(
id = row[Cars.id],
name = row[Cars.name],
year = row[Cars.year]
)
suspend fun show(id: Int): SerializedCar? = transaction {
Cars
.select { Cars.id eq id }
.map(::resultRowToCar)
.singleOrNull()
}
}
and my route is :
get("/cars/{id}") {
val id = call.parameters.getOrFail<Int>("id").toInt()
call.respond(mapOf("car" to carDAO.show(id)))
}
but I get this error :
500: kotlinx.serialization.SerializationException: Serializer for class 'SerializedCar' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
You can see the project here for more info:
<https://github.com/danygiguere/ktor-kotlin-example>
And if I uncomment //@Serializable
, my IDE tells me : Impossible to make this class serializable because its parent is not serializable and does not have exactly one constructor without parameters
hfhbd
05/08/2022, 9:25 PM@Serializable
data class SerializedCar(id: Int, name: String, year: Int)
Alternative you could write your custom serializer for Car
by yourself.