kotlinx-serialization-json Please ensure that class is marked as '@Serializable' and that the serialization compiler plugin is applied.

Please ensure that class is marked as '@Serializable' and that the serialization compiler plugin is applied.

 

@Serializable
data class PersonModel(val name:String,val age:Int)

val personModel = PersonModel("xxx", 18)
val json = Json.encodeToString(personModel)
println("json:$json")

需要默认值的(或者 字段加 @EncodeDefault)
Json{encodeDefaults=true}.encodeToString(personModel)

 

 

主目录
plugins {
    ...
    kotlin("plugin.serialization") version "1.5.31" apply false
}

模块
plugins {
....
kotlin("plugin.serialization")
}

implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.9.0")

  

参考:https://dev59.com/yVR1hooBIXSAPYfQdMeZ

参考:https://www.baeldung-cn.com/kotlin/data-class-json-serialize-default-values

posted on 2026-01-21 09:32  少杨  阅读(0)  评论(0)    收藏  举报