From 46e6eeca9d643d57354455ff55fe98c6e0965fa4 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 11 Oct 2023 13:38:09 +0600 Subject: [PATCH] improve serializers --- .../dev/inmo/tgbotapi/types/chat/Abstracts.kt | 22 +++++----- .../tgbotapi/types/chat/ChatSerializers.kt | 44 +++++++++---------- .../dev/inmo/tgbotapi/types/chat/Extended.kt | 6 ++- 3 files changed, 36 insertions(+), 36 deletions(-) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Abstracts.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Abstracts.kt index d0d497e768..e2e85645ef 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Abstracts.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Abstracts.kt @@ -4,51 +4,51 @@ import dev.inmo.tgbotapi.utils.internal.ClassCastsIncluded import dev.inmo.tgbotapi.types.* import kotlinx.serialization.Serializable -@Serializable(PreviewChatSerializer::class) +@Serializable(ChatSerializer::class) sealed interface UsernameChat : Chat { val username: Username? } -@Serializable(PreviewChatSerializer::class) +@Serializable(ChatSerializer::class) sealed interface PrivateChat : Chat, UsernameChat { override val id: UserId val firstName: String val lastName: String } -@Serializable(PreviewChatSerializer::class) +@Serializable(ChatSerializer::class) sealed interface PublicChat : Chat { val title: String } -@Serializable(PreviewChatSerializer::class) +@Serializable(ChatSerializer::class) sealed interface SuperPublicChat : PublicChat, UsernameChat -@Serializable(PreviewChatSerializer::class) +@Serializable(ChatSerializer::class) sealed interface ChannelChat : SuperPublicChat { override val id: ChatId } -@Serializable(PreviewChatSerializer::class) +@Serializable(ChatSerializer::class) sealed interface GroupChat : PublicChat -@Serializable(PreviewChatSerializer::class) +@Serializable(ChatSerializer::class) sealed interface SupergroupChat : GroupChat, SuperPublicChat -@Serializable(PreviewChatSerializer::class) +@Serializable(ChatSerializer::class) sealed interface ForumChat : SupergroupChat -@Serializable(PreviewChatSerializer::class) +@Serializable(ChatSerializer::class) sealed interface PossiblyPremiumChat : Chat { val isPremium: Boolean } -@Serializable(PreviewChatSerializer::class) +@Serializable(ChatSerializer::class) sealed interface AbleToAddInAttachmentMenuChat : Chat { val addedToAttachmentMenu: Boolean } -@Serializable(PreviewChatSerializer::class) +@Serializable(ChatSerializer::class) @ClassCastsIncluded(excludeRegex = ".*Impl") sealed interface Chat { val id: IdChatIdentifier diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ChatSerializers.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ChatSerializers.kt index 989979c3e8..80433c7420 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ChatSerializers.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ChatSerializers.kt @@ -56,38 +56,35 @@ object ChatSerializer : KSerializer { override fun deserialize(decoder: Decoder): Chat { val decodedJson = JsonObject.serializer().deserialize(decoder) - val type = decodedJson[typeField] ?.jsonPrimitive ?.content ?.asChatType ?: error("Field $typeField must be presented, but absent in $decodedJson") - val isForum = decodedJson[isForumField] ?.jsonPrimitive ?.booleanOrNull == true + return try { + formatter.decodeFromJsonElement(ExtendedChatSerializer, decodedJson) + } catch (e: SerializationException) { + val type = decodedJson[typeField] ?.jsonPrimitive ?.content ?.asChatType ?: error("Field $typeField must be presented, but absent in $decodedJson") + val isForum = decodedJson[isForumField] ?.jsonPrimitive ?.booleanOrNull == true - return when (type) { - ChatType.PrivateChatType -> formatter.decodeFromJsonElement(PrivateChatImpl.serializer(), decodedJson) - ChatType.GroupChatType -> formatter.decodeFromJsonElement(GroupChatImpl.serializer(), decodedJson) - ChatType.SupergroupChatType -> if (isForum) { - formatter.decodeFromJsonElement(ForumChatImpl.serializer(), decodedJson) - } else { - formatter.decodeFromJsonElement(SupergroupChatImpl.serializer(), decodedJson) + when (type) { + ChatType.PrivateChatType -> formatter.decodeFromJsonElement(PrivateChatImpl.serializer(), decodedJson) + ChatType.GroupChatType -> formatter.decodeFromJsonElement(GroupChatImpl.serializer(), decodedJson) + ChatType.SupergroupChatType -> if (isForum) { + formatter.decodeFromJsonElement(ForumChatImpl.serializer(), decodedJson) + } else { + formatter.decodeFromJsonElement(SupergroupChatImpl.serializer(), decodedJson) + } + ChatType.ChannelChatType -> formatter.decodeFromJsonElement(ChannelChatImpl.serializer(), decodedJson) + is ChatType.UnknownChatType -> UnknownChatType( + formatter.decodeFromJsonElement(Long.serializer(), decodedJson[chatIdField] ?: JsonPrimitive(-1)).toChatId(), + decodedJson.toString(), + decodedJson + ) } - ChatType.ChannelChatType -> formatter.decodeFromJsonElement(ChannelChatImpl.serializer(), decodedJson) - is ChatType.UnknownChatType -> UnknownChatType( - formatter.decodeFromJsonElement(Long.serializer(), decodedJson[chatIdField] ?: JsonPrimitive(-1)).toChatId(), - decodedJson.toString(), - decodedJson - ) } } override fun serialize(encoder: Encoder, value: Chat) { when (value) { is ExtendedChat -> ExtendedChatSerializer.serialize(encoder, value) - is PrivateChatImpl -> PrivateChatImpl.serializer().serialize(encoder, value) - is GroupChatImpl -> GroupChatImpl.serializer().serialize(encoder, value) - is SupergroupChatImpl -> SupergroupChatImpl.serializer().serialize(encoder, value) - is ForumChatImpl -> ForumChatImpl.serializer().serialize(encoder, value) - is ChannelChatImpl -> ChannelChatImpl.serializer().serialize(encoder, value) - is CommonBot -> CommonBot.serializer().serialize(encoder, value) + is PreviewChat -> PreviewChatSerializer.serialize(encoder, value) is ExtendedBot -> ExtendedBot.serializer().serialize(encoder, value) - is CommonUser -> CommonUser.serializer().serialize(encoder, value) - is UnknownChatType -> JsonObject.serializer().serialize(encoder, value.rawJson) } } } @@ -170,6 +167,7 @@ sealed class ExtendedChatSerializer : KSerializer { is ExtendedSupergroupChatImpl -> ExtendedSupergroupChatImpl.serializer().serialize(encoder, value) is ExtendedForumChatImpl -> ExtendedForumChatImpl.serializer().serialize(encoder, value) is ExtendedChannelChatImpl -> ExtendedChannelChatImpl.serializer().serialize(encoder, value) + is ExtendedBot -> ExtendedBot.serializer().serialize(encoder, value) is UnknownExtendedChat -> JsonObject.serializer().serialize(encoder, value.rawJson) } } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt index 1cc41715e5..91d502f931 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt @@ -184,8 +184,10 @@ data class ExtendedBot( @SerialName(canReadAllGroupMessagesField) val canReadAllGroupMessages: Boolean = false, @SerialName(supportInlineQueriesField) - val supportsInlineQueries: Boolean = false -) : Bot() { + val supportsInlineQueries: Boolean = false, + @SerialName(photoField) + override val chatPhoto: ChatPhoto? = null +) : Bot(), ExtendedChat { @SerialName(isBotField) private val isBot = true }