commonMain.net.folivo.trixnity.clientserverapi.model.uia.RequestWithUIA.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.uia
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.SerializationException
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.*
@Serializable(with = RequestWithUIASerializer::class)
data class RequestWithUIA(
val request: T,
val authentication: AuthenticationRequestWithSession?,
)
class RequestWithUIASerializer(private val baseSerializer: KSerializer) :
KSerializer> {
override val descriptor = buildClassSerialDescriptor("RequestWithUIASerializer")
override fun deserialize(decoder: Decoder): RequestWithUIA {
require(decoder is JsonDecoder)
val jsonObject = decoder.decodeJsonElement()
if (jsonObject !is JsonObject) throw SerializationException("expected request to be json object")
val request = decoder.json.decodeFromJsonElement(baseSerializer, jsonObject)
val authObject = jsonObject["auth"]
?.let { if (it !is JsonObject) throw SerializationException("expected auth to be json object") else it }
val auth = authObject?.let { decoder.json.decodeFromJsonElement(it) }
return RequestWithUIA(request, auth)
}
override fun serialize(encoder: Encoder, value: RequestWithUIA) {
require(encoder is JsonEncoder)
val jsonObject = JsonObject(
buildMap {
putAll(encoder.json.encodeToJsonElement(baseSerializer, value.request).jsonObject)
value.authentication?.let { authentication ->
put("auth", encoder.json.encodeToJsonElement(authentication))
}
}
)
encoder.encodeJsonElement(jsonObject)
}
}