This commit is contained in:
InsanusMokrassar 2021-04-15 14:30:45 +06:00
parent ffc2d23be7
commit 040dd517d8

View File

@ -6,13 +6,13 @@ import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.* import kotlinx.serialization.encoding.*
import kotlin.reflect.KClass import kotlin.reflect.KClass
class TypedSerializer<T : Any>( open class TypedSerializer<T : Any>(
kClass: KClass<T>, kClass: KClass<T>,
presetSerializers: Map<String, KSerializer<out T>> = emptyMap() presetSerializers: Map<String, KSerializer<out T>> = emptyMap()
) : KSerializer<T> { ) : KSerializer<T> {
private val serializers = presetSerializers.toMutableMap() protected val serializers = presetSerializers.toMutableMap()
@InternalSerializationApi @InternalSerializationApi
override val descriptor: SerialDescriptor = buildSerialDescriptor( override open val descriptor: SerialDescriptor = buildSerialDescriptor(
"TextSourceSerializer", "TextSourceSerializer",
SerialKind.CONTEXTUAL SerialKind.CONTEXTUAL
) { ) {
@ -21,7 +21,7 @@ class TypedSerializer<T : Any>(
} }
@InternalSerializationApi @InternalSerializationApi
override fun deserialize(decoder: Decoder): T { override open fun deserialize(decoder: Decoder): T {
return decoder.decodeStructure(descriptor) { return decoder.decodeStructure(descriptor) {
var type: String? = null var type: String? = null
lateinit var result: T lateinit var result: T
@ -45,12 +45,12 @@ class TypedSerializer<T : Any>(
} }
@InternalSerializationApi @InternalSerializationApi
private fun <O: T> CompositeEncoder.encode(value: O) { protected open fun <O: T> CompositeEncoder.encode(value: O) {
encodeSerializableElement(descriptor, 1, value::class.serializer() as KSerializer<O>, value) encodeSerializableElement(descriptor, 1, value::class.serializer() as KSerializer<O>, value)
} }
@InternalSerializationApi @InternalSerializationApi
override fun serialize(encoder: Encoder, value: T) { override open fun serialize(encoder: Encoder, value: T) {
encoder.encodeStructure(descriptor) { encoder.encodeStructure(descriptor) {
val valueSerializer = value::class.serializer() val valueSerializer = value::class.serializer()
val type = serializers.keys.first { serializers[it] == valueSerializer } val type = serializers.keys.first { serializers[it] == valueSerializer }
@ -60,13 +60,11 @@ class TypedSerializer<T : Any>(
} }
fun <O: T> include(type: String, serializer: KSerializer<O>) { open fun <O: T> include(type: String, serializer: KSerializer<O>) {
require(type !in serializers.keys)
serializers[type] = serializer serializers[type] = serializer
} }
fun exclude(type: String) { open fun exclude(type: String) {
require(type !in serializers.keys)
serializers.remove(type) serializers.remove(type)
} }
} }