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
|
2020-08-18 06:50:11 +00:00
|
|
|
import kotlinx.serialization.descriptors.*
|
|
|
|
import kotlinx.serialization.encoding.Decoder
|
|
|
|
import kotlinx.serialization.encoding.Encoder
|
|
|
|
import kotlinx.serialization.json.*
|
2019-08-12 09:41:07 +00:00
|
|
|
|
2020-08-19 19:50:51 +00:00
|
|
|
private val formatter
|
|
|
|
get() = nonstrictJsonFormat
|
2019-12-02 08:51:53 +00:00
|
|
|
|
2020-01-15 07:03:42 +00:00
|
|
|
internal object PreviewChatSerializer : KSerializer<Chat> {
|
2020-08-18 06:50:11 +00:00
|
|
|
@InternalSerializationApi
|
|
|
|
override val descriptor: SerialDescriptor = buildSerialDescriptor("PreviewChatSerializer", PolymorphicKind.OPEN)
|
2019-08-12 09:41:07 +00:00
|
|
|
|
|
|
|
override fun deserialize(decoder: Decoder): Chat {
|
2020-08-18 06:50:11 +00:00
|
|
|
val decodedJson = JsonObject.serializer().deserialize(decoder)
|
2019-08-12 09:41:07 +00:00
|
|
|
|
2020-08-18 06:50:11 +00:00
|
|
|
val type = decodedJson[typeField] ?.jsonPrimitive ?.content ?: error("Field $typeField must be presented, but absent in $decodedJson")
|
2019-08-12 09:41:07 +00:00
|
|
|
|
|
|
|
return when (type) {
|
2020-08-18 06:50:11 +00:00
|
|
|
"private" -> formatter.decodeFromJsonElement(PrivateChatImpl.serializer(), decodedJson)
|
|
|
|
"group" -> formatter.decodeFromJsonElement(GroupChatImpl.serializer(), decodedJson)
|
|
|
|
"supergroup" -> formatter.decodeFromJsonElement(SupergroupChatImpl.serializer(), decodedJson)
|
|
|
|
"channel" -> formatter.decodeFromJsonElement(ChannelChatImpl.serializer(), decodedJson)
|
2020-01-22 21:47:43 +00:00
|
|
|
else -> UnknownChatType(
|
2020-08-18 06:50:11 +00:00
|
|
|
formatter.decodeFromJsonElement(Long.serializer(), decodedJson[chatIdField] ?: JsonPrimitive(-1)).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-08-18 06:50:11 +00:00
|
|
|
@InternalSerializationApi
|
|
|
|
override val descriptor: SerialDescriptor = buildSerialDescriptor("PreviewChatSerializer", PolymorphicKind.OPEN)
|
2019-08-12 09:41:07 +00:00
|
|
|
|
|
|
|
override fun deserialize(decoder: Decoder): ExtendedChat {
|
2020-08-18 06:50:11 +00:00
|
|
|
val decodedJson = JsonObject.serializer().deserialize(decoder)
|
2019-08-12 09:41:07 +00:00
|
|
|
|
2020-08-18 06:50:11 +00:00
|
|
|
val type = decodedJson[typeField] ?.jsonPrimitive ?.content ?: error("Field $typeField must be presented, but absent in $decodedJson")
|
2019-08-12 09:41:07 +00:00
|
|
|
|
|
|
|
return when (type) {
|
2020-08-18 06:50:11 +00:00
|
|
|
"private" -> formatter.decodeFromJsonElement(ExtendedPrivateChatImpl.serializer(), decodedJson)
|
|
|
|
"group" -> formatter.decodeFromJsonElement(ExtendedGroupChatImpl.serializer(), decodedJson)
|
|
|
|
"supergroup" -> formatter.decodeFromJsonElement(ExtendedSupergroupChatImpl.serializer(), decodedJson)
|
|
|
|
"channel" -> formatter.decodeFromJsonElement(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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|