Any pointers on how to kotlin.serialize with HttpC...
# ktor
p
Any pointers on how to kotlin.serialize with HttpClient to deserialize json with lists as root? I’m using: HttpClient { install(JsonFeature) { serializer = KotlinxSerializer().apply { setMapper(MyClass::class, MyClass.serializer()) setMapper(AnotherClass::class, AnotherClass.serializer()) } } install(ExpectSuccess) } Appears I need to setMapper for List<MyClass>, however that is not possible with generics. I see I can get the serializer for it with MyClass.serialzer().list, but registering it to deseriralize/serialize on http requests is not straight forward. Anyone know of a good solution? https://stackoverflow.com/questions/52971069/ktor-serialize-deserialize-json-with-list-as-root-in-multiplatform
m
Last time I used kotlinx.serialization it wasn't possible to deserialize maps/lists on kotlin/js and I had to wrap every payload. Maybe that is still the case?
n
there should be a option to specify the serializer used at the callsite, at least in Fuel there is i have not used ktor-client for a while
p
Interesting. I’m not targeting JS for this project though.
j
right now it's not possible with KotlinxSerializer due to missing generic information. I had the same question - I'll search for the answer
i think you can make your own kotlinxserializer for ktor using this function, it will work for anything receiving at least since you have a typetoken there
n
last time i tried to do that for a outgoing jsonBody the pipeline did not have any typetoken
j
@Nikky I think you mean TypeInfo - unfortunately the generic type is erased at runtime (see my link) so that won't help you in this case
p
Right, I don’t think we have type of the generic. I may be able to get something working in my case, but a generic solution doesn’t seem possible currently
just not the pipline for sending data apparently
anyways this TypeInfo and its reified type could be passed to
serializerByTypeToken
on kotlinx-serialization
p
thanks will take a look
I see what you are saying now NikkyAi. I am working with a multiplatform project though, so doesn’t look like serializerByTypeToken is an option.
n
well that seems annoying, in that case you can still use a serializer and turn it into a list serializer, you just way have to turn the result into a string and parse it then unless ktor-client has a method for passing in a custom serializer