From 6013c3ba86ac9c378de24ea8a81244f12ccc882b Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 18 Aug 2020 12:50:11 +0600 Subject: [PATCH] versions updates --- CHANGELOG.md | 14 +++++++ TelegramBotAPI-all/build.gradle | 9 +++-- TelegramBotAPI-extensions-api/build.gradle | 9 +++-- .../extensions/api/JsonUtils.kt | 2 +- TelegramBotAPI-extensions-utils/build.gradle | 9 +++-- .../extensions/utils/JsonFormat.kt | 2 +- .../utils/updates/UpdateDeserialization.kt | 4 +- .../utils/updates/retrieving/Webhook.kt | 4 +- TelegramBotAPI/build.gradle | 28 ++++++++------ .../bot/Ktor/KtorRequestsExecutor.kt | 6 +-- .../requests/abstracts/InputFile.kt | 5 ++- .../requests/send/media/SendMediaGroup.kt | 5 ++- .../TelegramBotAPI/types/ChatIdentifier.kt | 6 ++- .../types/ChatMember/abstracts/ChatMember.kt | 5 ++- .../InlineQueryResultSerializer.kt | 6 ++- .../InputMessageContentSerializer.kt | 6 ++- .../types/InputMedia/InputMediaPhoto.kt | 5 ++- .../types/InputMedia/InputMediaSerializer.kt | 6 ++- .../types/InputMedia/InputMediaVideo.kt | 6 ++- .../InputMedia/MediaGroupMemberInputMedia.kt | 1 + .../MediaGroupMemberInputMediaSerializer.kt | 16 +++++--- .../types/ParseMode/ParseMode.kt | 2 + .../TelegramBotAPI/types/TelegramDate.kt | 2 + .../TelegramBotAPI/types/User.kt | 18 +++++---- .../TelegramBotAPI/types/actions/BotAction.kt | 2 + .../InlineKeyboardButtonSerializer.kt | 15 +++++--- .../types/buttons/KeyboardButton.kt | 24 ++++++------ .../types/buttons/KeyboardButtonPollType.kt | 8 ++-- .../types/buttons/KeyboardMarkupSerializer.kt | 6 ++- .../types/chat/ChatSerializers.kt | 37 +++++++++++-------- .../types/dice/DiceAnimationType.kt | 5 ++- .../types/message/abstracts/Message.kt | 9 ++++- .../TelegramBotAPI/types/polls/Poll.kt | 21 ++++++----- .../TelegramBotAPI/types/polls/PollOption.kt | 3 ++ .../TelegramBotAPI/types/update/RawUpdate.kt | 1 + .../types/update/abstracts/Update.kt | 16 ++++---- .../TelegramBotAPI/utils/JSON.kt | 24 ++++++------ .../TelegramBotAPI/utils/MimeType.kt | 27 +++++++++++++- .../TelegramBotAPI/TestsJsonFormat.kt | 3 +- .../TelegramBotAPI/types/BotActionTests.kt | 8 ++-- .../types/ChatIdentifierTests.kt | 29 ++++++--------- .../TelegramBotAPI/types/ParseModeTests.kt | 14 +++---- .../TelegramBotAPI/types/TelegramDateTests.kt | 8 ++-- .../TelegramBotAPI/utils/MimeType.kt | 35 ------------------ .../TelegramBotAPI/utils/MimeTypeActual.kt | 17 +++++++++ .../TelegramBotAPI/utils/MimeType.kt | 28 -------------- .../TelegramBotAPI/utils/MimeTypeActual.kt | 10 +++++ .../utils/extensions/Webhooks.kt | 4 +- build.gradle | 18 +++++++++ docs/build.gradle | 9 +++-- gradle.properties | 17 +++++---- gradle/wrapper/gradle-wrapper.properties | 2 +- 52 files changed, 335 insertions(+), 241 deletions(-) delete mode 100644 TelegramBotAPI/src/jsMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeType.kt create mode 100644 TelegramBotAPI/src/jsMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeTypeActual.kt delete mode 100644 TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeType.kt create mode 100644 TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeTypeActual.kt create mode 100644 build.gradle diff --git a/CHANGELOG.md b/CHANGELOG.md index 762e1fb887..98ef200740 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,20 @@ ## 0.28.0 +### RC + +**THIS VERSION CONTAINS BREAKING CHANGES** + +* `Common`: + * Version updates: + * `Kotlin`: `1.3.72` -> `1.4.0` + * `Coroutines`: `1.3.8` -> `1.3.9` + * `Serialization`: `0.20.0` -> `1.0.0-RC` + * `Klock`: `1.11.14` -> `1.12.0` + * `UUID`: `0.1.1` -> `0.2.0` + * `Ktor`: `1.3.2` -> `1.3.2-1.4.0-rc` + * `buildMimeType` function now is cache-oriented getter which will save already got mime types into internal map + ## 0.27.0 * `Common`: diff --git a/TelegramBotAPI-all/build.gradle b/TelegramBotAPI-all/build.gradle index 939d13307f..a24eceae69 100644 --- a/TelegramBotAPI-all/build.gradle +++ b/TelegramBotAPI-all/build.gradle @@ -13,8 +13,8 @@ buildscript { } plugins { - id "org.jetbrains.kotlin.multiplatform" version "$kotlin_version" - id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version" + id "org.jetbrains.kotlin.multiplatform" + id "org.jetbrains.kotlin.plugin.serialization" } project.version = "$library_version" @@ -31,7 +31,10 @@ repositories { kotlin { jvm() - js() + js { + browser() + nodejs() + } sourceSets { commonMain { diff --git a/TelegramBotAPI-extensions-api/build.gradle b/TelegramBotAPI-extensions-api/build.gradle index 217310d289..3334bf03aa 100644 --- a/TelegramBotAPI-extensions-api/build.gradle +++ b/TelegramBotAPI-extensions-api/build.gradle @@ -13,8 +13,8 @@ buildscript { } plugins { - id "org.jetbrains.kotlin.multiplatform" version "$kotlin_version" - id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version" + id "org.jetbrains.kotlin.multiplatform" + id "org.jetbrains.kotlin.plugin.serialization" } project.version = "$library_version" @@ -31,7 +31,10 @@ repositories { kotlin { jvm() - js() + js { + browser() + nodejs() + } sourceSets { commonMain { diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/JsonUtils.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/JsonUtils.kt index a88439d920..0a4153f2c1 100644 --- a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/JsonUtils.kt +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/JsonUtils.kt @@ -6,6 +6,6 @@ import kotlinx.serialization.json.Json internal val nonstrictJsonFormat = Json { isLenient = true ignoreUnknownKeys = true - serializeSpecialFloatingPointValues = true + allowSpecialFloatingPointValues = true useArrayPolymorphism = true } diff --git a/TelegramBotAPI-extensions-utils/build.gradle b/TelegramBotAPI-extensions-utils/build.gradle index 217310d289..3334bf03aa 100644 --- a/TelegramBotAPI-extensions-utils/build.gradle +++ b/TelegramBotAPI-extensions-utils/build.gradle @@ -13,8 +13,8 @@ buildscript { } plugins { - id "org.jetbrains.kotlin.multiplatform" version "$kotlin_version" - id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version" + id "org.jetbrains.kotlin.multiplatform" + id "org.jetbrains.kotlin.plugin.serialization" } project.version = "$library_version" @@ -31,7 +31,10 @@ repositories { kotlin { jvm() - js() + js { + browser() + nodejs() + } sourceSets { commonMain { diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/JsonFormat.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/JsonFormat.kt index 63977802c2..86b3f21720 100644 --- a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/JsonFormat.kt +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/JsonFormat.kt @@ -6,6 +6,6 @@ import kotlinx.serialization.json.Json internal val nonstrictJsonFormat = Json { isLenient = true ignoreUnknownKeys = true - serializeSpecialFloatingPointValues = true + allowSpecialFloatingPointValues = true useArrayPolymorphism = true } diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdateDeserialization.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdateDeserialization.kt index 017a61400d..b8c889e624 100644 --- a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdateDeserialization.kt +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdateDeserialization.kt @@ -8,11 +8,11 @@ import kotlinx.serialization.json.JsonElement /** * @return Deserialize [source] as [com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update] */ -fun Json.toTelegramUpdate(source: String) = parse(UpdateDeserializationStrategy, source) +fun Json.toTelegramUpdate(source: String) = decodeFromString(UpdateDeserializationStrategy, source) /** * @return Deserialize [source] as [com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update] */ -fun Json.toTelegramUpdate(source: JsonElement) = fromJson(UpdateDeserializationStrategy, source) +fun Json.toTelegramUpdate(source: JsonElement) = decodeFromJsonElement(UpdateDeserializationStrategy, source) /** * @return Deserialize [this] as [com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update]. In fact, diff --git a/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt b/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt index 45343c69f3..6aaa5cb381 100644 --- a/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt +++ b/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt @@ -45,8 +45,8 @@ fun Route.includeWebhookHandlingInRoute( exceptionsHandler ?: {} ) { val asJson = - nonstrictJsonFormat.parseJson(call.receiveText()) - val update = nonstrictJsonFormat.fromJson( + nonstrictJsonFormat.parseToJsonElement(call.receiveText()) + val update = nonstrictJsonFormat.decodeFromJsonElement( UpdateDeserializationStrategy, asJson ) diff --git a/TelegramBotAPI/build.gradle b/TelegramBotAPI/build.gradle index ff5a6c7704..dc6d9b7f4b 100644 --- a/TelegramBotAPI/build.gradle +++ b/TelegramBotAPI/build.gradle @@ -13,8 +13,8 @@ buildscript { } plugins { - id "org.jetbrains.kotlin.multiplatform" version "$kotlin_version" - id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version" + id "org.jetbrains.kotlin.multiplatform" + id "org.jetbrains.kotlin.plugin.serialization" } project.version = "$library_version" @@ -31,15 +31,18 @@ repositories { kotlin { jvm() - js() + js { + browser() + nodejs() + } sourceSets { commonMain { dependencies { implementation kotlin('stdlib') - api "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$kotlin_coroutines_version" - api "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$kotlin_serialisation_runtime_version" - api "org.jetbrains.kotlinx:kotlinx-serialization-properties-common:$kotlin_serialisation_runtime_version" + api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version" + api "org.jetbrains.kotlinx:kotlinx-serialization-core:$kotlin_serialisation_runtime_version" + api "org.jetbrains.kotlinx:kotlinx-serialization-properties:$kotlin_serialisation_runtime_version" api "com.soywiz.korlibs.klock:klock:$klock_version" api "com.benasher44:uuid:$uuid_version" @@ -56,9 +59,6 @@ kotlin { jvmMain { dependencies { - api "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$kotlin_serialisation_runtime_version" - api "org.jetbrains.kotlinx:kotlinx-serialization-properties:$kotlin_serialisation_runtime_version" - api "io.ktor:ktor-client:$ktor_version" api "io.ktor:ktor-server:$ktor_version" @@ -76,12 +76,16 @@ kotlin { jsMain { dependencies { - api "org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:$kotlin_serialisation_runtime_version" - api "org.jetbrains.kotlinx:kotlinx-serialization-properties-js:$kotlin_serialisation_runtime_version" - api "org.jetbrains.kotlinx:kotlinx-coroutines-core-js:$kotlin_coroutines_version" api "io.ktor:ktor-client-js:$ktor_version" } } + + jsTest { + dependencies { + implementation kotlin('test-junit') + implementation kotlin('test-js') + } + } } targets.all { diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/KtorRequestsExecutor.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/KtorRequestsExecutor.kt index b9714d11b3..379db907df 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/KtorRequestsExecutor.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/KtorRequestsExecutor.kt @@ -45,7 +45,7 @@ class KtorRequestsExecutor( { e -> throw if (e is ClientRequestException) { val content = e.response.readText() - val responseObject = jsonFormatter.parse(Response.serializer(), content) + val responseObject = jsonFormatter.decodeFromString(Response.serializer(), content) newRequestException( responseObject, content, @@ -71,10 +71,10 @@ class KtorRequestsExecutor( val response = statement?.execute() ?: throw IllegalArgumentException("Can't execute request: $request") val content = response.receive() - val responseObject = jsonFormatter.parse(Response.serializer(), content) + val responseObject = jsonFormatter.decodeFromString(Response.serializer(), content) (responseObject.result?.let { - jsonFormatter.fromJson(request.resultDeserializer, it) + jsonFormatter.decodeFromJsonElement(request.resultDeserializer, it) } ?: responseObject.parameters?.let { val error = it.error if (error is RetryAfterError) { diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/InputFile.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/InputFile.kt index 262623227d..3227b9d0fe 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/InputFile.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/InputFile.kt @@ -2,6 +2,9 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts import com.github.insanusmokrassar.TelegramBotAPI.utils.StorageFile import kotlinx.serialization.* +import kotlinx.serialization.descriptors.* +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder @Serializable(InputFileSerializer::class) sealed class InputFile { @@ -21,7 +24,7 @@ fun String.toInputFile() = FileId(this) @Serializer(InputFile::class) internal object InputFileSerializer : KSerializer { - override val descriptor: SerialDescriptor = PrimitiveDescriptor(FileId::class.toString(), PrimitiveKind.STRING) + override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor(FileId::class.toString(), PrimitiveKind.STRING) override fun serialize(encoder: Encoder, value: InputFile) = encoder.encodeString(value.fileId) override fun deserialize(decoder: Decoder): FileId = FileId(decoder.decodeString()) } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendMediaGroup.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendMediaGroup.kt index c28dab19ea..43841c3869 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendMediaGroup.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendMediaGroup.kt @@ -12,6 +12,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.throwRangeError import com.github.insanusmokrassar.TelegramBotAPI.utils.toJsonWithoutNulls import kotlinx.serialization.* import kotlinx.serialization.builtins.ListSerializer +import kotlinx.serialization.json.buildJsonArray import kotlinx.serialization.json.jsonArray @Deprecated("Replaced and renamed", ReplaceWith("mediaCountInMediaGroup", "com.github.insanusmokrassar.TelegramBotAPI.types.mediaCountInMediaGroup")) @@ -71,9 +72,9 @@ data class SendMediaGroupData internal constructor( ) : DataRequest>, SendMessageRequest> { @SerialName(mediaField) private val convertedMedia: String - get() = jsonArray { + get() = buildJsonArray { media.forEach { - +it.toJsonWithoutNulls(MediaGroupMemberInputMediaSerializer) + add(it.toJsonWithoutNulls(MediaGroupMemberInputMediaSerializer)) } }.toString() 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 603eb5e838..f65163691c 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,7 +1,9 @@ package com.github.insanusmokrassar.TelegramBotAPI.types import kotlinx.serialization.* -import kotlinx.serialization.json.JsonPrimitiveSerializer +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder +import kotlinx.serialization.json.* @Serializable(ChatIdentifierSerializer::class) sealed class ChatIdentifier @@ -40,7 +42,7 @@ fun String.toUsername(): Username = Username(this) @Serializer(ChatIdentifier::class) internal object ChatIdentifierSerializer : KSerializer { override fun deserialize(decoder: Decoder): ChatIdentifier { - val id = JsonPrimitiveSerializer.deserialize(decoder) + val id = JsonPrimitive.serializer().deserialize(decoder) return id.longOrNull ?.let { ChatId(it) } ?: id.content.let { diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatMember/abstracts/ChatMember.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatMember/abstracts/ChatMember.kt index 93c60cbf9b..6c385e4454 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatMember/abstracts/ChatMember.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatMember/abstracts/ChatMember.kt @@ -3,6 +3,9 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.ChatMember.abstracts import com.github.insanusmokrassar.TelegramBotAPI.types.ChatMember.RawChatMember import com.github.insanusmokrassar.TelegramBotAPI.types.User import kotlinx.serialization.* +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder interface ChatMember { val user: User @@ -20,5 +23,5 @@ internal object ChatMemberDeserializationStrategy : DeserializationStrategy { - override val descriptor: SerialDescriptor = SerialDescriptor(InlineQueryResult::class.toString(), PolymorphicKind.OPEN) + @InternalSerializationApi + override val descriptor: SerialDescriptor = buildSerialDescriptor(InlineQueryResult::class.toString(), PolymorphicKind.OPEN) override fun serialize(encoder: Encoder, value: InlineQueryResult) { when(value) { is InlineQueryResultArticle -> InlineQueryResultArticle.serializer().serialize(encoder, value) diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InputMessageContentSerializer.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InputMessageContentSerializer.kt index eefeae7248..3505235113 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InputMessageContentSerializer.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InputMessageContentSerializer.kt @@ -3,10 +3,14 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries import com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.InputMessageContent.* import com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.abstracts.InputMessageContent import kotlinx.serialization.* +import kotlinx.serialization.descriptors.* +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder @Serializer(InputMessageContent::class) internal object InputMessageContentSerializer : KSerializer { - override val descriptor: SerialDescriptor = SerialDescriptor(InputMessageContent::class.toString(), PolymorphicKind.OPEN) + @InternalSerializationApi + override val descriptor: SerialDescriptor = buildSerialDescriptor(InputMessageContent::class.toString(), PolymorphicKind.OPEN) override fun serialize(encoder: Encoder, value: InputMessageContent) { when (value) { is InputContactMessageContent -> InputContactMessageContent.serializer().serialize(encoder, value) diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/InputMediaPhoto.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/InputMediaPhoto.kt index f45e903b94..5fed0893a7 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/InputMediaPhoto.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/InputMediaPhoto.kt @@ -18,7 +18,7 @@ data class InputMediaPhoto( ) : InputMedia, MediaGroupMemberInputMedia { override val type: String = photoInputMediaType - override fun serialize(format: StringFormat): String = format.stringify(serializer(), this) + override fun serialize(format: StringFormat): String = format.encodeToString(serializer(), this) @SerialName(mediaField) val media: String = when (file) { @@ -27,7 +27,8 @@ data class InputMediaPhoto( } @Transient - override val arguments: Map = Properties.storeNullable(serializer(), this) + @Deprecated("Marked as deprecated for removal in future updates", level = DeprecationLevel.ERROR) + override val arguments: Map = error("Unsupported operation") } fun PhotoSize.toInputMediaPhoto( diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/InputMediaSerializer.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/InputMediaSerializer.kt index 9da0db77fe..c61c13b8b0 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/InputMediaSerializer.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/InputMediaSerializer.kt @@ -1,10 +1,14 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.InputMedia import kotlinx.serialization.* +import kotlinx.serialization.descriptors.* +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder @Serializer(InputMedia::class) internal object InputMediaSerializer : KSerializer { - override val descriptor: SerialDescriptor = SerialDescriptor(InputMedia::class.toString(), PolymorphicKind.OPEN) + @InternalSerializationApi + override val descriptor: SerialDescriptor = buildSerialDescriptor(InputMedia::class.toString(), PolymorphicKind.OPEN) override fun serialize(encoder: Encoder, value: InputMedia) { when (value) { is InputMediaVideo -> InputMediaVideo.serializer().serialize(encoder, value) diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/InputMediaVideo.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/InputMediaVideo.kt index 7b12d56613..aa1626ae2a 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/InputMediaVideo.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/InputMediaVideo.kt @@ -5,6 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.ParseMode import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.parseModeField import com.github.insanusmokrassar.TelegramBotAPI.types.mediaField import kotlinx.serialization.* +import kotlinx.serialization.properties.Properties internal const val videoInputMediaType = "video" @@ -21,7 +22,7 @@ data class InputMediaVideo( ) : InputMedia, SizedInputMedia, DuratedInputMedia, ThumbedInputMedia, MediaGroupMemberInputMedia { override val type: String = videoInputMediaType - override fun serialize(format: StringFormat): String = format.stringify(serializer(), this) + override fun serialize(format: StringFormat): String = format.encodeToString(serializer(), this) @SerialName(mediaField) val media: String = when (file) { @@ -30,5 +31,6 @@ data class InputMediaVideo( } @Transient - override val arguments: Map = Properties.storeNullable(serializer(), this) + @Deprecated("Marked as deprecated for removal in future updates", level = DeprecationLevel.ERROR) + override val arguments: Map = error("Unsupported operation") } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/MediaGroupMemberInputMedia.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/MediaGroupMemberInputMedia.kt index 5e9a429c20..ed211ca949 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/MediaGroupMemberInputMedia.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/MediaGroupMemberInputMedia.kt @@ -7,5 +7,6 @@ import kotlinx.serialization.StringFormat @Serializable(MediaGroupMemberInputMediaSerializer::class) interface MediaGroupMemberInputMedia : InputMedia, CaptionedOutput { fun serialize(format: StringFormat): String + @Deprecated("Marked as deprecated for removal in future updates", level = DeprecationLevel.ERROR) val arguments: Map } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/MediaGroupMemberInputMediaSerializer.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/MediaGroupMemberInputMediaSerializer.kt index 2796b3d222..cd9a86c68d 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/MediaGroupMemberInputMediaSerializer.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/MediaGroupMemberInputMediaSerializer.kt @@ -3,11 +3,15 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.InputMedia import com.github.insanusmokrassar.TelegramBotAPI.types.typeField import com.github.insanusmokrassar.TelegramBotAPI.utils.nonstrictJsonFormat import kotlinx.serialization.* -import kotlinx.serialization.json.JsonObjectSerializer +import kotlinx.serialization.descriptors.* +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder +import kotlinx.serialization.json.* @Serializer(MediaGroupMemberInputMedia::class) internal object MediaGroupMemberInputMediaSerializer : KSerializer { - override val descriptor: SerialDescriptor = SerialDescriptor(MediaGroupMemberInputMedia::class.toString(), PolymorphicKind.OPEN) + @InternalSerializationApi + override val descriptor: SerialDescriptor = buildSerialDescriptor(MediaGroupMemberInputMedia::class.toString(), PolymorphicKind.OPEN) override fun serialize(encoder: Encoder, value: MediaGroupMemberInputMedia) { when (value) { is InputMediaPhoto -> InputMediaPhoto.serializer().serialize(encoder, value) @@ -16,11 +20,11 @@ internal object MediaGroupMemberInputMediaSerializer : KSerializer nonstrictJsonFormat.fromJson(InputMediaPhoto.serializer(), json) - videoInputMediaType -> nonstrictJsonFormat.fromJson(InputMediaVideo.serializer(), json) + return when (json[typeField] ?.jsonPrimitive ?.contentOrNull) { + photoInputMediaType -> nonstrictJsonFormat.decodeFromJsonElement(InputMediaPhoto.serializer(), json) + videoInputMediaType -> nonstrictJsonFormat.decodeFromJsonElement(InputMediaVideo.serializer(), json) else -> error("Illegal type of incoming MediaGroupMemberInputMedia") } } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ParseMode/ParseMode.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ParseMode/ParseMode.kt index 9e53d9b5d7..46d66003c5 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ParseMode/ParseMode.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ParseMode/ParseMode.kt @@ -1,6 +1,8 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode import kotlinx.serialization.* +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder internal const val parseModeField = "parse_mode" diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/TelegramDate.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/TelegramDate.kt index f4ac135f99..381e08da1d 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/TelegramDate.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/TelegramDate.kt @@ -2,6 +2,8 @@ package com.github.insanusmokrassar.TelegramBotAPI.types import com.soywiz.klock.DateTime import kotlinx.serialization.* +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder @Serializable(TelegramDateSerializer::class) data class TelegramDate( diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/User.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/User.kt index 9a8574e2dc..c885173630 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/User.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/User.kt @@ -5,7 +5,9 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.chat.extended.ExtendedPr import com.github.insanusmokrassar.TelegramBotAPI.utils.PreviewFeature import com.github.insanusmokrassar.TelegramBotAPI.utils.nonstrictJsonFormat import kotlinx.serialization.* -import kotlinx.serialization.json.JsonObjectSerializer +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder +import kotlinx.serialization.json.* @Serializable(UserSerializer::class) sealed class User : PrivateChat @@ -69,24 +71,24 @@ data class ExtendedBot( @Serializer(User::class) internal object UserSerializer : KSerializer { override fun deserialize(decoder: Decoder): User { - val asJson = JsonObjectSerializer.deserialize(decoder) + val asJson = JsonObject.serializer().deserialize(decoder) return when { - asJson.getPrimitiveOrNull(isBotField) ?.booleanOrNull != true -> nonstrictJsonFormat.fromJson( + asJson[isBotField] ?.jsonPrimitive ?.booleanOrNull != true -> nonstrictJsonFormat.decodeFromJsonElement( CommonUser.serializer(), asJson ) else -> { - if ((asJson.get(canJoinGroupsField) - ?: asJson.get(canReadAllGroupMessagesField) - ?: asJson.get(supportInlineQueriesField)) != null + if ((asJson[canJoinGroupsField] + ?: asJson[canReadAllGroupMessagesField] + ?: asJson[supportInlineQueriesField]) != null ) { - nonstrictJsonFormat.fromJson( + nonstrictJsonFormat.decodeFromJsonElement( ExtendedBot.serializer(), asJson ) } else { - nonstrictJsonFormat.fromJson( + nonstrictJsonFormat.decodeFromJsonElement( CommonBot.serializer(), asJson ) diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/actions/BotAction.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/actions/BotAction.kt index cce421d77b..da51cab99a 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/actions/BotAction.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/actions/BotAction.kt @@ -1,6 +1,8 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.actions import kotlinx.serialization.* +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder /** * Use BotAction objects realisations to notify user about bot actions diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButtonSerializer.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButtonSerializer.kt index c6e08ada73..f45f542aca 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButtonSerializer.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButtonSerializer.kt @@ -3,11 +3,14 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardB import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.utils.nonstrictJsonFormat import kotlinx.serialization.* -import kotlinx.serialization.json.JsonElementSerializer -import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.descriptors.* +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder +import kotlinx.serialization.json.* internal object InlineKeyboardButtonSerializer : KSerializer { - override val descriptor: SerialDescriptor = SerialDescriptor( + @InternalSerializationApi + override val descriptor: SerialDescriptor = buildSerialDescriptor( "com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons.InlineKeyboardButton", PolymorphicKind.SEALED ) @@ -26,10 +29,10 @@ internal object InlineKeyboardButtonSerializer : KSerializer SwitchInlineQueryCurrentChatInlineKeyboardButton.serializer().serialize(encoder, value) is URLInlineKeyboardButton -> URLInlineKeyboardButton.serializer().serialize(encoder, value) is CallbackGameInlineKeyboardButton -> CallbackGameInlineKeyboardButton.serializer().serialize(encoder, value) - is UnknownInlineKeyboardButton -> JsonElementSerializer.serialize(encoder, value.rawData) + is UnknownInlineKeyboardButton -> JsonElement.serializer().serialize(encoder, value.rawData) } } } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/KeyboardButton.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/KeyboardButton.kt index f913c0d07a..6343c50ab5 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/KeyboardButton.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/KeyboardButton.kt @@ -3,6 +3,8 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.buttons import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.utils.nonstrictJsonFormat import kotlinx.serialization.* +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.json.* @Serializable(KeyboardButtonSerializer::class) @@ -47,26 +49,26 @@ data class RequestPollKeyboardButton( @Serializer(KeyboardButton::class) internal object KeyboardButtonSerializer : KSerializer { override fun deserialize(decoder: Decoder): KeyboardButton { - val asJson = JsonElementSerializer.deserialize(decoder) + val asJson = JsonElement.serializer().deserialize(decoder) return when { asJson is JsonPrimitive -> SimpleKeyboardButton(asJson.content) - asJson is JsonObject && asJson.getPrimitiveOrNull(requestContactField) != null -> RequestContactKeyboardButton( - asJson.getPrimitive(textField).content + asJson is JsonObject && asJson[requestContactField] != null -> RequestContactKeyboardButton( + asJson[textField]!!.jsonPrimitive.content ) - asJson is JsonObject && asJson.getPrimitiveOrNull(requestLocationField) != null -> RequestLocationKeyboardButton( - asJson.getPrimitive(textField).content + asJson is JsonObject && asJson[requestLocationField] != null -> RequestLocationKeyboardButton( + asJson[textField]!!.jsonPrimitive.content ) - asJson is JsonObject && asJson.getObjectOrNull(requestPollField) != null -> RequestPollKeyboardButton( - asJson.getPrimitive(textField).content, - nonstrictJsonFormat.fromJson( + asJson is JsonObject && asJson[requestPollField] != null -> RequestPollKeyboardButton( + asJson[textField]!!.jsonPrimitive.content, + nonstrictJsonFormat.decodeFromJsonElement( KeyboardButtonPollType.serializer(), - asJson.getObject(requestPollField) + asJson[requestPollField] ?.jsonObject ?: buildJsonObject { } ) ) else -> UnknownKeyboardButton( when (asJson) { - is JsonObject -> asJson.getPrimitive(textField).content + is JsonObject -> asJson[textField]!!.jsonPrimitive.content is JsonArray -> "" is JsonPrimitive -> asJson.content }, @@ -81,7 +83,7 @@ internal object KeyboardButtonSerializer : KSerializer { is RequestLocationKeyboardButton -> RequestLocationKeyboardButton.serializer().serialize(encoder, value) is RequestPollKeyboardButton -> RequestPollKeyboardButton.serializer().serialize(encoder, value) is SimpleKeyboardButton -> encoder.encodeString(value.text) - is UnknownKeyboardButton -> JsonElementSerializer.serialize(encoder, nonstrictJsonFormat.parseJson(value.raw)) + is UnknownKeyboardButton -> JsonElement.serializer().serialize(encoder, nonstrictJsonFormat.parseToJsonElement(value.raw)) } } } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/KeyboardButtonPollType.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/KeyboardButtonPollType.kt index aef28823a3..218f1d4c76 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/KeyboardButtonPollType.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/KeyboardButtonPollType.kt @@ -2,6 +2,8 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.buttons import com.github.insanusmokrassar.TelegramBotAPI.types.* import kotlinx.serialization.* +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.json.* @Serializable(KeyboardButtonPollTypeSerializer::class) @@ -25,11 +27,11 @@ object QuizKeyboardButtonPollType : KeyboardButtonPollType() { @Serializer(KeyboardButtonPollType::class) internal object KeyboardButtonPollTypeSerializer : KSerializer { override fun deserialize(decoder: Decoder): KeyboardButtonPollType { - val asJson = JsonElementSerializer.deserialize(decoder) + val asJson = JsonElement.serializer().deserialize(decoder) val type = when (asJson) { is JsonPrimitive -> asJson.content - else -> asJson.jsonObject.getPrimitive(typeField).content + else -> asJson.jsonObject[typeField] ?.jsonPrimitive ?.content ?: "absent" } return when (type) { @@ -43,7 +45,7 @@ internal object KeyboardButtonPollTypeSerializer : KSerializer { - override val descriptor: SerialDescriptor = SerialDescriptor( + @InternalSerializationApi + override val descriptor: SerialDescriptor = buildSerialDescriptor( KeyboardMarkup::class.toString(), PolymorphicKind.OPEN ) diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/chat/ChatSerializers.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/chat/ChatSerializers.kt index 927a59966a..31a9d06552 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/chat/ChatSerializers.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/chat/ChatSerializers.kt @@ -8,25 +8,29 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.chat.extended.* import com.github.insanusmokrassar.TelegramBotAPI.utils.nonstrictJsonFormat import kotlinx.serialization.* import kotlinx.serialization.builtins.serializer -import kotlinx.serialization.json.JsonObjectSerializer +import kotlinx.serialization.descriptors.* +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder +import kotlinx.serialization.json.* private val formatter = nonstrictJsonFormat internal object PreviewChatSerializer : KSerializer { - override val descriptor: SerialDescriptor = SerialDescriptor("PreviewChatSerializer", PolymorphicKind.OPEN) + @InternalSerializationApi + override val descriptor: SerialDescriptor = buildSerialDescriptor("PreviewChatSerializer", PolymorphicKind.OPEN) override fun deserialize(decoder: Decoder): Chat { - val decodedJson = JsonObjectSerializer.deserialize(decoder) + val decodedJson = JsonObject.serializer().deserialize(decoder) - val type = decodedJson.getPrimitive(typeField).content + val type = decodedJson[typeField] ?.jsonPrimitive ?.content ?: error("Field $typeField must be presented, but absent in $decodedJson") return when (type) { - "private" -> formatter.fromJson(PrivateChatImpl.serializer(), decodedJson) - "group" -> formatter.fromJson(GroupChatImpl.serializer(), decodedJson) - "supergroup" -> formatter.fromJson(SupergroupChatImpl.serializer(), decodedJson) - "channel" -> formatter.fromJson(ChannelChatImpl.serializer(), decodedJson) + "private" -> formatter.decodeFromJsonElement(PrivateChatImpl.serializer(), decodedJson) + "group" -> formatter.decodeFromJsonElement(GroupChatImpl.serializer(), decodedJson) + "supergroup" -> formatter.decodeFromJsonElement(SupergroupChatImpl.serializer(), decodedJson) + "channel" -> formatter.decodeFromJsonElement(ChannelChatImpl.serializer(), decodedJson) else -> UnknownChatType( - formatter.fromJson(Long.serializer(), decodedJson.getPrimitive(chatIdField)).toChatId(), + formatter.decodeFromJsonElement(Long.serializer(), decodedJson[chatIdField] ?: JsonPrimitive(-1)).toChatId(), decodedJson.toString() ) } @@ -44,18 +48,19 @@ internal object PreviewChatSerializer : KSerializer { } internal object ExtendedChatSerializer : KSerializer { - override val descriptor: SerialDescriptor = SerialDescriptor("PreviewChatSerializer", PolymorphicKind.OPEN) + @InternalSerializationApi + override val descriptor: SerialDescriptor = buildSerialDescriptor("PreviewChatSerializer", PolymorphicKind.OPEN) override fun deserialize(decoder: Decoder): ExtendedChat { - val decodedJson = JsonObjectSerializer.deserialize(decoder) + val decodedJson = JsonObject.serializer().deserialize(decoder) - val type = decodedJson.getPrimitive(typeField).content + val type = decodedJson[typeField] ?.jsonPrimitive ?.content ?: error("Field $typeField must be presented, but absent in $decodedJson") return when (type) { - "private" -> formatter.fromJson(ExtendedPrivateChatImpl.serializer(), decodedJson) - "group" -> formatter.fromJson(ExtendedGroupChatImpl.serializer(), decodedJson) - "supergroup" -> formatter.fromJson(ExtendedSupergroupChatImpl.serializer(), decodedJson) - "channel" -> formatter.fromJson(ExtendedChannelChatImpl.serializer(), decodedJson) + "private" -> formatter.decodeFromJsonElement(ExtendedPrivateChatImpl.serializer(), decodedJson) + "group" -> formatter.decodeFromJsonElement(ExtendedGroupChatImpl.serializer(), decodedJson) + "supergroup" -> formatter.decodeFromJsonElement(ExtendedSupergroupChatImpl.serializer(), decodedJson) + "channel" -> formatter.decodeFromJsonElement(ExtendedChannelChatImpl.serializer(), decodedJson) else -> throw IllegalArgumentException("Unknown type of chat") } } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/dice/DiceAnimationType.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/dice/DiceAnimationType.kt index 0c51776d74..5591aee613 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/dice/DiceAnimationType.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/dice/DiceAnimationType.kt @@ -1,6 +1,9 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.dice import kotlinx.serialization.* +import kotlinx.serialization.descriptors.* +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder @Serializable(DiceAnimationTypeSerializer::class) sealed class DiceAnimationType { @@ -27,7 +30,7 @@ typealias UnknownDiceAnimationType = CustomDiceAnimationType @Serializer(DiceAnimationType::class) internal object DiceAnimationTypeSerializer : KSerializer { - override val descriptor: SerialDescriptor = PrimitiveDescriptor("DiceAnimationType", PrimitiveKind.STRING) + override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("DiceAnimationType", PrimitiveKind.STRING) override fun deserialize(decoder: Decoder): DiceAnimationType { return when (val type = decoder.decodeString()) { CubeDiceAnimationType.emoji -> CubeDiceAnimationType diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/abstracts/Message.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/abstracts/Message.kt index cd9d2d4408..b9eb3b04e3 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/abstracts/Message.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/abstracts/Message.kt @@ -5,6 +5,9 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat import com.github.insanusmokrassar.TelegramBotAPI.types.message.RawMessage import com.soywiz.klock.DateTime import kotlinx.serialization.* +import kotlinx.serialization.descriptors.* +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder interface Message { val messageId: MessageIdentifier @@ -20,9 +23,10 @@ data class UnknownMessageType( ) : Message internal class TelegramBotAPIMessageDeserializationStrategyClass : DeserializationStrategy { - override val descriptor: SerialDescriptor = SerialDescriptor("TelegramBotAPIMessageSerializer", PolymorphicKind.OPEN) + @InternalSerializationApi + override val descriptor: SerialDescriptor = buildSerialDescriptor("TelegramBotAPIMessageSerializer", PolymorphicKind.OPEN) - override fun patch(decoder: Decoder, old: T): T = throw UpdateNotSupportedException("TelegramBotAPIMessageSerializer") + override fun patch(decoder: Decoder, old: T): T = error("TelegramBotAPIMessageSerializer") @Suppress("UNCHECKED_CAST") override fun deserialize(decoder: Decoder): T { @@ -34,6 +38,7 @@ internal object TelegramBotAPIMessageDeserializationStrategy internal class TelegramBotAPIMessageDeserializeOnlySerializerClass : KSerializer { private val deserializer = TelegramBotAPIMessageDeserializationStrategyClass() + @InternalSerializationApi override val descriptor: SerialDescriptor get() = deserializer.descriptor diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/polls/Poll.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/polls/Poll.kt index 8b1f45bd55..21a1636277 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/polls/Poll.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/polls/Poll.kt @@ -7,8 +7,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.nonstrictJsonFormat import com.soywiz.klock.DateTime import com.soywiz.klock.TimeSpan import kotlinx.serialization.* -import kotlinx.serialization.json.JsonObject -import kotlinx.serialization.json.JsonObjectSerializer +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder +import kotlinx.serialization.json.* sealed class ScheduledCloseInfo { abstract val closeDateTime: DateTime @@ -101,11 +103,10 @@ data class UnknownPollType internal constructor( val raw: JsonObject ) : Poll() { @Transient - override val scheduledCloseInfo: ScheduledCloseInfo? = raw.getPrimitiveOrNull( - closeDateField - ) ?.longOrNull ?.asExactScheduledCloseInfo ?: raw.getPrimitiveOrNull( - openPeriodField - ) ?.longOrNull ?.asApproximateScheduledCloseInfo + override val scheduledCloseInfo: ScheduledCloseInfo? = (raw[closeDateField] ?: raw[openPeriodField]) + ?.jsonPrimitive + ?.longOrNull + ?.asApproximateScheduledCloseInfo } @Serializable(PollSerializer::class) @@ -150,8 +151,8 @@ internal object PollSerializer : KSerializer { get() = RawPoll.serializer().descriptor override fun deserialize(decoder: Decoder): Poll { - val asJson = JsonObjectSerializer.deserialize(decoder) - val rawPoll = nonstrictJsonFormat.fromJson(RawPoll.serializer(), asJson) + val asJson = JsonObject.serializer().deserialize(decoder) + val rawPoll = nonstrictJsonFormat.decodeFromJsonElement(RawPoll.serializer(), asJson) return when (rawPoll.type) { quizPollType -> QuizPoll( @@ -218,7 +219,7 @@ internal object PollSerializer : KSerializer { closeDate = (closeInfo as? ExactScheduledCloseInfo) ?.closeDateTime ?.unixMillisLong ?.div(1000L) ) is UnknownPollType -> { - JsonObjectSerializer.serialize(encoder, value.raw) + JsonObject.serializer().serialize(encoder, value.raw) return } } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/polls/PollOption.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/polls/PollOption.kt index c85eb47306..3f9119997d 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/polls/PollOption.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/polls/PollOption.kt @@ -3,6 +3,9 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.polls import com.github.insanusmokrassar.TelegramBotAPI.types.textField import com.github.insanusmokrassar.TelegramBotAPI.types.votesCountField import kotlinx.serialization.* +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder @Serializable(PollOptionSerializer::class) sealed class PollOption { diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/RawUpdate.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/RawUpdate.kt index b5e7f8f395..d6f7ada961 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/RawUpdate.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/RawUpdate.kt @@ -14,6 +14,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update import com.github.insanusmokrassar.TelegramBotAPI.types.updateIdField import kotlinx.serialization.* import kotlinx.serialization.json.JsonElement +import kotlinx.serialization.json.jsonObject @Serializable internal data class RawUpdate constructor( diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/Update.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/Update.kt index 24be238958..3a5212438b 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/Update.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/Update.kt @@ -4,8 +4,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.update.RawUpdate import com.github.insanusmokrassar.TelegramBotAPI.utils.nonstrictJsonFormat import kotlinx.serialization.* +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.json.JsonElement -import kotlinx.serialization.json.JsonElementSerializer interface Update { val updateId: UpdateIdentifier @@ -21,7 +23,7 @@ data class UnknownUpdate( typealias UnknownUpdateType = UnknownUpdate internal object UpdateSerializerWithoutSerialization : KSerializer { - override val descriptor: SerialDescriptor = JsonElementSerializer.descriptor + override val descriptor: SerialDescriptor = JsonElement.serializer().descriptor override fun deserialize(decoder: Decoder): Update = UpdateDeserializationStrategy.deserialize(decoder) @@ -36,17 +38,17 @@ internal object UpdateSerializerWithoutSerialization : KSerializer { * @see kotlinx.serialization.json.Json.parse */ object UpdateDeserializationStrategy : DeserializationStrategy { - override val descriptor: SerialDescriptor = JsonElementSerializer.descriptor - - override fun patch(decoder: Decoder, old: Update): Update = throw UpdateNotSupportedException("Update") + override val descriptor: SerialDescriptor = JsonElement.serializer().descriptor override fun deserialize(decoder: Decoder): Update { - val asJson = JsonElementSerializer.deserialize(decoder) - return nonstrictJsonFormat.fromJson( + val asJson = JsonElement.serializer().deserialize(decoder) + return nonstrictJsonFormat.decodeFromJsonElement( RawUpdate.serializer(), asJson ).asUpdate( asJson ) } + + override fun patch(decoder: Decoder, old: Update): Update = error("Unsupported operation") } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/JSON.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/JSON.kt index 1c26099558..01c3d0f8b5 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/JSON.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/JSON.kt @@ -3,40 +3,42 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils import kotlinx.serialization.SerializationStrategy import kotlinx.serialization.json.* -@Suppress("EXPERIMENTAL_API_USAGE") internal val nonstrictJsonFormat = Json { isLenient = true ignoreUnknownKeys = true - serializeSpecialFloatingPointValues = true + allowSpecialFloatingPointValues = true useArrayPolymorphism = true } fun T.toJsonWithoutNulls(serializer: SerializationStrategy): JsonObject = toJson(serializer).withoutNulls() -fun T.toJson(serializer: SerializationStrategy): JsonObject = nonstrictJsonFormat.toJson( +fun T.toJson(serializer: SerializationStrategy): JsonObject = nonstrictJsonFormat.encodeToJsonElement( serializer, this ).jsonObject fun JsonArray.withoutNulls(): JsonArray { - return jsonArray { + return buildJsonArray { forEach { when (it) { - is JsonObject -> +it.withoutNulls() - is JsonArray -> +it.withoutNulls() - is JsonPrimitive -> +it + is JsonObject -> add(it.withoutNulls()) + is JsonArray -> add(it.withoutNulls()) + is JsonPrimitive -> add(it) } } } } fun JsonObject.withoutNulls(): JsonObject { - return json { + return buildJsonObject { forEach { (k, v) -> when (v) { - is JsonObject -> k to v.withoutNulls() - is JsonArray -> k to v.withoutNulls() - !is JsonNull -> k to v + is JsonObject -> put(k, v.withoutNulls()) + is JsonArray -> put(k, v.withoutNulls()) + !is JsonNull -> put(k, v) + JsonNull -> { + // do nothing + } } } } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeType.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeType.kt index e7ffa19f8d..cf99d80639 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeType.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeType.kt @@ -1,13 +1,36 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils import kotlinx.serialization.* +import kotlinx.serialization.descriptors.* +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder @Serializable(MimeTypeSerializer::class) expect class MimeType { val raw: String } -expect fun buildMimeType(raw: String): MimeType +private val mimesCache = mutableMapOf() + +fun String.asMimeType() = buildMimeType(this) + +internal expect fun createMimeType(raw: String): MimeType +fun buildMimeType(raw: String): MimeType = mimesCache.getOrPut(raw) { + createMimeType(raw) +} @Serializer(MimeType::class) -internal expect object MimeTypeSerializer : KSerializer +internal object MimeTypeSerializer : KSerializer { + override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("mimeType", PrimitiveKind.STRING) + + override fun deserialize(decoder: Decoder): MimeType { + val mimeType = decoder.decodeString() + return mimesCache.getOrPut(mimeType) { + createMimeType(mimeType) + } + } + + override fun serialize(encoder: Encoder, value: MimeType) { + encoder.encodeString(value.raw) + } +} diff --git a/TelegramBotAPI/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/TestsJsonFormat.kt b/TelegramBotAPI/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/TestsJsonFormat.kt index 57d6f8185c..c027bfd689 100644 --- a/TelegramBotAPI/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/TestsJsonFormat.kt +++ b/TelegramBotAPI/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/TestsJsonFormat.kt @@ -1,6 +1,5 @@ package com.github.insanusmokrassar.TelegramBotAPI import kotlinx.serialization.json.Json -import kotlinx.serialization.json.JsonConfiguration -val TestsJsonFormat = Json(JsonConfiguration.Stable) +val TestsJsonFormat = Json {} diff --git a/TelegramBotAPI/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/BotActionTests.kt b/TelegramBotAPI/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/BotActionTests.kt index 4c1cf4d8f3..94b7b2994b 100644 --- a/TelegramBotAPI/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/BotActionTests.kt +++ b/TelegramBotAPI/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/BotActionTests.kt @@ -2,12 +2,10 @@ package com.github.insanusmokrassar.TelegramBotAPI.types import com.github.insanusmokrassar.TelegramBotAPI.TestsJsonFormat import com.github.insanusmokrassar.TelegramBotAPI.types.actions.* -import kotlinx.serialization.ImplicitReflectionSerializer import kotlinx.serialization.Serializable import kotlin.test.Test import kotlin.test.assertEquals -@ImplicitReflectionSerializer class BotActionTests { @Serializable data class Example( @@ -33,10 +31,10 @@ class BotActionTests { } private fun checkBotActionSerializeDeserialize(example: Example) { - val stringified = TestsJsonFormat.stringify(Example.serializer(), example) + val stringified = TestsJsonFormat.encodeToString(Example.serializer(), example) assertEquals("{\"botAction\":\"${example.botAction.actionName}\"}", stringified) - val deserialized = TestsJsonFormat.parse(Example.serializer(), stringified) + val deserialized = TestsJsonFormat.decodeFromString(Example.serializer(), stringified) assertEquals(example, deserialized) @@ -44,7 +42,7 @@ class BotActionTests { } @Test - fun `BotAction correctly serialized and deserialized`() { + fun `BotAction_correctly_serialized_and_deserialized`() { fun BotAction.example() = Example(this) listOf( TypingAction.example(), 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 02005ee51c..07eddca7dd 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,43 +1,38 @@ package com.github.insanusmokrassar.TelegramBotAPI.types import com.github.insanusmokrassar.TelegramBotAPI.TestsJsonFormat -import kotlinx.serialization.ImplicitReflectionSerializer import kotlinx.serialization.Serializable import kotlin.test.* -@ImplicitReflectionSerializer private const val chatIdentifierChatId: Identifier = 123L -@ImplicitReflectionSerializer private const val chatIdentifierLink = "tg://user?id=$chatIdentifierChatId" -@ImplicitReflectionSerializer private const val testUsername = "@Example" -@ImplicitReflectionSerializer class ChatIdentifierTests { @Test - fun `Cast from Int to ChatId is working correctly`() { + fun `Cast_from_Int_to_ChatId_is_working_correctly`() { val chatId = chatIdentifierChatId.toInt().toChatId() assertEquals(chatIdentifierChatId, chatId.chatId) } @Test - fun `Cast from Byte to ChatId is working correctly`() { + fun `Cast_from_Byte_to_ChatId_is_working_correctly`() { val chatId = chatIdentifierChatId.toByte().toChatId() assertEquals(chatIdentifierChatId, chatId.chatId) } @Test - fun `Cast from Identifier to ChatId is working correctly`() { + fun `Cast_from_Identifier_to_ChatId_is_working_correctly`() { val chatId = chatIdentifierChatId.toChatId() assertEquals(chatIdentifierChatId, chatId.chatId) } @Test - fun `Creating link from ChatId is correct`() { + fun `Creating_link_from_ChatId_is_correct`() { val chatId = chatIdentifierChatId.toChatId() assertEquals(chatIdentifierLink, chatId.link) } @Test - fun `Cast from String to Username is working correctly`() { + fun `Cast_from_String_to_Username_is_working_correctly`() { assertEquals(testUsername, testUsername.toUsername().username) assertFails("Username creating must fail when trying to create from string which is not starting from @ symbol") { @@ -47,32 +42,32 @@ class ChatIdentifierTests { @Test - fun `Deserializing from String must work correctly`() { + fun `Deserializing_from_String_must_work_correctly`() { @Serializable data class Example( val identifier: ChatIdentifier ) Example(chatIdentifierChatId.toChatId()).let { withChatId -> - val stringified = TestsJsonFormat.stringify(Example.serializer(), withChatId) + val stringified = TestsJsonFormat.encodeToString(Example.serializer(), withChatId) assertEquals(stringified, "{\"identifier\":$chatIdentifierChatId}") - val deserialized = TestsJsonFormat.parse(Example.serializer(), stringified) + val deserialized = TestsJsonFormat.decodeFromString(Example.serializer(), stringified) assertEquals(withChatId, deserialized) } Example(testUsername.toUsername()).let { withUsername -> - val stringified = TestsJsonFormat.stringify(Example.serializer(), withUsername) + val stringified = TestsJsonFormat.encodeToString(Example.serializer(), withUsername) assertEquals(stringified, "{\"identifier\":\"$testUsername\"}") - val deserialized = TestsJsonFormat.parse(Example.serializer(), stringified) + val deserialized = TestsJsonFormat.decodeFromString(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 = TestsJsonFormat.stringify(Example.serializer(), withUsername).replace("@", "") + val stringified = TestsJsonFormat.encodeToString(Example.serializer(), withUsername).replace("@", "") assertEquals("{\"identifier\":\"${testUsername.replace("@", "")}\"}", stringified) - val deserialized = TestsJsonFormat.parse(Example.serializer(), stringified) + val deserialized = TestsJsonFormat.decodeFromString(Example.serializer(), stringified) assertEquals(withUsername, deserialized) } } diff --git a/TelegramBotAPI/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ParseModeTests.kt b/TelegramBotAPI/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ParseModeTests.kt index 6bacbc57e8..8ba0cc4542 100644 --- a/TelegramBotAPI/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ParseModeTests.kt +++ b/TelegramBotAPI/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ParseModeTests.kt @@ -2,12 +2,10 @@ package com.github.insanusmokrassar.TelegramBotAPI.types import com.github.insanusmokrassar.TelegramBotAPI.TestsJsonFormat import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.* -import kotlinx.serialization.ImplicitReflectionSerializer import kotlinx.serialization.Serializable import kotlin.test.Test import kotlin.test.assertEquals -@ImplicitReflectionSerializer class ParseModeTests { @Serializable data class Example( @@ -15,24 +13,24 @@ class ParseModeTests { ) @Test - fun `Markdown parse mode correctly serializing and deserializing`() { + fun `Markdown_parse_mode_correctly_serializing_and_deserializing`() { val example = Example(Markdown) - val stringified = TestsJsonFormat.stringify(Example.serializer(), example) + val stringified = TestsJsonFormat.encodeToString(Example.serializer(), example) assertEquals("{\"mode\":\"Markdown\"}", stringified) - val deserialized = TestsJsonFormat.parse(Example.serializer(), stringified) + val deserialized = TestsJsonFormat.decodeFromString(Example.serializer(), stringified) assertEquals(example, deserialized) } @Test - fun `HTML parse mode correctly serializing and deserializing`() { + fun `HTML_parse_mode_correctly_serializing_and_deserializing`() { val example = Example(HTML) - val stringified = TestsJsonFormat.stringify(Example.serializer(), example) + val stringified = TestsJsonFormat.encodeToString(Example.serializer(), example) assertEquals("{\"mode\":\"HTML\"}", stringified) - val deserialized = TestsJsonFormat.parse(Example.serializer(), stringified) + val deserialized = TestsJsonFormat.decodeFromString(Example.serializer(), stringified) assertEquals(example, deserialized) } } diff --git a/TelegramBotAPI/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/TelegramDateTests.kt b/TelegramBotAPI/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/TelegramDateTests.kt index faf94bf560..028f56b1cb 100644 --- a/TelegramBotAPI/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/TelegramDateTests.kt +++ b/TelegramBotAPI/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/TelegramDateTests.kt @@ -2,7 +2,6 @@ package com.github.insanusmokrassar.TelegramBotAPI.types import com.github.insanusmokrassar.TelegramBotAPI.TestsJsonFormat import com.soywiz.klock.DateTime -import kotlinx.serialization.ImplicitReflectionSerializer import kotlinx.serialization.Serializable import kotlin.test.Test import kotlin.test.assertEquals @@ -11,20 +10,19 @@ private val dateTimeUnix = DateTime.nowUnixLong() private val dateTimeMillis = dateTimeUnix * 1000 private val dateTime = DateTime(dateTimeMillis) -@ImplicitReflectionSerializer class TelegramDateTests { @Serializable data class Example( val dateTime: TelegramDate ) @Test - fun `Serializtion of TelegramDate is working correctly`() { + fun `Serializtion_of_TelegramDate_is_working_correctly`() { val example = Example(TelegramDate(dateTimeUnix)) - val stringified = TestsJsonFormat.stringify(Example.serializer(), example) + val stringified = TestsJsonFormat.encodeToString(Example.serializer(), example) assertEquals("{\"dateTime\":$dateTimeUnix}", stringified) - val deserialized = TestsJsonFormat.parse(Example.serializer(), stringified) + val deserialized = TestsJsonFormat.decodeFromString(Example.serializer(), stringified) assertEquals(example, deserialized) assertEquals(dateTime, deserialized.dateTime.asDate) diff --git a/TelegramBotAPI/src/jsMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeType.kt b/TelegramBotAPI/src/jsMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeType.kt deleted file mode 100644 index dece7f4e4f..0000000000 --- a/TelegramBotAPI/src/jsMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeType.kt +++ /dev/null @@ -1,35 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.utils - -import kotlinx.serialization.* -import org.w3c.dom.get -import kotlin.browser.window - -private val mimesCache = mutableMapOf() - -@Serializable(MimeTypeSerializer::class) -actual class MimeType( - actual val raw: String -) { - @Transient - val jsMimeType = window.navigator.mimeTypes[raw] - - override fun toString(): String = raw -} - -@Serializer(MimeType::class) -internal actual object MimeTypeSerializer : KSerializer { - override val descriptor: SerialDescriptor = PrimitiveDescriptor("mimeType", PrimitiveKind.STRING) - - override fun deserialize(decoder: Decoder): MimeType { - val mimeType = decoder.decodeString() - return mimesCache.getOrPut(mimeType) { - buildMimeType(mimeType) - } - } - - override fun serialize(encoder: Encoder, value: MimeType) { - encoder.encodeString(value.raw) - } -} - -actual fun buildMimeType(raw: String) = MimeType(raw) diff --git a/TelegramBotAPI/src/jsMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeTypeActual.kt b/TelegramBotAPI/src/jsMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeTypeActual.kt new file mode 100644 index 0000000000..ec54773fda --- /dev/null +++ b/TelegramBotAPI/src/jsMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeTypeActual.kt @@ -0,0 +1,17 @@ +package com.github.insanusmokrassar.TelegramBotAPI.utils + +import kotlinx.serialization.* +import org.w3c.dom.get +import kotlinx.browser.window + +@Serializable(MimeTypeSerializer::class) +actual class MimeType( + actual val raw: String +) { + @Transient + val jsMimeType = window.navigator.mimeTypes[raw] + + override fun toString(): String = raw +} + +actual fun createMimeType(raw: String) = MimeType(raw) diff --git a/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeType.kt b/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeType.kt deleted file mode 100644 index a653618e7a..0000000000 --- a/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeType.kt +++ /dev/null @@ -1,28 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.utils - -import kotlinx.serialization.* - -private val mimesCache = mutableMapOf() - -@Serializable(MimeTypeSerializer::class) -actual class MimeType( - actual val raw: String -) : javax.activation.MimeType(raw) - -@Serializer(MimeType::class) -internal actual object MimeTypeSerializer : KSerializer { - override val descriptor: SerialDescriptor = PrimitiveDescriptor("mimeType", PrimitiveKind.STRING) - - override fun deserialize(decoder: Decoder): MimeType { - val mimeType = decoder.decodeString() - return mimesCache.getOrPut(mimeType) { - MimeType(mimeType) - } - } - - override fun serialize(encoder: Encoder, value: MimeType) { - encoder.encodeString(value.raw) - } -} - -actual fun buildMimeType(raw: String): MimeType = MimeType(raw) diff --git a/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeTypeActual.kt b/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeTypeActual.kt new file mode 100644 index 0000000000..f96206db52 --- /dev/null +++ b/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeTypeActual.kt @@ -0,0 +1,10 @@ +package com.github.insanusmokrassar.TelegramBotAPI.utils + +import kotlinx.serialization.* + +@Serializable(MimeTypeSerializer::class) +actual class MimeType( + actual val raw: String +) : javax.activation.MimeType(raw) + +actual fun createMimeType(raw: String): MimeType = MimeType(raw) diff --git a/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt b/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt index 9f3865c94d..322cfb8f28 100644 --- a/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt +++ b/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt @@ -77,8 +77,8 @@ suspend fun RequestsExecutor.setWebhook( exceptionsHandler ?.invoke(it) } ) { - val asJson = nonstrictJsonFormat.parseJson(call.receiveText()) - val update = nonstrictJsonFormat.fromJson( + val asJson = nonstrictJsonFormat.parseToJsonElement(call.receiveText()) + val update = nonstrictJsonFormat.decodeFromJsonElement( UpdateDeserializationStrategy, asJson ) diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000000..59d75fc3a7 --- /dev/null +++ b/build.gradle @@ -0,0 +1,18 @@ +buildscript { + repositories { + mavenLocal() + jcenter() + mavenCentral() + } + + dependencies { + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version" + classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:$gradle_bintray_plugin_version" + } +} + +plugins { + id "org.jetbrains.kotlin.multiplatform" version "$kotlin_version" apply false + id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version" apply false +} diff --git a/docs/build.gradle b/docs/build.gradle index dc5565324b..5131e7e4b3 100644 --- a/docs/build.gradle +++ b/docs/build.gradle @@ -13,8 +13,8 @@ buildscript { } plugins { - id "org.jetbrains.kotlin.multiplatform" version "$kotlin_version" - id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version" + id "org.jetbrains.kotlin.multiplatform" + id "org.jetbrains.kotlin.plugin.serialization" id "org.jetbrains.dokka" version "$dokka_version" } @@ -26,7 +26,10 @@ repositories { kotlin { jvm() - js() + js { + browser() + nodejs() + } sourceSets { commonMain { diff --git a/gradle.properties b/gradle.properties index 06c8e7e55d..6167792c3d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,14 +1,15 @@ kotlin.code.style=official -kotlin_version=1.3.72 -kotlin_coroutines_version=1.3.8 -kotlin_serialisation_runtime_version=0.20.0 -klock_version=1.11.14 -uuid_version=0.1.1 -ktor_version=1.3.2 +org.gradle.parallel=true +kotlin_version=1.4.0 +kotlin_coroutines_version=1.3.9 +kotlin_serialisation_runtime_version=1.0.0-RC +klock_version=1.12.0 +uuid_version=0.2.0 +ktor_version=1.3.2-1.4.0-rc javax_activation_version=1.1.1 library_group=com.github.insanusmokrassar -library_version=0.28.0 +library_version=0.28.0-rc -gradle_bintray_plugin_version=1.8.4 +gradle_bintray_plugin_version=1.8.5 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 8db0d0d953..fa55e31a18 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.6-bin.zip