1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-01 07:25:23 +00:00
tgbotapi/TelegramBotAPI-core/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/Update.kt

53 lines
1.8 KiB
Kotlin
Raw Normal View History

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
2020-03-22 07:37:01 +00:00
import com.github.insanusmokrassar.TelegramBotAPI.utils.nonstrictJsonFormat
2019-08-17 16:08:36 +00:00
import kotlinx.serialization.*
2020-08-18 06:50:11 +00:00
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonElement
2018-12-26 08:07:24 +00:00
interface Update {
2018-12-26 08:07:24 +00:00
val updateId: UpdateIdentifier
val data: Any
2018-12-26 08:07:24 +00:00
}
2019-08-17 16:08:36 +00:00
2020-06-27 03:43:24 +00:00
data class UnknownUpdate(
2020-01-22 21:35:56 +00:00
override val updateId: UpdateIdentifier,
override val data: String,
val rawJson: JsonElement
2020-01-22 21:35:56 +00:00
) : Update
internal object UpdateSerializerWithoutSerialization : KSerializer<Update> {
2020-08-18 06:50:11 +00:00
override val descriptor: SerialDescriptor = JsonElement.serializer().descriptor
2019-08-17 16:08:36 +00:00
override fun deserialize(decoder: Decoder): Update = UpdateDeserializationStrategy.deserialize(decoder)
2020-03-22 07:37:01 +00:00
override fun serialize(encoder: Encoder, value: Update) = throw UnsupportedOperationException()
2019-08-17 16:08:36 +00:00
}
/**
* Use this object to deserialize objects with type [Update]. Currently it is restricted to use this
* [DeserializationStrategy] only with JSON
*
* @see StringFormat.parse
* @see kotlinx.serialization.json.Json.parse
*/
object UpdateDeserializationStrategy : DeserializationStrategy<Update> {
2020-08-18 06:50:11 +00:00
override val descriptor: SerialDescriptor = JsonElement.serializer().descriptor
2019-08-17 16:08:36 +00:00
override fun deserialize(decoder: Decoder): Update {
2020-08-18 06:50:11 +00:00
val asJson = JsonElement.serializer().deserialize(decoder)
return nonstrictJsonFormat.decodeFromJsonElement(
2020-01-22 21:31:56 +00:00
RawUpdate.serializer(),
asJson
).asUpdate(
2020-01-22 22:01:24 +00:00
asJson
2020-01-22 21:31:56 +00:00
)
2019-08-17 16:08:36 +00:00
}
2020-08-18 06:50:11 +00:00
override fun patch(decoder: Decoder, old: Update): Update = error("Unsupported operation")
2019-08-17 16:08:36 +00:00
}