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-04-13 05:17:15 +00:00
|
|
|
import kotlinx.serialization.json.JsonElement
|
2020-01-22 21:31:56 +00:00
|
|
|
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
|
|
|
|
2020-06-27 03:43:24 +00:00
|
|
|
data class UnknownUpdate(
|
2020-01-22 21:35:56 +00:00
|
|
|
override val updateId: UpdateIdentifier,
|
2020-04-13 05:17:15 +00:00
|
|
|
override val data: String,
|
|
|
|
val rawJson: JsonElement
|
2020-01-22 21:35:56 +00:00
|
|
|
) : Update
|
2020-06-27 03:43:24 +00:00
|
|
|
@Deprecated("Renamed", ReplaceWith("UnknownUpdate"))
|
|
|
|
typealias UnknownUpdateType = UnknownUpdate
|
2020-01-22 21:35:56 +00:00
|
|
|
|
2020-04-22 07:20:15 +00:00
|
|
|
internal object UpdateSerializerWithoutSerialization : KSerializer<Update> {
|
2020-03-22 07:37:01 +00:00
|
|
|
override val descriptor: SerialDescriptor = JsonElementSerializer.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
|
|
|
}
|
|
|
|
|
2020-05-13 13:53:27 +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-03-22 07:37:01 +00:00
|
|
|
override val descriptor: SerialDescriptor = JsonElementSerializer.descriptor
|
2019-08-17 16:08:36 +00:00
|
|
|
|
2020-03-22 07:37:01 +00:00
|
|
|
override fun patch(decoder: Decoder, old: Update): Update = throw UpdateNotSupportedException("Update")
|
2019-08-17 16:08:36 +00:00
|
|
|
|
|
|
|
override fun deserialize(decoder: Decoder): Update {
|
2020-01-22 21:31:56 +00:00
|
|
|
val asJson = JsonElementSerializer.deserialize(decoder)
|
2020-03-22 07:37:01 +00:00
|
|
|
return nonstrictJsonFormat.fromJson(
|
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
|
|
|
}
|
|
|
|
}
|