mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-21 08:07:16 +00:00
TypedSerializer#plusAssign and TypedSerializer#minusAssign
This commit is contained in:
parent
ef9af71960
commit
a24a335743
@ -8,6 +8,8 @@
|
||||
* New property `MPPFile#withoutSlashAtTheEnd`
|
||||
* Extension `clamp` has been deprecated
|
||||
* New extension `Iterable#diff`
|
||||
* `Serialization`:
|
||||
* New operators `TypedSerializer#plusAssign` and `TypedSerializer#minusAssign`
|
||||
|
||||
## 0.5.24
|
||||
|
||||
|
@ -11,6 +11,7 @@ open class TypedSerializer<T : Any>(
|
||||
presetSerializers: Map<String, KSerializer<out T>> = emptyMap(),
|
||||
) : KSerializer<T> {
|
||||
protected val serializers = presetSerializers.toMutableMap()
|
||||
@ExperimentalSerializationApi
|
||||
@InternalSerializationApi
|
||||
override val descriptor: SerialDescriptor = buildSerialDescriptor(
|
||||
"TypedSerializer",
|
||||
@ -32,8 +33,9 @@ open class TypedSerializer<T : Any>(
|
||||
element("value", ContextualSerializer(kClass).descriptor)
|
||||
}
|
||||
|
||||
@ExperimentalSerializationApi
|
||||
@InternalSerializationApi
|
||||
open override fun deserialize(decoder: Decoder): T {
|
||||
override fun deserialize(decoder: Decoder): T {
|
||||
return decoder.decodeStructure(descriptor) {
|
||||
var type: String? = null
|
||||
lateinit var result: T
|
||||
@ -56,13 +58,15 @@ open class TypedSerializer<T : Any>(
|
||||
}
|
||||
}
|
||||
|
||||
@ExperimentalSerializationApi
|
||||
@InternalSerializationApi
|
||||
protected open fun <O: T> CompositeEncoder.encode(value: O) {
|
||||
encodeSerializableElement(descriptor, 1, value::class.serializer() as KSerializer<O>, value)
|
||||
}
|
||||
|
||||
@ExperimentalSerializationApi
|
||||
@InternalSerializationApi
|
||||
open override fun serialize(encoder: Encoder, value: T) {
|
||||
override fun serialize(encoder: Encoder, value: T) {
|
||||
encoder.encodeStructure(descriptor) {
|
||||
val valueSerializer = value::class.serializer()
|
||||
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(
|
||||
presetSerializers: Map<String, KSerializer<out T>> = emptyMap()
|
||||
) = TypedSerializer(T::class, presetSerializers)
|
||||
|
Loading…
Reference in New Issue
Block a user