I have an api that returns a json payload that has...
# ktor
s
I have an api that returns a json payload that has a number of keys of object ids and then the objects.
Copy code
{
  "-ZxqaLK58wkKYwibR4PMP": {"prop1": "val1", "prop2": "val2"},
  "-fdsafDEW34fSDFjnowe": {"prop1": "val3", "prop2": "val4"},
}
I'm using kotlinx.serialization to deserialize them but am encountering the error:
kotlinx.serialization.SerializationException: Can't locate argument-less serializer for interface java.util.Map
when I invoke the service with
<http://httpClient.post|httpClient.post><Map<String, MessageResult>>(urlStr)
The error message says that I should provide the serializer explicitly. Is that something I have to configure in my serializer module or something I have to tell ktor's json feature to do?
d
Try removing
import java.util.*
.
Which back-end is this on?
You may have to register a Map serializer.
s
It is common code. That exception message is from running tests on the jvm. I ended up creating a "holder" class with a custom serializer.
Copy code
@Serializable
class MessageResultMap(val items: Map<String, ItemMessageResult>) {
    @Serializer(MessageResultMap::class)
    companion object : KSerializer<MessageResultMap> {

        override val descriptor = StringDescriptor.withName("MessageResultMap")

        override fun serialize(encoder: Encoder, obj: MessageResultMap) {
            Pair(String.serializer(), ItemMessageResult.serializer()).map.serialize(encoder, obj.items)
        }

        override fun deserialize(decoder: Decoder): MessageResultMap {
            val items = Pair(String.serializer(), ItemMessageResult.serializer()).map.deserialize(decoder)
            return MessageResultMap(items)
        }
    }
}
And calling it like:
Copy code
val items = <http://httpClient.post|httpClient.post><MessageResultMap>(urlStr) {
	...
}.items
d
KotlinXSerializer().register(Pair(String.serializer(), ItemMessageResult.serializer()).map)
didn't work?