https://kotlinlang.org logo
#ktor
Title
# ktor
i

Ivan Pavlov

09/12/2020, 4:45 AM
Hello guys, I'm getting strange error using ktor-serialization:1.4.0
Copy code
kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 15: Expected '[, kind: SEALED'
JSON input: {
    "place":{
        "type":"point",
        "lon":22.0,
        "lat":56.7
    }
}
Routing looks like that:
Copy code
post("/") {
  val body = call.receive<AddVisitDataDto>()
  call.respond(body.toString())
}
My model:
Copy code
@kotlinx.serialization.Serializable
data class AddVisitDataDto(
    val place: PlaceDto
)

@kotlinx.serialization.Serializable
sealed class PlaceDto {
    @kotlinx.serialization.Serializable
    @SerialName("external")
    data class External(val externalId: String) : PlaceDto()
    @kotlinx.serialization.Serializable
    @SerialName("point")
    data class Point(val lon: Double, val lat: Double) : PlaceDto()
}
When I change receiving to the following code it works fine:
Copy code
val text = call.receiveText()
val body = Json.decodeFromString<AddVisitDataDto>(text)
I tried to add
@Polymorphic
to
place
property but it only changes error to
kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 15: Expected '[, kind: OPEN'
Am I doing something wrong?
Also serialization works like this:
Copy code
{
"place": [
  "point",
  {
    "lon": 22.0,
    "lat": 56.7
    }
]
}
5 Views