1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 16:23:48 +00:00

optimize deserialization of InlineKeyboardButton's

This commit is contained in:
InsanusMokrassar 2019-06-02 22:10:04 +08:00
parent 5e8bf82358
commit ae7365e9e9
3 changed files with 19 additions and 20 deletions

View File

@ -88,6 +88,7 @@ const val loginUrlField = "login_url"
const val forwardTextField = "forward_text" const val forwardTextField = "forward_text"
const val botUsernameField = "bot_username" const val botUsernameField = "bot_username"
const val switchInlineQueryCurrentChatField = "switch_inline_query_current_chat" const val switchInlineQueryCurrentChatField = "switch_inline_query_current_chat"
const val switchInlineQueryField = "switch_inline_query"
const val requestWriteAccessField = "request_write_access" const val requestWriteAccessField = "request_write_access"
@ -199,6 +200,7 @@ const val resultsField = "results"
const val certificateField = "certificate" const val certificateField = "certificate"
const val questionField = "question" const val questionField = "question"
const val optionsField = "options" const val optionsField = "options"
const val payField = "pay"
const val pointField = "point" const val pointField = "point"
const val xShiftField = "x_shift" const val xShiftField = "x_shift"

View File

@ -12,6 +12,7 @@ sealed class InlineKeyboardButton {
@Serializable @Serializable
data class PayInlineKeyboardButton( data class PayInlineKeyboardButton(
override val text: String, override val text: String,
@SerialName(payField)
val pay: Boolean val pay: Boolean
) : InlineKeyboardButton() ) : InlineKeyboardButton()
@ -40,12 +41,13 @@ data class SwitchInlineQueryCurrentChatInlineKeyboardButton(
@Serializable @Serializable
data class SwitchInlineQueryInlineKeyboardButton( data class SwitchInlineQueryInlineKeyboardButton(
override val text: String, override val text: String,
@SerialName("switch_inline_query") @SerialName(switchInlineQueryField)
val switchInlineQuery: String val switchInlineQuery: String
) : InlineKeyboardButton() ) : InlineKeyboardButton()
@Serializable @Serializable
data class URLInlineKeyboardButton( data class URLInlineKeyboardButton(
override val text: String, override val text: String,
@SerialName(urlField)
val url: String val url: String
) : InlineKeyboardButton() ) : InlineKeyboardButton()

View File

@ -1,34 +1,29 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import kotlinx.serialization.* import kotlinx.serialization.*
import kotlinx.serialization.internal.StringDescriptor import kotlinx.serialization.internal.StringDescriptor
import kotlinx.serialization.json.Json import kotlinx.serialization.json.*
import kotlinx.serialization.json.JsonElementSerializer
object InlineKeyboardButtonSerializer : KSerializer<InlineKeyboardButton> { object InlineKeyboardButtonSerializer : KSerializer<InlineKeyboardButton> {
override val descriptor: SerialDescriptor = StringDescriptor.withName("com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons.InlineKeyboardButton") override val descriptor: SerialDescriptor = StringDescriptor.withName("com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons.InlineKeyboardButton")
private val serializers = listOf( private fun resolveSerializer(json: JsonObject): KSerializer<out InlineKeyboardButton> {
CallbackDataInlineKeyboardButton.serializer(), return when {
LoginURLInlineKeyboardButton.serializer(), json[callbackDataField] != null -> CallbackDataInlineKeyboardButton.serializer()
PayInlineKeyboardButton.serializer(), json[loginUrlField] != null -> LoginURLInlineKeyboardButton.serializer()
SwitchInlineQueryInlineKeyboardButton.serializer(), json[payField] != null -> PayInlineKeyboardButton.serializer()
SwitchInlineQueryCurrentChatInlineKeyboardButton.serializer(), json[switchInlineQueryField] != null -> SwitchInlineQueryInlineKeyboardButton.serializer()
URLInlineKeyboardButton.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 { override fun deserialize(decoder: Decoder): InlineKeyboardButton {
val json = JsonElementSerializer.deserialize(decoder) val json = JsonElementSerializer.deserialize(decoder)
serializers.forEach { return Json.nonstrict.fromJson(resolveSerializer(json.jsonObject), json)
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")
} }
override fun serialize(encoder: Encoder, obj: InlineKeyboardButton) { override fun serialize(encoder: Encoder, obj: InlineKeyboardButton) {