1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-26 03:58:44 +00:00

improve serializers

This commit is contained in:
InsanusMokrassar 2023-10-11 13:38:09 +06:00
parent 80be86454d
commit 46e6eeca9d
3 changed files with 36 additions and 36 deletions

View File

@ -4,51 +4,51 @@ import dev.inmo.tgbotapi.utils.internal.ClassCastsIncluded
import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@Serializable(PreviewChatSerializer::class) @Serializable(ChatSerializer::class)
sealed interface UsernameChat : Chat { sealed interface UsernameChat : Chat {
val username: Username? val username: Username?
} }
@Serializable(PreviewChatSerializer::class) @Serializable(ChatSerializer::class)
sealed interface PrivateChat : Chat, UsernameChat { sealed interface PrivateChat : Chat, UsernameChat {
override val id: UserId override val id: UserId
val firstName: String val firstName: String
val lastName: String val lastName: String
} }
@Serializable(PreviewChatSerializer::class) @Serializable(ChatSerializer::class)
sealed interface PublicChat : Chat { sealed interface PublicChat : Chat {
val title: String val title: String
} }
@Serializable(PreviewChatSerializer::class) @Serializable(ChatSerializer::class)
sealed interface SuperPublicChat : PublicChat, UsernameChat sealed interface SuperPublicChat : PublicChat, UsernameChat
@Serializable(PreviewChatSerializer::class) @Serializable(ChatSerializer::class)
sealed interface ChannelChat : SuperPublicChat { sealed interface ChannelChat : SuperPublicChat {
override val id: ChatId override val id: ChatId
} }
@Serializable(PreviewChatSerializer::class) @Serializable(ChatSerializer::class)
sealed interface GroupChat : PublicChat sealed interface GroupChat : PublicChat
@Serializable(PreviewChatSerializer::class) @Serializable(ChatSerializer::class)
sealed interface SupergroupChat : GroupChat, SuperPublicChat sealed interface SupergroupChat : GroupChat, SuperPublicChat
@Serializable(PreviewChatSerializer::class) @Serializable(ChatSerializer::class)
sealed interface ForumChat : SupergroupChat sealed interface ForumChat : SupergroupChat
@Serializable(PreviewChatSerializer::class) @Serializable(ChatSerializer::class)
sealed interface PossiblyPremiumChat : Chat { sealed interface PossiblyPremiumChat : Chat {
val isPremium: Boolean val isPremium: Boolean
} }
@Serializable(PreviewChatSerializer::class) @Serializable(ChatSerializer::class)
sealed interface AbleToAddInAttachmentMenuChat : Chat { sealed interface AbleToAddInAttachmentMenuChat : Chat {
val addedToAttachmentMenu: Boolean val addedToAttachmentMenu: Boolean
} }
@Serializable(PreviewChatSerializer::class) @Serializable(ChatSerializer::class)
@ClassCastsIncluded(excludeRegex = ".*Impl") @ClassCastsIncluded(excludeRegex = ".*Impl")
sealed interface Chat { sealed interface Chat {
val id: IdChatIdentifier val id: IdChatIdentifier

View File

@ -56,38 +56,35 @@ object ChatSerializer : KSerializer<Chat> {
override fun deserialize(decoder: Decoder): Chat { override fun deserialize(decoder: Decoder): Chat {
val decodedJson = JsonObject.serializer().deserialize(decoder) val decodedJson = JsonObject.serializer().deserialize(decoder)
val type = decodedJson[typeField] ?.jsonPrimitive ?.content ?.asChatType ?: error("Field $typeField must be presented, but absent in $decodedJson") return try {
val isForum = decodedJson[isForumField] ?.jsonPrimitive ?.booleanOrNull == true 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) { when (type) {
ChatType.PrivateChatType -> formatter.decodeFromJsonElement(PrivateChatImpl.serializer(), decodedJson) ChatType.PrivateChatType -> formatter.decodeFromJsonElement(PrivateChatImpl.serializer(), decodedJson)
ChatType.GroupChatType -> formatter.decodeFromJsonElement(GroupChatImpl.serializer(), decodedJson) ChatType.GroupChatType -> formatter.decodeFromJsonElement(GroupChatImpl.serializer(), decodedJson)
ChatType.SupergroupChatType -> if (isForum) { ChatType.SupergroupChatType -> if (isForum) {
formatter.decodeFromJsonElement(ForumChatImpl.serializer(), decodedJson) formatter.decodeFromJsonElement(ForumChatImpl.serializer(), decodedJson)
} else { } else {
formatter.decodeFromJsonElement(SupergroupChatImpl.serializer(), decodedJson) 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) { override fun serialize(encoder: Encoder, value: Chat) {
when (value) { when (value) {
is ExtendedChat -> ExtendedChatSerializer.serialize(encoder, value) is ExtendedChat -> ExtendedChatSerializer.serialize(encoder, value)
is PrivateChatImpl -> PrivateChatImpl.serializer().serialize(encoder, value) is PreviewChat -> PreviewChatSerializer.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 ExtendedBot -> ExtendedBot.serializer().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<ExtendedChat> {
is ExtendedSupergroupChatImpl -> ExtendedSupergroupChatImpl.serializer().serialize(encoder, value) is ExtendedSupergroupChatImpl -> ExtendedSupergroupChatImpl.serializer().serialize(encoder, value)
is ExtendedForumChatImpl -> ExtendedForumChatImpl.serializer().serialize(encoder, value) is ExtendedForumChatImpl -> ExtendedForumChatImpl.serializer().serialize(encoder, value)
is ExtendedChannelChatImpl -> ExtendedChannelChatImpl.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) is UnknownExtendedChat -> JsonObject.serializer().serialize(encoder, value.rawJson)
} }
} }

View File

@ -184,8 +184,10 @@ data class ExtendedBot(
@SerialName(canReadAllGroupMessagesField) @SerialName(canReadAllGroupMessagesField)
val canReadAllGroupMessages: Boolean = false, val canReadAllGroupMessages: Boolean = false,
@SerialName(supportInlineQueriesField) @SerialName(supportInlineQueriesField)
val supportsInlineQueries: Boolean = false val supportsInlineQueries: Boolean = false,
) : Bot() { @SerialName(photoField)
override val chatPhoto: ChatPhoto? = null
) : Bot(), ExtendedChat {
@SerialName(isBotField) @SerialName(isBotField)
private val isBot = true private val isBot = true
} }