1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-03 00:15:27 +00:00
tgbotapi/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButtonSerializer.kt

46 lines
2.5 KiB
Kotlin
Raw Normal View History

2019-06-02 14:02:12 +00:00
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons
import com.github.insanusmokrassar.TelegramBotAPI.types.*
2020-03-22 07:37:01 +00:00
import com.github.insanusmokrassar.TelegramBotAPI.utils.nonstrictJsonFormat
2019-06-02 14:02:12 +00:00
import kotlinx.serialization.*
2020-04-13 05:11:09 +00:00
import kotlinx.serialization.json.*
2019-06-02 14:02:12 +00:00
2020-01-15 07:03:42 +00:00
internal object InlineKeyboardButtonSerializer : KSerializer<InlineKeyboardButton> {
2020-03-22 07:37:01 +00:00
override val descriptor: SerialDescriptor = SerialDescriptor(
"com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons.InlineKeyboardButton",
PolymorphicKind.SEALED
)
2019-06-02 14:02:12 +00:00
2020-04-13 05:11:09 +00:00
private fun resolveSerializer(json: JsonObject): KSerializer<out InlineKeyboardButton>? {
return when {
json[callbackDataField] != null -> CallbackDataInlineKeyboardButton.serializer()
json[callbackGameField] != null -> CallbackGameInlineKeyboardButton.serializer()
json[loginUrlField] != null -> LoginURLInlineKeyboardButton.serializer()
json[payField] != null -> PayInlineKeyboardButton.serializer()
json[switchInlineQueryField] != null -> SwitchInlineQueryInlineKeyboardButton.serializer()
json[switchInlineQueryCurrentChatField] != null -> SwitchInlineQueryCurrentChatInlineKeyboardButton.serializer()
json[urlField] != null -> URLInlineKeyboardButton.serializer()
2020-04-13 05:11:09 +00:00
else -> null
}
}
2019-06-02 14:02:12 +00:00
override fun deserialize(decoder: Decoder): InlineKeyboardButton {
2020-04-13 05:11:09 +00:00
val json = JsonObjectSerializer.deserialize(decoder)
2019-06-02 14:02:12 +00:00
2020-04-13 05:11:09 +00:00
return resolveSerializer(json) ?.let {
nonstrictJsonFormat.fromJson(it, json)
} ?: UnknownInlineKeyboardButton(json[textField] ?.contentOrNull ?: "", json)
2019-06-02 14:02:12 +00:00
}
2020-03-22 07:37:01 +00:00
override fun serialize(encoder: Encoder, value: InlineKeyboardButton) {
when (value) {
is CallbackDataInlineKeyboardButton -> CallbackDataInlineKeyboardButton.serializer().serialize(encoder, value)
is LoginURLInlineKeyboardButton -> LoginURLInlineKeyboardButton.serializer().serialize(encoder, value)
is PayInlineKeyboardButton -> PayInlineKeyboardButton.serializer().serialize(encoder, value)
is SwitchInlineQueryInlineKeyboardButton -> SwitchInlineQueryInlineKeyboardButton.serializer().serialize(encoder, value)
is SwitchInlineQueryCurrentChatInlineKeyboardButton -> SwitchInlineQueryCurrentChatInlineKeyboardButton.serializer().serialize(encoder, value)
is URLInlineKeyboardButton -> URLInlineKeyboardButton.serializer().serialize(encoder, value)
2019-06-02 14:02:12 +00:00
}
}
}