Does anyone have an example of using Ktor + Kotlin...
# ktor
s
Does anyone have an example of using Ktor + Kotlinx.Serialization when getting a JsonArray back as a response? I see the
setListMapper()
and
registerList<>()
functions as part of the JsonFeature, but am unsure of how to use them, or if they are even applicable in the case of getting back an array response. Just trying to test it in a test project at the moment, this is what I have. Does my array need to be within the
@Serializable
data class itself? The format of the response is:
Copy code
[
    {
        id: 382910,
        name: "name",
        isEnabled: true
    },
    {
        //etc..
    }
]
I get this error:
Can't locate argument-less serializer for interface java.util.List (Kotlin reflection is not available). For generic classes, such as lists, please provide serializer explicitly.
which makes sense but I also believe there is a way to do this without writing a custom serializer, I just can't find the info anywhere.
d
Ah, that's an annoying error. I have a work-around. one sec.
s
🙏
Just replace
MatrixJson
with
Json
and should work for you.
s
hmm interesting.. will try to work this into my result class and see if it works 👍 I am curious if there is a way to do this without the extra parsing step however seeing as Ktor has a
setListMapper
function for their Kotlinx.Serializer json feature, which is just appending the
.list
onto the KSerializer internally. It seems to be made for this use case. 🤔
I see this in Ktor issues:
Copy code
Unfortunately, top-level lists are supported only for serialization in ktor.

The deserialization will be supported in 1.3.40 on jvm and 1.3.50 on other platforms.
not sure if the functionality was added however. Snooping around..
d
Iirc it hasn't been fully added yet.