I'm getting from server a list response like this ...
# ktor
l
I'm getting from server a list response like this
[{"id":1}, {"id":2}]
(without enclosing JSON object). How do I deserialize it?
j
ListSerializer(YourType.serializer())
l
So for Json I configure it like this in the `HttpClient`:
Copy code
install(ContentNegotiation) {
    json(Json)
}
Can I somehow register this
ListSerializer
as well, so the client tries it automatically when receiving the response?
j
Won’t this work?
val list: List<YourType>
= response.body()
2
d
response.body<List<YourType>>()
if you don't want to
val
it.
FWIW, an API that returns a JSON Array is a design-smell. "Oh, you want to report pagination? Hmm, can't do that with an array, too bad."
j
You absolutely can, as there are headers in which pagination cursors can be placed.
☝️ 1
☝🏼 1
l
Thank you!
d
@jw that’s some dark magic there ;-)
j
Not at all, here's an example with GitHub's API Which is using the Link HTTP header, defined in the HTTP specification.
I'm not a "REST everywhere" type of guy, but if you want to create a RESTful API, then you should only provide the data in the payload, and add the metadata to the headers
d
I was being flippant, since it’s an obvious answer in retrospect, but I didn’t think of it myself