1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-23 02:28:45 +00:00

ChatSerializers formatter extracting

This commit is contained in:
InsanusMokrassar 2019-12-02 14:51:53 +06:00
parent e296c2ee00
commit 21544013d9

View File

@ -9,6 +9,8 @@ import kotlinx.serialization.internal.StringDescriptor
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObjectSerializer import kotlinx.serialization.json.JsonObjectSerializer
private val formatter = Json.nonstrict
object PreviewChatSerializer : KSerializer<Chat> { object PreviewChatSerializer : KSerializer<Chat> {
override val descriptor: SerialDescriptor = StringDescriptor.withName("PreviewChatSerializer") override val descriptor: SerialDescriptor = StringDescriptor.withName("PreviewChatSerializer")
@ -18,10 +20,10 @@ object PreviewChatSerializer : KSerializer<Chat> {
val type = decodedJson.getPrimitive(typeField).content val type = decodedJson.getPrimitive(typeField).content
return when (type) { return when (type) {
"private" -> Json.nonstrict.fromJson(PrivateChatImpl.serializer(), decodedJson) "private" -> formatter.fromJson(PrivateChatImpl.serializer(), decodedJson)
"group" -> Json.nonstrict.fromJson(GroupChatImpl.serializer(), decodedJson) "group" -> formatter.fromJson(GroupChatImpl.serializer(), decodedJson)
"supergroup" -> Json.nonstrict.fromJson(SupergroupChatImpl.serializer(), decodedJson) "supergroup" -> formatter.fromJson(SupergroupChatImpl.serializer(), decodedJson)
"channel" -> Json.nonstrict.fromJson(ChannelChatImpl.serializer(), decodedJson) "channel" -> formatter.fromJson(ChannelChatImpl.serializer(), decodedJson)
else -> throw IllegalArgumentException("Unknown type of chat") else -> throw IllegalArgumentException("Unknown type of chat")
} }
} }
@ -46,10 +48,10 @@ object ExtendedChatSerializer : KSerializer<ExtendedChat> {
val type = decodedJson.getPrimitive(typeField).content val type = decodedJson.getPrimitive(typeField).content
return when (type) { return when (type) {
"private" -> Json.nonstrict.fromJson(ExtendedPrivateChatImpl.serializer(), decodedJson) "private" -> formatter.fromJson(ExtendedPrivateChatImpl.serializer(), decodedJson)
"group" -> Json.nonstrict.fromJson(ExtendedGroupChatImpl.serializer(), decodedJson) "group" -> formatter.fromJson(ExtendedGroupChatImpl.serializer(), decodedJson)
"supergroup" -> Json.nonstrict.fromJson(ExtendedSupergroupChatImpl.serializer(), decodedJson) "supergroup" -> formatter.fromJson(ExtendedSupergroupChatImpl.serializer(), decodedJson)
"channel" -> Json.nonstrict.fromJson(ExtendedChannelChatImpl.serializer(), decodedJson) "channel" -> formatter.fromJson(ExtendedChannelChatImpl.serializer(), decodedJson)
else -> throw IllegalArgumentException("Unknown type of chat") else -> throw IllegalArgumentException("Unknown type of chat")
} }
} }