commonMain.com.bselzer.ktx.serialization.context.XmlContext.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of serialization-xml Show documentation
Show all versions of serialization-xml Show documentation
kotlinx.serialization extensions for pdvrieze.xmlutil
package com.bselzer.ktx.serialization.context
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import nl.adaptivity.xmlutil.serialization.XML
import nl.adaptivity.xmlutil.serialization.XmlValue
open class XmlContext(
override val instance: XML
) : StringFormatContext(instance) {
companion object Default : XmlContext(XML.defaultInstance)
internal class Wrapper(
@XmlValue
val value: T
)
internal class WrapperSerializer(
private val valueSerializer: DeserializationStrategy
) : DeserializationStrategy> {
@OptIn(ExperimentalSerializationApi::class)
override val descriptor: SerialDescriptor = SerialDescriptor("Wrapper", valueSerializer.descriptor)
override fun deserialize(decoder: Decoder): Wrapper {
val value = decoder.decodeSerializableValue(valueSerializer)
return Wrapper(value)
}
}
override fun String.decode(deserializer: DeserializationStrategy): T {
val wrapper = WrapperSerializer(deserializer)
return instance.decodeFromString(wrapper, "$this ").value
}
}