mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-12-23 00:57:13 +00:00
optimize deserialization of InlineKeyboardButton's
This commit is contained in:
parent
5e8bf82358
commit
ae7365e9e9
@ -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"
|
||||
|
@ -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()
|
||||
|
@ -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<InlineKeyboardButton> {
|
||||
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<out InlineKeyboardButton> {
|
||||
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) {
|
||||
|
Loading…
Reference in New Issue
Block a user