1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-12-15 02:35:47 +00:00

fixes in ChatIdentifierSerializer

This commit is contained in:
2020-03-22 15:53:37 +06:00
parent 89881a7349
commit c5c8a743e6
3 changed files with 17 additions and 14 deletions

View File

@@ -1,6 +1,7 @@
package com.github.insanusmokrassar.TelegramBotAPI.types
import kotlinx.serialization.*
import kotlinx.serialization.json.JsonPrimitiveSerializer
@Serializable(ChatIdentifierSerializer::class)
sealed class ChatIdentifier
@@ -39,13 +40,15 @@ fun String.toUsername(): Username = Username(this)
@Serializer(ChatIdentifier::class)
internal object ChatIdentifierSerializer : KSerializer<ChatIdentifier> {
override fun deserialize(decoder: Decoder): ChatIdentifier {
val id = decoder.decodeString()
return id.toLongOrNull() ?.let {
val id = JsonPrimitiveSerializer.deserialize(decoder)
return id.longOrNull ?.let {
ChatId(it)
} ?: if (!id.startsWith("@")) {
Username("@$id")
} else {
Username(id)
} ?: id.content.let {
if (!it.startsWith("@")) {
Username("@$it")
} else {
Username(it)
}
}
}