From 6273990b003e67f4e4e4dfce2b0614b63282c0fa Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 24 Sep 2023 21:33:26 +0600 Subject: [PATCH] fix in chat member deserializing --- .../tgbotapi/types/chat/member/ChatMember.kt | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/ChatMember.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/ChatMember.kt index e8de6e9608..d8ea2691e6 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/ChatMember.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/ChatMember.kt @@ -4,6 +4,7 @@ import dev.inmo.tgbotapi.abstracts.WithUser import dev.inmo.tgbotapi.types.statusField import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.nonstrictJsonFormat +import kotlinx.serialization.DeserializationStrategy import kotlinx.serialization.KSerializer import kotlinx.serialization.Serializable import kotlinx.serialization.descriptors.SerialDescriptor @@ -15,13 +16,13 @@ import kotlinx.serialization.json.jsonPrimitive @Serializable(ChatMemberSerializer::class) sealed interface ChatMember : WithUser { @Serializable - enum class Status(val status: String) { - Creator("creator"), - Administrator("administrator"), - Member("member"), - Restricted("restricted"), - Left("left"), - Kicked("kicked") + enum class Status(val status: String, val deserializationStrategy: DeserializationStrategy) { + Creator("creator", OwnerChatMember.serializer()), + Administrator("administrator", AdministratorChatMemberImpl.serializer()), + Member("member", MemberChatMemberImpl.serializer()), + Restricted("restricted", RestrictedChatMember.serializer()), + Left("left", LeftChatMemberImpl.serializer()), + Kicked("kicked", KickedChatMember.serializer()) } val status: Status } @@ -32,17 +33,14 @@ object ChatMemberSerializer : KSerializer { override fun deserialize(decoder: Decoder): ChatMember { val json = JsonObject.serializer().deserialize(decoder) - val status = ChatMember.Status.valueOf( - json[statusField] ?.jsonPrimitive ?.content ?: error("Status field of chat member must be specified, but incoming json contains next: $json") - ) - return when (status) { - ChatMember.Status.Creator -> nonstrictJsonFormat.decodeFromJsonElement(OwnerChatMember.serializer(), json) - ChatMember.Status.Administrator -> nonstrictJsonFormat.decodeFromJsonElement(AdministratorChatMemberImpl.serializer(), json) - ChatMember.Status.Member -> nonstrictJsonFormat.decodeFromJsonElement(MemberChatMemberImpl.serializer(), json) - ChatMember.Status.Restricted -> nonstrictJsonFormat.decodeFromJsonElement(RestrictedChatMember.serializer(), json) - ChatMember.Status.Left -> nonstrictJsonFormat.decodeFromJsonElement(LeftChatMemberImpl.serializer(), json) - ChatMember.Status.Kicked -> nonstrictJsonFormat.decodeFromJsonElement(KickedChatMember.serializer(), json) - } + val status = json[statusField] ?.jsonPrimitive ?.content ?: error("Status field of chat member must be specified, but incoming json contains next: $json") + return ChatMember.Status.values().firstNotNullOfOrNull { + if (it.status == status) { + nonstrictJsonFormat.decodeFromJsonElement(it.deserializationStrategy, json) + } else { + null + } + } ?: error("Unknown type of chat member in json: $json") } override fun serialize(encoder: Encoder, value: ChatMember) {