mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 08:13:47 +00:00
updates in TextSourceSerializer
This commit is contained in:
parent
c0451d4c8f
commit
9982534001
@ -5,7 +5,7 @@
|
|||||||
* `Common`:
|
* `Common`:
|
||||||
* `Version`:
|
* `Version`:
|
||||||
* `uuid`: `0.2.3` -> `0.2.4`
|
* `uuid`: `0.2.3` -> `0.2.4`
|
||||||
* `MicroUtils`: `0.4.33` -> `0.4.34`
|
* `MicroUtils`: `0.4.33` -> `0.4.35`
|
||||||
* `Core`:
|
* `Core`:
|
||||||
* All `TextSource` implementators have become `Serializable`
|
* All `TextSource` implementators have become `Serializable`
|
||||||
* New serializer `TextSourceSerializer`
|
* New serializer `TextSourceSerializer`
|
||||||
|
@ -12,7 +12,7 @@ klock_version=2.0.7
|
|||||||
uuid_version=0.2.4
|
uuid_version=0.2.4
|
||||||
ktor_version=1.5.3
|
ktor_version=1.5.3
|
||||||
|
|
||||||
micro_utils_version=0.4.34
|
micro_utils_version=0.4.35
|
||||||
|
|
||||||
javax_activation_version=1.1.1
|
javax_activation_version=1.1.1
|
||||||
|
|
||||||
|
@ -52,6 +52,7 @@ kotlin {
|
|||||||
api "dev.inmo:micro_utils.coroutines:$micro_utils_version"
|
api "dev.inmo:micro_utils.coroutines:$micro_utils_version"
|
||||||
api "dev.inmo:micro_utils.serialization.base64:$micro_utils_version"
|
api "dev.inmo:micro_utils.serialization.base64:$micro_utils_version"
|
||||||
api "dev.inmo:micro_utils.serialization.encapsulator:$micro_utils_version"
|
api "dev.inmo:micro_utils.serialization.encapsulator:$micro_utils_version"
|
||||||
|
api "dev.inmo:micro_utils.serialization.typed_serializer:$micro_utils_version"
|
||||||
|
|
||||||
api "io.ktor:ktor-client-core:$ktor_version"
|
api "io.ktor:ktor-client-core:$ktor_version"
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package dev.inmo.tgbotapi.types.MessageEntity.textsources
|
package dev.inmo.tgbotapi.types.MessageEntity.textsources
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.serialization.typed_serializer.TypedSerializer
|
||||||
import dev.inmo.tgbotapi.CommonAbstracts.TextSource
|
import dev.inmo.tgbotapi.CommonAbstracts.TextSource
|
||||||
import dev.inmo.tgbotapi.CommonAbstracts.justTextSources
|
import dev.inmo.tgbotapi.CommonAbstracts.justTextSources
|
||||||
import dev.inmo.tgbotapi.types.MessageEntity.*
|
import dev.inmo.tgbotapi.types.MessageEntity.*
|
||||||
@ -30,63 +31,14 @@ private val baseSerializers: Map<String, KSerializer<out TextSource>> = mapOf(
|
|||||||
)
|
)
|
||||||
|
|
||||||
@Serializer(TextSource::class)
|
@Serializer(TextSource::class)
|
||||||
object TextSourceSerializer : KSerializer<TextSource> {
|
object TextSourceSerializer : TypedSerializer<TextSource>(TextSource::class, baseSerializers) {
|
||||||
private val serializers = baseSerializers.toMutableMap()
|
override fun <T: TextSource> include(type: String, serializer: KSerializer<T>) {
|
||||||
@InternalSerializationApi
|
|
||||||
override val descriptor: SerialDescriptor = buildSerialDescriptor(
|
|
||||||
"TextSourceSerializer",
|
|
||||||
SerialKind.CONTEXTUAL
|
|
||||||
) {
|
|
||||||
element("type", String.serializer().descriptor)
|
|
||||||
element("value", ContextualSerializer(TextSource::class).descriptor)
|
|
||||||
}
|
|
||||||
|
|
||||||
@InternalSerializationApi
|
|
||||||
override fun deserialize(decoder: Decoder): TextSource {
|
|
||||||
return decoder.decodeStructure(descriptor) {
|
|
||||||
var type: String? = null
|
|
||||||
lateinit var result: TextSource
|
|
||||||
while (true) {
|
|
||||||
when (val index = decodeElementIndex(descriptor)) {
|
|
||||||
0 -> type = decodeStringElement(descriptor, 0)
|
|
||||||
1 -> {
|
|
||||||
require(type != null) { "Type is null, but it is expected that was inited already" }
|
|
||||||
result = decodeSerializableElement(
|
|
||||||
descriptor,
|
|
||||||
1,
|
|
||||||
serializers.getValue(type)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
CompositeDecoder.DECODE_DONE -> break
|
|
||||||
else -> error("Unexpected index: $index")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@InternalSerializationApi
|
|
||||||
private fun <T : TextSource> CompositeEncoder.encode(value: T) {
|
|
||||||
encodeSerializableElement(descriptor, 1, value::class.serializer() as KSerializer<T>, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
@InternalSerializationApi
|
|
||||||
override fun serialize(encoder: Encoder, value: TextSource) {
|
|
||||||
encoder.encodeStructure(descriptor) {
|
|
||||||
val valueSerializer = value::class.serializer()
|
|
||||||
val type = serializers.keys.first { serializers[it] == valueSerializer }
|
|
||||||
encodeStringElement(descriptor, 0, type)
|
|
||||||
encode(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun <T: TextSource> include(type: String, serializer: KSerializer<T>) {
|
|
||||||
require(type !in baseSerializers.keys)
|
require(type !in baseSerializers.keys)
|
||||||
serializers[type] = serializer
|
super.include(type, serializer)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun exclude(type: String) {
|
override fun exclude(type: String) {
|
||||||
require(type !in baseSerializers.keys)
|
require(type !in baseSerializers.keys)
|
||||||
serializers.remove(type)
|
super.exclude(type)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user