From c9bf84d36a7d6211568b96e6609f208ebdf7d3ee Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 21 Feb 2019 12:07:05 +0800 Subject: [PATCH 1/5] start 0.11.0 --- CHANGELOG.md | 2 ++ build.gradle | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aae807f2bc..863093438f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # TelegramBotAPI changelog +## 0.11.0 + ## 0.10.0 * Most part of abstractions was replaced from `requests` and `types` on more high level diff --git a/build.gradle b/build.gradle index 9bf5d340ff..319391dab6 100644 --- a/build.gradle +++ b/build.gradle @@ -1,4 +1,4 @@ -project.version = "0.10.3" +project.version = "0.11.0" project.group = "com.github.insanusmokrassar" buildscript { From 069d9c29a24ca44765ea45e4bad1ee97b7799293 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 21 Feb 2019 12:10:14 +0800 Subject: [PATCH 2/5] update version of gradle and libs --- CHANGELOG.md | 5 +++++ gradle.properties | 8 ++++---- gradle/wrapper/gradle-wrapper.properties | 4 ++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 863093438f..2181001752 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## 0.11.0 +* Kotlin `1.3.11` -> `1.3.21` +* Kotlin coroutines `1.1.0` -> `1.1.1` +* Kotlin serialization `0.9.1` -> `0.10.0` +* Ktor `1.1.1` -> `1.1.2` + ## 0.10.0 * Most part of abstractions was replaced from `requests` and `types` on more high level diff --git a/gradle.properties b/gradle.properties index 209f0390f7..ca94a1211a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,8 +1,8 @@ kotlin.code.style=official -kotlin_version=1.3.11 -kotlin_coroutines_version=1.1.0 -kotlin_serialisation_runtime_version=0.9.1 +kotlin_version=1.3.21 +kotlin_coroutines_version=1.1.1 +kotlin_serialisation_runtime_version=0.10.0 joda_time_version=2.10.1 -ktor_version=1.1.1 +ktor_version=1.1.2 gradle_bintray_plugin_version=1.8.4 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index bdeff908db..59f3ee8c4c 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed Nov 07 21:46:14 CST 2018 +#Thu Feb 21 12:05:40 HKT 2019 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-bin.zip From 2974a33e5110614865d908c27119c7eb22e3a998 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 21 Feb 2019 14:09:04 +0800 Subject: [PATCH 3/5] fixes in serialization --- build.gradle | 6 ++++++ gradle.properties | 2 ++ .../Ktor/base/MultipartRequestCallFactory.kt | 2 +- .../requests/abstracts/InputFile.kt | 14 ++++++------- .../InputMessageContentSerializer.kt | 20 ++++++++++++++++++ .../abstracts/InputMessageContent.kt | 4 ++++ .../types/InputMedia/InputMedia.kt | 2 ++ .../types/InputMedia/InputMediaSerializer.kt | 21 +++++++++++++++++++ .../types/buttons/InlineKeyboardMarkup.kt | 9 ++++++-- .../types/buttons/KeyboardMarkup.kt | 3 +++ .../types/buttons/KeyboardMarkupSerializer.kt | 15 +++++++++++++ .../TelegramBotAPI/utils/StorageFile.kt | 20 ++++++++++++++++++ 12 files changed, 108 insertions(+), 10 deletions(-) create mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InputMessageContentSerializer.kt create mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/InputMediaSerializer.kt create mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/KeyboardMarkupSerializer.kt create mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt diff --git a/build.gradle b/build.gradle index 319391dab6..78c107f9c6 100644 --- a/build.gradle +++ b/build.gradle @@ -40,3 +40,9 @@ dependencies { // Use JUnit test framework testImplementation 'junit:junit:4.12' } + +compileKotlin { + kotlinOptions { + freeCompilerArgs = [ disableImplicitReflectionSerializerAnnotation ] + } +} diff --git a/gradle.properties b/gradle.properties index ca94a1211a..30d76e621e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,3 +6,5 @@ joda_time_version=2.10.1 ktor_version=1.1.2 gradle_bintray_plugin_version=1.8.4 + +disableImplicitReflectionSerializerAnnotation=-Xexperimental=kotlinx.serialization.ImplicitReflectionSerializer diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/base/MultipartRequestCallFactory.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/base/MultipartRequestCallFactory.kt index 80c2b5f8df..41d4f8be9e 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/base/MultipartRequestCallFactory.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/base/MultipartRequestCallFactory.kt @@ -33,7 +33,7 @@ class MultipartRequestCallFactory : KtorCallFactory { when (value) { is MultipartFile -> append( key, - value.file.inputStream().asInput(), + value.file.asInput(), Headers.build { append(HttpHeaders.ContentType, value.mimeType) append(HttpHeaders.ContentDisposition, "filename=${value.fileId}") diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/InputFile.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/InputFile.kt index 5488db6c0b..fcc7c627ba 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/InputFile.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/InputFile.kt @@ -1,5 +1,6 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts +import com.github.insanusmokrassar.TelegramBotAPI.utils.StorageFile import kotlinx.serialization.* import java.io.File import java.nio.file.Files @@ -30,16 +31,15 @@ object FileIdSerializer : KSerializer { /** * Contains info about file for sending */ +@Serializable data class MultipartFile ( - val file: File, - val mimeType: String = Files.probeContentType(file.toPath()), - val filename: String = file.name + val file: StorageFile, + val mimeType: String = file.contentType, + val filename: String = file.fileName ) : InputFile() { - override val fileId: String by lazy { - "${UUID.randomUUID()}.${file.extension}" - } + override val fileId: String = file.generateCustomName() } fun File.toInputFile(): InputFile = MultipartFile( - this + StorageFile(this) ) diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InputMessageContentSerializer.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InputMessageContentSerializer.kt new file mode 100644 index 0000000000..2114cea456 --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InputMessageContentSerializer.kt @@ -0,0 +1,20 @@ +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.* + +@Serializer(InputMessageContent::class) +object InputMessageContentSerializer : KSerializer { + override fun serialize(encoder: Encoder, obj: InputMessageContent) { + when (obj) { + is InputContactMessageContent -> InputContactMessageContent.serializer().serialize(encoder, obj) + is InputLocationMessageContent -> InputLocationMessageContent.serializer().serialize(encoder, obj) + is InputTextMessageContent -> InputTextMessageContent.serializer().serialize(encoder, obj) + is InputVenueMessageContent -> InputVenueMessageContent.serializer().serialize(encoder, obj) + else -> throw IllegalArgumentException("Unknown for serializing InputContactMessageContent") + } + } + + override fun deserialize(decoder: Decoder): InputMessageContent = throw IllegalStateException("Object can't be deserialized") +} diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/abstracts/InputMessageContent.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/abstracts/InputMessageContent.kt index f36cbb3a20..4b7ad7902a 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/abstracts/InputMessageContent.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/abstracts/InputMessageContent.kt @@ -1,3 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.abstracts +import com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.InputMessageContentSerializer +import kotlinx.serialization.Serializable + +@Serializable(InputMessageContentSerializer::class) interface InputMessageContent diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/InputMedia.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/InputMedia.kt index 03388e0ba5..41ec671ded 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/InputMedia.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/InputMedia.kt @@ -1,9 +1,11 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.InputMedia import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.InputFile +import kotlinx.serialization.Serializable const val inputMediaFileAttachmentNameTemplate = "attach://%s" +@Serializable(InputMediaSerializer::class) interface InputMedia { val type: String val file: InputFile diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/InputMediaSerializer.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/InputMediaSerializer.kt new file mode 100644 index 0000000000..f63bad756b --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InputMedia/InputMediaSerializer.kt @@ -0,0 +1,21 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.InputMedia + +import kotlinx.serialization.* + +@Serializer(InputMedia::class) +object InputMediaSerializer : KSerializer { + override fun serialize(encoder: Encoder, obj: InputMedia) { + when (obj) { + is InputMediaVideo -> InputMediaVideo.serializer().serialize(encoder, obj) + is InputMediaAudio -> InputMediaAudio.serializer().serialize(encoder, obj) + is InputMediaPhoto -> InputMediaPhoto.serializer().serialize(encoder, obj) + is InputMediaAnimation -> InputMediaAnimation.serializer().serialize(encoder, obj) + is InputMediaDocument -> InputMediaDocument.serializer().serialize(encoder, obj) + else -> throw IllegalArgumentException("Can't perform and serialize $obj") + } + } + + override fun deserialize(decoder: Decoder): InputMedia { + throw IllegalStateException("Object can't be deserialized") + } +} diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardMarkup.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardMarkup.kt index 7c5e9e590e..39c5bea691 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardMarkup.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardMarkup.kt @@ -1,11 +1,16 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.buttons import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons.InlineKeyboardButton -import kotlinx.serialization.SerialName -import kotlinx.serialization.Serializable +import kotlinx.serialization.* +import kotlinx.serialization.internal.ArrayListSerializer @Serializable data class InlineKeyboardMarkup( @SerialName("inline_keyboard") + @Serializable(with = KeyboardSerializer::class) val keyboard: Matrix ) : KeyboardMarkup + +object KeyboardSerializer : KSerializer> by ArrayListSerializer( + ArrayListSerializer(ContextSerializer(InlineKeyboardButton::class)) +) diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/KeyboardMarkup.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/KeyboardMarkup.kt index 09629a51be..a8702a797a 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/KeyboardMarkup.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/KeyboardMarkup.kt @@ -1,3 +1,6 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.buttons +import kotlinx.serialization.Serializable + +@Serializable(KeyboardMarkupSerializer::class) interface KeyboardMarkup diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/KeyboardMarkupSerializer.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/KeyboardMarkupSerializer.kt new file mode 100644 index 0000000000..97de7e2ef1 --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/KeyboardMarkupSerializer.kt @@ -0,0 +1,15 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.buttons + +import kotlinx.serialization.* + +@Serializer(KeyboardMarkup::class) +object KeyboardMarkupSerializer : KSerializer { + override fun serialize(encoder: Encoder, obj: KeyboardMarkup) { + when(obj) { + is ForceReply -> ForceReply.serializer().serialize(encoder, obj) + is InlineKeyboardMarkup -> InlineKeyboardMarkup.serializer().serialize(encoder, obj) + is ReplyKeyboardMarkup -> ReplyKeyboardMarkup.serializer().serialize(encoder, obj) + is ReplyKeyboardRemove -> ReplyKeyboardRemove.serializer().serialize(encoder, obj) + } + } +} diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt new file mode 100644 index 0000000000..4173370af3 --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt @@ -0,0 +1,20 @@ +package com.github.insanusmokrassar.TelegramBotAPI.utils + +import kotlinx.io.core.Input +import kotlinx.io.streams.asInput +import kotlinx.serialization.Serializable +import kotlinx.serialization.Transient +import java.io.File +import java.nio.file.Files +import java.util.* + +@Serializable +data class StorageFile( + @Transient + private val file: File = throw IllegalStateException("Can't create object without file") +) { + val contentType: String = Files.probeContentType(file.toPath()) + val fileName: String = file.name + fun generateCustomName(): String = "${UUID.randomUUID()}.${file.extension}" + fun asInput(): Input = Files.newInputStream(file.toPath()).asInput() +} From 313068543f058b737a658a025849a77a305973d0 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 21 Feb 2019 14:21:33 +0800 Subject: [PATCH 4/5] fix warnings --- .../bot/Ktor/KtorRequestsExecutor.kt | 4 +- .../requests/abstracts/InputFile.kt | 4 +- .../requests/chat/modify/SetChatPhoto.kt | 4 +- .../TelegramBotAPI/types/ChatIdentifier.kt | 10 ++--- .../InlineQueryResultSerializer.kt | 44 +++++++++---------- .../types/ParseMode/ParseMode.kt | 8 ++-- .../TelegramBotAPI/types/TelegramDate.kt | 8 ++-- .../TelegramBotAPI/types/actions/BotAction.kt | 8 ++-- .../types/payments/abstracts/Currencied.kt | 8 ++-- .../TelegramBotAPI/utils/JSON.kt | 10 +++-- 10 files changed, 55 insertions(+), 53 deletions(-) diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/KtorRequestsExecutor.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/KtorRequestsExecutor.kt index 8b8eebf996..15ae5c6537 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/KtorRequestsExecutor.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/KtorRequestsExecutor.kt @@ -16,7 +16,7 @@ import io.ktor.client.engine.okhttp.OkHttp import io.ktor.util.cio.toByteArray import kotlinx.coroutines.delay import kotlinx.io.charsets.Charset -import kotlinx.serialization.json.JSON +import kotlinx.serialization.json.Json class KtorRequestsExecutor( token: String, @@ -25,7 +25,7 @@ class KtorRequestsExecutor( callsFactories: List = emptyList(), excludeDefaultFactories: Boolean = false, private val requestsLimiter: RequestLimiter = EmptyLimiter, - private val jsonFormatter: JSON = JSON.nonstrict + private val jsonFormatter: Json = Json.nonstrict ) : BaseRequestsExecutor(token, hostUrl) { constructor( token: String, diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/InputFile.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/InputFile.kt index fcc7c627ba..869e1663e2 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/InputFile.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/InputFile.kt @@ -23,8 +23,8 @@ fun String.toInputFile(): InputFile = FileId(this) @Serializer(FileId::class) object FileIdSerializer : KSerializer { - override fun serialize(output: Encoder, obj: FileId) = output.encodeString(obj.fileId) - override fun deserialize(input: Decoder): FileId = FileId(input.decodeString()) + override fun serialize(encoder: Encoder, obj: FileId) = encoder.encodeString(obj.fileId) + override fun deserialize(decoder: Decoder): FileId = FileId(decoder.decodeString()) } // TODO:: add checks for files size diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/chat/modify/SetChatPhoto.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/chat/modify/SetChatPhoto.kt index 78d766f62e..184b344255 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/chat/modify/SetChatPhoto.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/chat/modify/SetChatPhoto.kt @@ -4,10 +4,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.types.ChatRequ import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.MultipartFile import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.MultipartRequest import com.github.insanusmokrassar.TelegramBotAPI.types.* +import com.github.insanusmokrassar.TelegramBotAPI.utils.toJson import kotlinx.serialization.* import kotlinx.serialization.internal.BooleanSerializer import kotlinx.serialization.json.JsonObject -import kotlinx.serialization.json.JsonTreeMapper @Serializable data class SetChatPhoto ( @@ -19,5 +19,5 @@ data class SetChatPhoto ( override fun method(): String = "setChatPhoto" override fun resultSerializer(): KSerializer = BooleanSerializer override val mediaMap: Map = mapOf(photoField to photo) - override val paramsJson: JsonObject = JsonTreeMapper().writeTree(this, serializer()).jsonObject + override val paramsJson: JsonObject = this.toJson(serializer()) } diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifier.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifier.kt index 127e9964d7..959cc61e02 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifier.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifier.kt @@ -32,8 +32,8 @@ fun String.asUsername(): Username = Username(this) @Serializer(ChatIdentifier::class) internal class ChatIdentifierSerializer: KSerializer { - override fun deserialize(input: Decoder): ChatIdentifier { - val id = input.decodeString() + override fun deserialize(decoder: Decoder): ChatIdentifier { + val id = decoder.decodeString() return id.toLongOrNull() ?.let { ChatId(it) } ?: if (!id.startsWith("@")) { @@ -43,10 +43,10 @@ internal class ChatIdentifierSerializer: KSerializer { } } - override fun serialize(output: Encoder, obj: ChatIdentifier) { + override fun serialize(encoder: Encoder, obj: ChatIdentifier) { when (obj) { - is ChatId -> output.encodeString(obj.chatId.toString()) - is Username -> output.encodeString(obj.username) + is ChatId -> encoder.encodeString(obj.chatId.toString()) + is Username -> encoder.encodeString(obj.username) } } } \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InlineQueryResult/serializers/InlineQueryResultSerializer.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InlineQueryResult/serializers/InlineQueryResultSerializer.kt index 1ac85e94b6..62296d41e5 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InlineQueryResult/serializers/InlineQueryResultSerializer.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InlineQueryResult/serializers/InlineQueryResultSerializer.kt @@ -8,32 +8,32 @@ import sun.reflect.generics.reflectiveObjects.NotImplementedException @Serializer(InlineQueryResult::class) object InlineQueryResultSerializer : KSerializer { - override fun serialize(output: Encoder, obj: InlineQueryResult) { + override fun serialize(encoder: Encoder, obj: InlineQueryResult) { when(obj) { - is InlineQueryResultArticle -> InlineQueryResultArticle.serializer().serialize(output, obj) - is InlineQueryResultAudioCachedImpl -> InlineQueryResultAudioCachedImpl.serializer().serialize(output, obj) - is InlineQueryResultAudioImpl -> InlineQueryResultAudioImpl.serializer().serialize(output, obj) - is InlineQueryResultContact -> InlineQueryResultContact.serializer().serialize(output, obj) - is InlineQueryResultDocumentCachedImpl -> InlineQueryResultDocumentCachedImpl.serializer().serialize(output, obj) - is InlineQueryResultDocumentImpl -> InlineQueryResultDocumentImpl.serializer().serialize(output, obj) - is InlineQueryResultGame -> InlineQueryResultGame.serializer().serialize(output, obj) - is InlineQueryResultGifCachedImpl -> InlineQueryResultGifCachedImpl.serializer().serialize(output, obj) - is InlineQueryResultGifImpl -> InlineQueryResultGifImpl.serializer().serialize(output, obj) - is InlineQueryResultLocation -> InlineQueryResultLocation.serializer().serialize(output, obj) - is InlineQueryResultMpeg4GifCachedImpl -> InlineQueryResultMpeg4GifCachedImpl.serializer().serialize(output, obj) - is InlineQueryResultMpeg4GifImpl -> InlineQueryResultMpeg4GifImpl.serializer().serialize(output, obj) - is InlineQueryResultPhotoCachedImpl -> InlineQueryResultPhotoCachedImpl.serializer().serialize(output, obj) - is InlineQueryResultPhotoImpl -> InlineQueryResultPhotoImpl.serializer().serialize(output, obj) - is InlineQueryResultStickerCached -> InlineQueryResultStickerCached.serializer().serialize(output, obj) - is InlineQueryResultVenue -> InlineQueryResultVenue.serializer().serialize(output, obj) - is InlineQueryResultVideoCachedImpl -> InlineQueryResultVideoCachedImpl.serializer().serialize(output, obj) - is InlineQueryResultVideoImpl -> InlineQueryResultVideoImpl.serializer().serialize(output, obj) - is InlineQueryResultVoiceCachedImpl -> InlineQueryResultVoiceCachedImpl.serializer().serialize(output, obj) - is InlineQueryResultVoiceImpl -> InlineQueryResultVoiceImpl.serializer().serialize(output, obj) + is InlineQueryResultArticle -> InlineQueryResultArticle.serializer().serialize(encoder, obj) + is InlineQueryResultAudioCachedImpl -> InlineQueryResultAudioCachedImpl.serializer().serialize(encoder, obj) + is InlineQueryResultAudioImpl -> InlineQueryResultAudioImpl.serializer().serialize(encoder, obj) + is InlineQueryResultContact -> InlineQueryResultContact.serializer().serialize(encoder, obj) + is InlineQueryResultDocumentCachedImpl -> InlineQueryResultDocumentCachedImpl.serializer().serialize(encoder, obj) + is InlineQueryResultDocumentImpl -> InlineQueryResultDocumentImpl.serializer().serialize(encoder, obj) + is InlineQueryResultGame -> InlineQueryResultGame.serializer().serialize(encoder, obj) + is InlineQueryResultGifCachedImpl -> InlineQueryResultGifCachedImpl.serializer().serialize(encoder, obj) + is InlineQueryResultGifImpl -> InlineQueryResultGifImpl.serializer().serialize(encoder, obj) + is InlineQueryResultLocation -> InlineQueryResultLocation.serializer().serialize(encoder, obj) + is InlineQueryResultMpeg4GifCachedImpl -> InlineQueryResultMpeg4GifCachedImpl.serializer().serialize(encoder, obj) + is InlineQueryResultMpeg4GifImpl -> InlineQueryResultMpeg4GifImpl.serializer().serialize(encoder, obj) + is InlineQueryResultPhotoCachedImpl -> InlineQueryResultPhotoCachedImpl.serializer().serialize(encoder, obj) + is InlineQueryResultPhotoImpl -> InlineQueryResultPhotoImpl.serializer().serialize(encoder, obj) + is InlineQueryResultStickerCached -> InlineQueryResultStickerCached.serializer().serialize(encoder, obj) + is InlineQueryResultVenue -> InlineQueryResultVenue.serializer().serialize(encoder, obj) + is InlineQueryResultVideoCachedImpl -> InlineQueryResultVideoCachedImpl.serializer().serialize(encoder, obj) + is InlineQueryResultVideoImpl -> InlineQueryResultVideoImpl.serializer().serialize(encoder, obj) + is InlineQueryResultVoiceCachedImpl -> InlineQueryResultVoiceCachedImpl.serializer().serialize(encoder, obj) + is InlineQueryResultVoiceImpl -> InlineQueryResultVoiceImpl.serializer().serialize(encoder, obj) } } - override fun deserialize(input: Decoder): InlineQueryResult { + override fun deserialize(decoder: Decoder): InlineQueryResult { throw NotImplementedException() } } \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ParseMode/ParseMode.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ParseMode/ParseMode.kt index 6df7fd1aa5..94a1357a41 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ParseMode/ParseMode.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ParseMode/ParseMode.kt @@ -25,8 +25,8 @@ object HTMLParseMode : ParseMode() { @Serializer(ParseMode::class) internal class ParseModeSerializerObject: KSerializer { - override fun deserialize(input: Decoder): ParseMode { - val mode = input.decodeString() + override fun deserialize(decoder: Decoder): ParseMode { + val mode = decoder.decodeString() return when (mode) { MarkdownParseMode.parseModeName -> MarkdownParseMode HTMLParseMode.parseModeName -> HTMLParseMode @@ -34,7 +34,7 @@ internal class ParseModeSerializerObject: KSerializer { } } - override fun serialize(output: Encoder, obj: ParseMode) { - output.encodeString(obj.parseModeName) + override fun serialize(encoder: Encoder, obj: ParseMode) { + encoder.encodeString(obj.parseModeName) } } diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/TelegramDate.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/TelegramDate.kt index 5f6bb99c93..b83ec0ace6 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/TelegramDate.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/TelegramDate.kt @@ -22,15 +22,15 @@ fun DateTime.toTelegramDate(): TelegramDate = TelegramDate(this) @Serializer(TelegramDate::class) internal class TelegramDateSerializer: KSerializer { - override fun serialize(output: Encoder, obj: TelegramDate) { - output.encodeLong( + override fun serialize(encoder: Encoder, obj: TelegramDate) { + encoder.encodeLong( TimeUnit.MILLISECONDS.toSeconds(obj.asDate.millis) ) } - override fun deserialize(input: Decoder): TelegramDate { + override fun deserialize(decoder: Decoder): TelegramDate { return TelegramDate( - input.decodeLong() + decoder.decodeLong() ) } } diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/actions/BotAction.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/actions/BotAction.kt index 1b5ad39641..4dde4ccf52 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/actions/BotAction.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/actions/BotAction.kt @@ -20,12 +20,12 @@ sealed class BotAction { @Serializer(BotAction::class) class BotActionSerializer: KSerializer { - override fun serialize(output: Encoder, obj: BotAction) { - output.encodeString(obj.actionName) + override fun serialize(encoder: Encoder, obj: BotAction) { + encoder.encodeString(obj.actionName) } - override fun deserialize(input: Decoder): BotAction { - val actionName = input.decodeString() + override fun deserialize(decoder: Decoder): BotAction { + val actionName = decoder.decodeString() return actions.firstOrNull { it.actionName == actionName } ?: throw IllegalStateException("Unknown action type: $actionName") } } diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/payments/abstracts/Currencied.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/payments/abstracts/Currencied.kt index b0184bda99..15c523d6ad 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/payments/abstracts/Currencied.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/payments/abstracts/Currencied.kt @@ -9,12 +9,12 @@ interface Currencied { @Serializer(Currency::class) object CurrencySerializer : KSerializer { - override fun serialize(output: Encoder, obj: Currency) { - output.encodeString(obj.currencyCode) + override fun serialize(encoder: Encoder, obj: Currency) { + encoder.encodeString(obj.currencyCode) } - override fun deserialize(input: Decoder): Currency { - return input.decodeString().let { + override fun deserialize(decoder: Decoder): Currency { + return decoder.decodeString().let { Currency.getInstance(it) } } diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/JSON.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/JSON.kt index 792b70cf2a..45bc692189 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/JSON.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/JSON.kt @@ -4,13 +4,15 @@ import kotlinx.serialization.* import kotlinx.serialization.json.* @ImplicitReflectionSerializer -inline fun T.toJsonWithoutNulls(): JsonObject = toJson(serializerByValue(this)).withoutNulls() +inline fun T.toJsonWithoutNulls(): JsonObject = Json.nonstrict.toJson( + this +).jsonObject.withoutNulls() inline fun T.toJsonWithoutNulls(serializer: KSerializer): JsonObject = toJson(serializer).withoutNulls() -inline fun T.toJson(serializer: KSerializer): JsonObject = JsonTreeMapper().writeTree( - this, - serializer +inline fun T.toJson(serializer: KSerializer): JsonObject = Json.nonstrict.toJson( + serializer, + this ).jsonObject fun JsonArray.withoutNulls(): JsonArray { From b39232ae479b2f5069638d0cd49d327d241ad957 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 21 Feb 2019 15:48:49 +0800 Subject: [PATCH 5/5] add SimpleRequestSerializer --- .../bot/Ktor/base/SimpleRequestCallFactory.kt | 5 ++--- .../TelegramBotAPI/requests/abstracts/SimpleRequest.kt | 7 +++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/base/SimpleRequestCallFactory.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/base/SimpleRequestCallFactory.kt index b50a8a83e5..48b4d7e21a 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/base/SimpleRequestCallFactory.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/base/SimpleRequestCallFactory.kt @@ -1,8 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.base import com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.KtorCallFactory -import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request -import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest +import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.* import com.github.insanusmokrassar.TelegramBotAPI.utils.toJsonWithoutNulls import io.ktor.client.HttpClient import io.ktor.client.call.HttpClientCall @@ -27,7 +26,7 @@ class SimpleRequestCallFactory : KtorCallFactory { method = HttpMethod.Post accept(ContentType.Application.Json) - val content = request.toJsonWithoutNulls().toString() + val content = request.toJsonWithoutNulls(SimpleRequestSerializer).toString() body = TextContent( content, diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/SimpleRequest.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/SimpleRequest.kt index b1aab229dd..5a0a8f7f02 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/SimpleRequest.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/SimpleRequest.kt @@ -1,3 +1,10 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts +import kotlinx.serialization.ContextSerializer +import kotlinx.serialization.KSerializer +import kotlinx.serialization.Serializable + +@Serializable(SimpleRequestSerializer::class) interface SimpleRequest : Request + +object SimpleRequestSerializer : KSerializer> by ContextSerializer(SimpleRequest::class)