rya*_*oon 8 serialization kotlin
Serializable我在数据类上有一个简单的用法
@Serializable
data class Message(
val type: String,
val topics: List<String>,
val content: String
)
Run Code Online (Sandbox Code Playgroud)
但IDE显示以下警告
kotlinx.serialization compiler plugin is not applied to the module, so this annotation would not be processed. Make sure that you've setup your buildscript correctly and re-import project.
Run Code Online (Sandbox Code Playgroud)
在运行时我收到
Serializer for class 'Message' is not found.
Please ensure that class is marked as '@Serializable' and that the serialization compiler plugin is applied.
Run Code Online (Sandbox Code Playgroud)
我读过无数其他 StackOverflow 帖子,其中的问题是其设置不正确build.gradle.kts,但我仔细检查了我是否正在导入插件和依赖项。下面是我的build.gradle.kts
val ktor_version: String by project
val kotlin_version: String by project
val logback_version: String by project
plugins {
kotlin("jvm") version "1.9.0"
kotlin("plugin.serialization") version "1.9.0"
id("io.ktor.plugin") version "2.3.2"
}
group = "com.ryandyoon"
version = "0.0.1"
application {
mainClass.set("com.ryandyoon.ApplicationKt")
val isDevelopment: Boolean = project.ext.has("development")
applicationDefaultJvmArgs = listOf("-Dio.ktor.development=$isDevelopment")
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
implementation("io.ktor:ktor-server-core-jvm:$ktor_version")
implementation("io.ktor:ktor-server-netty-jvm:$ktor_version")
implementation("io.ktor:ktor-server-websockets:$ktor_version")
implementation("ch.qos.logback:logback-classic:$logback_version")
testImplementation("io.ktor:ktor-server-tests-jvm:$ktor_version")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
}
Run Code Online (Sandbox Code Playgroud)
如果它有影响的话,是的,我在 ktor 项目中使用它,但我只是简单地调用
Json.decodeFromString<Message>(messageString)
Run Code Online (Sandbox Code Playgroud)
小智 7
我能够解决警告“kotlinx.serialization 编译器插件未应用于模块,因此不会处理此注释。请确保您已正确设置构建脚本并重新导入项目。” 通过在项目级 build.gradle.kts中添加序列化插件,然后在模块级 build.gradle.kts 中应用它。
项目build.gradle.kts:
plugins {
...
kotlin("plugin.serialization") version "1.9.0" apply false
}
Run Code Online (Sandbox Code Playgroud)
模块build.gradle.kts:
plugins {
...
kotlin("plugin.serialization")
}
...
dependencies {
...
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3281 次 |
| 最近记录: |