mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-12-22 16:47:13 +00:00
improve serializers
This commit is contained in:
parent
80be86454d
commit
46e6eeca9d
@ -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
|
||||
|
@ -56,38 +56,35 @@ object ChatSerializer : KSerializer<Chat> {
|
||||
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<ExtendedChat> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user