commonMain.net.folivo.trixnity.clientserverapi.model.keys.CatchingGetKeysResponseSerializer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of trixnity-clientserverapi-model Show documentation
Show all versions of trixnity-clientserverapi-model Show documentation
Multiplatform Kotlin SDK for matrix-protocol
package net.folivo.trixnity.clientserverapi.model.keys
import io.github.oshai.kotlinlogging.KotlinLogging
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerializationException
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonDecoder
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.decodeFromJsonElement
import net.folivo.trixnity.core.model.UserId
import net.folivo.trixnity.core.model.keys.SignedCrossSigningKeys
import net.folivo.trixnity.core.model.keys.SignedDeviceKeys
private val log = KotlinLogging.logger {}
object CatchingGetKeysResponseSerializer : KSerializer {
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("QueryKeysResponseSerializer")
override fun deserialize(decoder: Decoder): GetKeys.Response {
require(decoder is JsonDecoder)
val json = decoder.decodeJsonElement()
if (json !is JsonObject) throw SerializationException("QueryKeysResponse should be of type JsonObject")
return GetKeys.Response(
failures = json["failures"]?.let { decoder.json.decodeFromJsonElement(it) },
deviceKeys = json["device_keys"]?.let { deviceKeysJson ->
decoder.json.decodeFromJsonElement