TypedSerializer#plusAssign and TypedSerializer#minusAssign

This commit is contained in:
InsanusMokrassar 2021-09-08 12:18:42 +06:00
parent ef9af71960
commit a24a335743
2 changed files with 18 additions and 2 deletions

View File

@ -8,6 +8,8 @@
* New property `MPPFile#withoutSlashAtTheEnd` * New property `MPPFile#withoutSlashAtTheEnd`
* Extension `clamp` has been deprecated * Extension `clamp` has been deprecated
* New extension `Iterable#diff` * New extension `Iterable#diff`
* `Serialization`:
* New operators `TypedSerializer#plusAssign` and `TypedSerializer#minusAssign`
## 0.5.24 ## 0.5.24

View File

@ -11,6 +11,7 @@ open class TypedSerializer<T : Any>(
presetSerializers: Map<String, KSerializer<out T>> = emptyMap(), presetSerializers: Map<String, KSerializer<out T>> = emptyMap(),
) : KSerializer<T> { ) : KSerializer<T> {
protected val serializers = presetSerializers.toMutableMap() protected val serializers = presetSerializers.toMutableMap()
@ExperimentalSerializationApi
@InternalSerializationApi @InternalSerializationApi
override val descriptor: SerialDescriptor = buildSerialDescriptor( override val descriptor: SerialDescriptor = buildSerialDescriptor(
"TypedSerializer", "TypedSerializer",
@ -32,8 +33,9 @@ open class TypedSerializer<T : Any>(
element("value", ContextualSerializer(kClass).descriptor) element("value", ContextualSerializer(kClass).descriptor)
} }
@ExperimentalSerializationApi
@InternalSerializationApi @InternalSerializationApi
open override fun deserialize(decoder: Decoder): T { override 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
@ -56,13 +58,15 @@ open class TypedSerializer<T : Any>(
} }
} }
@ExperimentalSerializationApi
@InternalSerializationApi @InternalSerializationApi
protected open 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)
} }
@ExperimentalSerializationApi
@InternalSerializationApi @InternalSerializationApi
open override fun serialize(encoder: Encoder, value: T) { override 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 }
@ -81,6 +85,16 @@ open class TypedSerializer<T : Any>(
} }
} }
@InternalSerializationApi
operator fun <T : Any> TypedSerializer<T>.plusAssign(kClass: KClass<T>) {
include(kClass.simpleName!!, kClass.serializer())
}
@InternalSerializationApi
operator fun <T : Any> TypedSerializer<T>.minusAssign(kClass: KClass<T>) {
exclude(kClass.simpleName!!)
}
inline fun <reified T : Any> TypedSerializer( inline fun <reified T : Any> TypedSerializer(
presetSerializers: Map<String, KSerializer<out T>> = emptyMap() presetSerializers: Map<String, KSerializer<out T>> = emptyMap()
) = TypedSerializer(T::class, presetSerializers) ) = TypedSerializer(T::class, presetSerializers)