diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt index 6ea61adc2b..d6ce1db4da 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt @@ -88,6 +88,7 @@ const val loginUrlField = "login_url" const val forwardTextField = "forward_text" const val botUsernameField = "bot_username" const val switchInlineQueryCurrentChatField = "switch_inline_query_current_chat" +const val switchInlineQueryField = "switch_inline_query" const val requestWriteAccessField = "request_write_access" @@ -199,6 +200,7 @@ const val resultsField = "results" const val certificateField = "certificate" const val questionField = "question" const val optionsField = "options" +const val payField = "pay" const val pointField = "point" const val xShiftField = "x_shift" diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButton.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButton.kt index 22b1a27dad..26809e7284 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButton.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButton.kt @@ -12,6 +12,7 @@ sealed class InlineKeyboardButton { @Serializable data class PayInlineKeyboardButton( override val text: String, + @SerialName(payField) val pay: Boolean ) : InlineKeyboardButton() @@ -40,12 +41,13 @@ data class SwitchInlineQueryCurrentChatInlineKeyboardButton( @Serializable data class SwitchInlineQueryInlineKeyboardButton( override val text: String, - @SerialName("switch_inline_query") + @SerialName(switchInlineQueryField) val switchInlineQuery: String ) : InlineKeyboardButton() @Serializable data class URLInlineKeyboardButton( override val text: String, + @SerialName(urlField) val url: String ) : InlineKeyboardButton() diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButtonSerializer.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButtonSerializer.kt index 2f289e39a0..4ee83f2133 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButtonSerializer.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButtonSerializer.kt @@ -1,34 +1,29 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons +import com.github.insanusmokrassar.TelegramBotAPI.types.* import kotlinx.serialization.* import kotlinx.serialization.internal.StringDescriptor -import kotlinx.serialization.json.Json -import kotlinx.serialization.json.JsonElementSerializer +import kotlinx.serialization.json.* object InlineKeyboardButtonSerializer : KSerializer { override val descriptor: SerialDescriptor = StringDescriptor.withName("com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons.InlineKeyboardButton") - private val serializers = listOf( - CallbackDataInlineKeyboardButton.serializer(), - LoginURLInlineKeyboardButton.serializer(), - PayInlineKeyboardButton.serializer(), - SwitchInlineQueryInlineKeyboardButton.serializer(), - SwitchInlineQueryCurrentChatInlineKeyboardButton.serializer(), - URLInlineKeyboardButton.serializer() - ) + private fun resolveSerializer(json: JsonObject): KSerializer { + return when { + json[callbackDataField] != null -> CallbackDataInlineKeyboardButton.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() + else -> throw IllegalArgumentException("Can't find correct serializer for inline button serialized as $json") + } + } override fun deserialize(decoder: Decoder): InlineKeyboardButton { val json = JsonElementSerializer.deserialize(decoder) - serializers.forEach { - try { - return Json.nonstrict.fromJson(it, json) - } catch (e: SerializationException) { - e - } - } - - throw IllegalArgumentException("There is no known type of serializer for \"$json\" as inline button") + return Json.nonstrict.fromJson(resolveSerializer(json.jsonObject), json) } override fun serialize(encoder: Encoder, obj: InlineKeyboardButton) {