call.receive<List<User>>() throws cann...
# ktor
n
call.receive<List<User>>() throws cannot cast exception when accessing elements. The solution is to wrap the list inside an object and call like this: call.receive<Users>() But what if I need array to be the root of the JSON? What can I do? P.S. I'm using gson.
d
I guess it is this issue, right? https://github.com/ktorio/ktor/issues/365
n
Right. Thanks.
I was wrong, that's ktor-client issue, my is ktor-server.
m
­čĄĚ try using Jackson?
Yep, works fine with Jackson.
Copy code
post("/foo") {
                call.respond(call.receive<List<String>>().size)
            }
Copy code
echo '["foo", "bar"]' | http POST 127.0.0.1:9080/foo
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 1
Content-Type: application/json; charset=UTF-8

2
n
try to access the element from the list, thats when the exception happens
m
Worked fine to get the first element too.
Granted, I was only trying to get a
List<String>
In the short term you could use a wrapper class with a
@JsonUnwrapped
List field (for Jackson)