2018-12-26 08:07:24 +00:00
|
|
|
package com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts
|
|
|
|
|
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
|
2019-08-17 16:08:36 +00:00
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.types.update.RawUpdate
|
|
|
|
import kotlinx.serialization.*
|
|
|
|
import kotlinx.serialization.internal.StringDescriptor
|
2020-01-22 21:31:56 +00:00
|
|
|
import kotlinx.serialization.json.Json
|
|
|
|
import kotlinx.serialization.json.JsonElementSerializer
|
2018-12-26 08:07:24 +00:00
|
|
|
|
2019-01-21 06:13:31 +00:00
|
|
|
interface Update {
|
2018-12-26 08:07:24 +00:00
|
|
|
val updateId: UpdateIdentifier
|
2019-01-21 06:13:31 +00:00
|
|
|
val data: Any
|
2018-12-26 08:07:24 +00:00
|
|
|
}
|
2019-08-17 16:08:36 +00:00
|
|
|
|
2019-08-17 16:29:42 +00:00
|
|
|
internal object UpdateSerializerWithoutDeserialization : KSerializer<Update> {
|
2019-08-17 16:08:36 +00:00
|
|
|
override val descriptor: SerialDescriptor = StringDescriptor.withName("UpdateSerializerWithoutDeserialization")
|
|
|
|
|
|
|
|
override fun deserialize(decoder: Decoder): Update = UpdateDeserializationStrategy.deserialize(decoder)
|
|
|
|
|
|
|
|
override fun serialize(encoder: Encoder, obj: Update) = throw UnsupportedOperationException()
|
|
|
|
}
|
|
|
|
|
2019-08-17 16:29:42 +00:00
|
|
|
internal object UpdateDeserializationStrategy : DeserializationStrategy<Update> {
|
2019-08-17 16:08:36 +00:00
|
|
|
override val descriptor: SerialDescriptor = StringDescriptor.withName("UpdateDeserializationStrategy")
|
|
|
|
|
|
|
|
override fun patch(decoder: Decoder, old: Update): Update = throw UpdateNotSupportedException(descriptor.name)
|
|
|
|
|
|
|
|
override fun deserialize(decoder: Decoder): Update {
|
2020-01-22 21:31:56 +00:00
|
|
|
val asJson = JsonElementSerializer.deserialize(decoder)
|
|
|
|
return Json.nonstrict.fromJson(
|
|
|
|
RawUpdate.serializer(),
|
|
|
|
asJson
|
|
|
|
).asUpdate(
|
|
|
|
asJson.toString()
|
|
|
|
)
|
2019-08-17 16:08:36 +00:00
|
|
|
}
|
|
|
|
}
|