2019-08-12 09:41:07 +00:00
|
|
|
package com.github.insanusmokrassar.TelegramBotAPI.types.chat
|
|
|
|
|
2020-01-22 21:47:43 +00:00
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
2019-08-12 09:41:07 +00:00
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat
|
2020-01-22 21:47:43 +00:00
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.UnknownChatType
|
2019-08-12 09:41:07 +00:00
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.ExtendedChat
|
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.extended.*
|
2020-03-22 07:37:01 +00:00
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.nonstrictJsonFormat
|
2019-08-12 09:41:07 +00:00
|
|
|
import kotlinx.serialization.*
|
2020-03-22 07:37:01 +00:00
|
|
|
import kotlinx.serialization.builtins.serializer
|
2019-08-12 09:41:07 +00:00
|
|
|
import kotlinx.serialization.json.JsonObjectSerializer
|
|
|
|
|
2020-03-22 07:37:01 +00:00
|
|
|
private val formatter = nonstrictJsonFormat
|
2019-12-02 08:51:53 +00:00
|
|
|
|
2020-01-15 07:03:42 +00:00
|
|
|
internal object PreviewChatSerializer : KSerializer<Chat> {
|
2020-03-22 07:37:01 +00:00
|
|
|
override val descriptor: SerialDescriptor = SerialDescriptor("PreviewChatSerializer", PolymorphicKind.OPEN)
|
2019-08-12 09:41:07 +00:00
|
|
|
|
|
|
|
override fun deserialize(decoder: Decoder): Chat {
|
|
|
|
val decodedJson = JsonObjectSerializer.deserialize(decoder)
|
|
|
|
|
|
|
|
val type = decodedJson.getPrimitive(typeField).content
|
|
|
|
|
|
|
|
return when (type) {
|
2019-12-02 08:51:53 +00:00
|
|
|
"private" -> formatter.fromJson(PrivateChatImpl.serializer(), decodedJson)
|
|
|
|
"group" -> formatter.fromJson(GroupChatImpl.serializer(), decodedJson)
|
|
|
|
"supergroup" -> formatter.fromJson(SupergroupChatImpl.serializer(), decodedJson)
|
|
|
|
"channel" -> formatter.fromJson(ChannelChatImpl.serializer(), decodedJson)
|
2020-01-22 21:47:43 +00:00
|
|
|
else -> UnknownChatType(
|
2020-03-22 07:37:01 +00:00
|
|
|
formatter.fromJson(Long.serializer(), decodedJson.getPrimitive(chatIdField)).toChatId(),
|
2020-01-22 21:47:43 +00:00
|
|
|
decodedJson.toString()
|
|
|
|
)
|
2019-08-12 09:41:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 07:37:01 +00:00
|
|
|
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 ChannelChatImpl -> ChannelChatImpl.serializer().serialize(encoder, value)
|
2019-08-12 09:41:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 07:03:42 +00:00
|
|
|
internal object ExtendedChatSerializer : KSerializer<ExtendedChat> {
|
2020-03-22 07:37:01 +00:00
|
|
|
override val descriptor: SerialDescriptor = SerialDescriptor("PreviewChatSerializer", PolymorphicKind.OPEN)
|
2019-08-12 09:41:07 +00:00
|
|
|
|
|
|
|
override fun deserialize(decoder: Decoder): ExtendedChat {
|
|
|
|
val decodedJson = JsonObjectSerializer.deserialize(decoder)
|
|
|
|
|
|
|
|
val type = decodedJson.getPrimitive(typeField).content
|
|
|
|
|
|
|
|
return when (type) {
|
2019-12-02 08:51:53 +00:00
|
|
|
"private" -> formatter.fromJson(ExtendedPrivateChatImpl.serializer(), decodedJson)
|
|
|
|
"group" -> formatter.fromJson(ExtendedGroupChatImpl.serializer(), decodedJson)
|
|
|
|
"supergroup" -> formatter.fromJson(ExtendedSupergroupChatImpl.serializer(), decodedJson)
|
|
|
|
"channel" -> formatter.fromJson(ExtendedChannelChatImpl.serializer(), decodedJson)
|
2019-08-12 09:41:07 +00:00
|
|
|
else -> throw IllegalArgumentException("Unknown type of chat")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 07:37:01 +00:00
|
|
|
override fun serialize(encoder: Encoder, value: ExtendedChat) {
|
|
|
|
when (value) {
|
|
|
|
is ExtendedPrivateChatImpl -> ExtendedPrivateChatImpl.serializer().serialize(encoder, value)
|
|
|
|
is ExtendedGroupChatImpl -> ExtendedGroupChatImpl.serializer().serialize(encoder, value)
|
|
|
|
is ExtendedSupergroupChatImpl -> ExtendedSupergroupChatImpl.serializer().serialize(encoder, value)
|
|
|
|
is ExtendedChannelChatImpl -> ExtendedChannelChatImpl.serializer().serialize(encoder, value)
|
2019-08-12 09:41:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|