webSocket("/ws") {
try {
while (true) {
when(val frame = incoming.receive()){
is Frame.Text -> {
val text = frame.readText() //i want to Serialize it to class object
send(Frame.Text(processRequest(text)))
}
else -> TODO()
}
}
} finally {
TODO()
}
}
i want to Serialize frame.readText()
to return class object
i'm totally new to Ktor world and i don't know if that is possible
You can use the underlying
kotlinx.serialization
that you might have already set up for ContentNegotiation already. If you haven't, instructions can be found here. This will require to make your class (I assumed the nameObjectType
) serializable with@Serializable
. More details here on how to make a class serializable and also how to encode/decode to JSON format. I included the solution snippet:I would usually use a flow (requires
kotlinx.coroutines
)