Hello everyone. Anyone could help me with a serial...
# ktor
d
Hello everyone. Anyone could help me with a serialization error. I’ve set up an Exposed user in ktor 2:
Copy code
object Users: IntIdTable() {
    val username = varchar("username", 128)
    val email = varchar("email", 128)
    val password = varchar("password", 1024)
}

class User(id: EntityID<Int>, username: String, email: String, password: String): IntEntity(id) {
    companion object : IntEntityClass<User>(Users)
    var username by Users.username
    var email by Users.email
    var password by Users.password
}
my serialization plugin is :
Copy code
import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.plugins.*
import io.ktor.server.application.*

fun Application.configureSerialization() {
    install(ContentNegotiation) {
        json()
    }
}
when I call http://0.0.0.0:8080/users, I get
Copy code
500: kotlinx.serialization.SerializationException: Serializer for class 'User' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
If I try adding @Serializable above the class User, my ide says
Copy code
This class does not have a constructor
Cannot access 'Serializable': it is internal in '<http://kotlin.io|kotlin.io>'
You can view my project on this branch: https://github.com/danygiguere/ktor-kotlin-example/tree/setting-up-exposed
e
wrong serializable,
kotlinx.serialization.Serializable
d
thanks @ephemient Does it mean I need to replace a depedency or does it have to do with my serialization plugin ?
e
no, it means you need to import that one and not
kotlin.io.Serializable
when you write
@Serializable
the IDE's auto-import functionality can get confused by multiple names sometimes so don't rely on it, check
d
I’m getting this ide error now though:
Impossible to make this class serializable because its parent is not serializable and does not have exactly one constructor without parameters
e
that's a #serialization question but will depend on your class structure. every property should be a
constructor(val)
, https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/basic-serialization.md#constructor-properties-requirement
d
thanks @ephemient I’ll try to figure it out, else I’ll bring the question to the serialization channel
a
I don’t think you can properly serialize/deserialize entity class so you need a separate serializable
User
class.
d
@thanks @Aleksei Tirman [JB] I’ve created a Ca model to simplify things and I’ll bring the question to the serialization channel.