1.4-M1
で試そうとしたのですが、おそらく、kotlinx.serialization
のコード生成部分がうまく動かなかったので 1.3.71
で行いました。
gradleのバージョンは 5.6.1
。
Slackのコマンドのリクエストをパースしてみました。
コード
build.gradle.kts
plugins { kotlin("js") version "1.3.71" kotlin("plugin.serialization") version "1.3.71" } group = "org.example" version = "1.0-SNAPSHOT" repositories { mavenCentral() } dependencies { implementation("org.jetbrains.kotlin:kotlin-stdlib-js") testImplementation("org.jetbrains.kotlin:kotlin-test-js") val serializationVersion = "0.20.0" implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:$serializationVersion") } kotlin { target { browser { } } }
SlackResponse.kt
import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @Serializable data class SlackResponse( @SerialName("team_id") val teamId: String, @SerialName("text") val text: String, @SerialName("user_name") val userName: String, @SerialName("user_id") val userId: String, @SerialName("command") val command: String, @SerialName("channel_name") val channelName: String, @SerialName("trigger_id") val triggerId: String, @SerialName("channel_id") val channelId: String, @SerialName("response_url") val responseUrl: String, @SerialName("token") val token: String, @SerialName("team_domain") val teamDomain: String )
Main.kt
import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonConfiguration fun main() { val json = """ { "team_id": "", "text": "任意のテキスト", "user_name": "matsudamper", "user_id": "", "command": "/command_name", "channel_name": "", "trigger_id": "", "channel_id": "", "response_url": "https://hooks.slack.com/commands/", "token": "", "team_domain": "" } """.trimIndent() // 1.4 -> Json().decodeFromString(SlackResponse.serializer(), json) val result = Json(JsonConfiguration.Stable).parse(SlackResponse.serializer(), json) println(result) }