diff --git a/CHANGELOG.md b/CHANGELOG.md index 084f9d3807..abcea7f75e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ `keyboard` * `makeLinkToMessage` now is not `PreviewFeature` * All deprecations was removed + * `RequestException` now extends `io.ktor.utils.io.errors.IOException` instead of `kotlinx.io.IOException` + * `Any#toJson` now is NOT `inline` ## 0.24.0 diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifier.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifier.kt index a871bceaa3..603eb5e838 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifier.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifier.kt @@ -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 { 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) + } } } diff --git a/TelegramBotAPI/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifierTests.kt b/TelegramBotAPI/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifierTests.kt index a11c7a22ed..02005ee51c 100644 --- a/TelegramBotAPI/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifierTests.kt +++ b/TelegramBotAPI/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifierTests.kt @@ -1,10 +1,8 @@ package com.github.insanusmokrassar.TelegramBotAPI.types -import com.github.insanusmokrassar.TelegramBotAPI.NonstrictTestsJsonFormat import com.github.insanusmokrassar.TelegramBotAPI.TestsJsonFormat import kotlinx.serialization.ImplicitReflectionSerializer import kotlinx.serialization.Serializable -import kotlinx.serialization.json.Json import kotlin.test.* @ImplicitReflectionSerializer @@ -56,25 +54,25 @@ class ChatIdentifierTests { ) Example(chatIdentifierChatId.toChatId()).let { withChatId -> - val stringified = NonstrictTestsJsonFormat.stringify(Example.serializer(), withChatId) + val stringified = TestsJsonFormat.stringify(Example.serializer(), withChatId) assertEquals(stringified, "{\"identifier\":$chatIdentifierChatId}") - val deserialized = NonstrictTestsJsonFormat.parse(Example.serializer(), stringified) + val deserialized = TestsJsonFormat.parse(Example.serializer(), stringified) assertEquals(withChatId, deserialized) } Example(testUsername.toUsername()).let { withUsername -> - val stringified = NonstrictTestsJsonFormat.stringify(Example.serializer(), withUsername) + val stringified = TestsJsonFormat.stringify(Example.serializer(), withUsername) assertEquals(stringified, "{\"identifier\":\"$testUsername\"}") - val deserialized = NonstrictTestsJsonFormat.parse(Example.serializer(), stringified) + val deserialized = TestsJsonFormat.parse(Example.serializer(), stringified) assertEquals(withUsername, deserialized) } // Replace @ by empty string, because from time to time we can retrieve from Telegram system // username without starting @ symbol Example(testUsername.toUsername()).let { withUsername -> - val stringified = NonstrictTestsJsonFormat.stringify(Example.serializer(), withUsername).replace("@", "") + val stringified = TestsJsonFormat.stringify(Example.serializer(), withUsername).replace("@", "") assertEquals("{\"identifier\":\"${testUsername.replace("@", "")}\"}", stringified) - val deserialized = NonstrictTestsJsonFormat.parse(Example.serializer(), stringified) + val deserialized = TestsJsonFormat.parse(Example.serializer(), stringified) assertEquals(withUsername, deserialized) } }