您好,我在 kotlin 中类序列化时遇到问题
构建.gradl.kt
...
plugins {
application
kotlin("jvm") version "1.6.21"
kotlin("plugin.serialization").version("1.6.21")
}
...
depenedancies{
...
implementation("io.ktor:ktor-server-content-negotiation:$ktor_version")
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktor_version")
}
Run Code Online (Sandbox Code Playgroud)
响应.kt
import kotlinx.serialization.*
...
interface BaseResponse<T>
@Serializable
data class PaginatedResponse<T>(
val prev: Int?,
val next: Int?,
val totalCount: Int = 0,
val totalPages: Int = 0,
val data: T? = null,
val message: String? = null
) : BaseResponse<T>
Run Code Online (Sandbox Code Playgroud)
用法
...
return PaginatedResponse<List<User>>(
prev,
next,
totalCount,
totalPages,
users
)
Run Code Online (Sandbox Code Playgroud)
kotlinx.serialization.SerializationException: Serializer for class 'PaginatedResponse' is not found.
Mark the …Run Code Online (Sandbox Code Playgroud) 有一个具有默认值的类的示例:
@Serializable
data class TestClass(
val obligatory: String,
val optional: Int = 0
)
Run Code Online (Sandbox Code Playgroud)
它可以从 json 正确反序列化,例如:{ "obligatory":"text", "optional":1 }和{ "obligatory":"text" }。
同时,其序列化的结果必须包含“可选”属性。
序列化的结果:
Json.encodeToString(TestClass("text"))
Run Code Online (Sandbox Code Playgroud)
我期待{ "obligatory":"text", "optional":0 },但现在我有了{ "obligatory":"text" }。
我应该如何更改代码才能达到预期结果?
我有以下数据类
@Serializable
data class Income(val id: String,
val description: String,
val amount: Int,
val Time: Date,
val userId: String)
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试使用 .serializer() 函数时,它说 .serializer() 未为 Income 类定义,因此我的项目无法编译。
val response = Json.stringify(Income.serializer(), Incomes)
call.respond(HttpStatusCode.OK,response)
Run Code Online (Sandbox Code Playgroud)
我看了两遍readme.md 中的文档。甚至还看了KotlinConf的公告视频
有没有人遇到同样的问题。我究竟做错了什么??
编辑:
我试图从 readme.md 复制粘贴样本,但遇到了同样的问题。
import kotlinx.serialization.*
import kotlinx.serialization.json.*
@Serializable
data class Data(val a: Int, val b: String = "42")
fun main() {
// Json also has .Default configuration which provides more reasonable settings,
// but is subject to change in future …Run Code Online (Sandbox Code Playgroud) 我希望我的 Spring Boot 项目使用 kotlinx.serialization。我不知道如何正确交换映射器...如果我想使用 GSON,我可以通过 spring.http.converters.preferred-json-mapper=gson 在 props 中记下它。
有人在这方面取得过成功吗?
我正在使用 Ktor 从 Moshi 转换为 kotlinx 序列化,当我尝试请求获取数据时,我收到此错误
kotlinx.serialization.MissingFieldException: 字段 'attachments' 是必需的,但它丢失了
这是有道理的,因为此特定响应不包含此字段
响应JSON
{
"data": {
"id": "1299418846990921728",
"text": "This is a test"
}
}
Run Code Online (Sandbox Code Playgroud)
但是我的 Serialized 类将该attachments字段设为可空(它仅在需要时才在响应中)所以它应该忽略它我想就像它对 Moshi 所做的那样
@Serializable
data class ResponseData(
val id: Long
val attachments: Attachments?,
val author_id: String?,
val text: String
}
Run Code Online (Sandbox Code Playgroud)
在我的 Ktor 客户端设置中,我将其设置为忽略未知密钥
private val _client: HttpClient = HttpClient(engine) {
install(JsonFeature) {
val json = Json {
this.isLenient = true
this.ignoreUnknownKeys = true
}
serializer = KotlinxSerializer(json)
}
}
Run Code Online (Sandbox Code Playgroud)
为什么它仍然说该字段是必需的,即使它可以为空?
给定如下 json,其中payload对象的结构会有所不同:
{
"id": 1,
"displayName": "Success",
"payload": {
"someProperty": "example",
"someOtherProperty": {
"someNestedProperty": "example"
}
}
}
Run Code Online (Sandbox Code Playgroud)
...使用kotlinx.serialization如何将其反序列化为以下数据类,其中 的值payload应该是有效负载对象的原始 json 字符串。
@Serializable
data class Stub(
val id: Int,
val displayName: String,
val payload: String
)
Run Code Online (Sandbox Code Playgroud) 我想将 json 字符串响应从 API 转换为对象:
val obj = Json.decodeFromString<MyModel>(jsonResponseString)
Run Code Online (Sandbox Code Playgroud)
我的数据类:
@Serializable
data class MyModel(
@SerializedName("field") val field: String
)
Run Code Online (Sandbox Code Playgroud)
它看起来非常简单,并且可以在调试模式下运行!
但是,当编译 AppBundle、以发布模式构建并从 Play Store 内部测试下载应用程序时,我收到以下错误:
Serializer for class '...' is not found. Mark the class as @serializable or provide the
serializer explicitly.
kotlinx.serialization.internal.Platform_commonKt.serializerNotRegistered
Run Code Online (Sandbox Code Playgroud) 这是我的 pojo 类
@Serializable
data class Response(
@SerialName("message") val message: String?,
@SerialName("parameters") val parameters: Map<String, String>?
)
Run Code Online (Sandbox Code Playgroud)
这是 Json,我试图从以下位置解码:
{
"message": "Some text"
}
Run Code Online (Sandbox Code Playgroud)
此处,该字段parameters是可选的。当我尝试解码时
Json.decodeFromString<Response>(response)
Run Code Online (Sandbox Code Playgroud)
我收到以下异常:
kotlinx.serialization.MissingFieldException:序列名“Response”的类型需要字段“parameters”,但它丢失了
我期待将该字段设置parameters为null,如果该字段在Json
我正在尝试初始化 Ktor http 客户端并设置 json 序列化。我需要允许JSON.nonstrict对象允许的非严格反序列化。只是无法了解如何将此设置应用于序列化程序。
val client = HttpClient {
install(JsonFeature) {
serializer = KotlinxSerializer()
}
}
Run Code Online (Sandbox Code Playgroud) 不确定是否可能,但对于我的一生,我不知道如何序列化它。
sealed class ServiceResult<out T : Any> {
data class Success<out T : Any>(val data: T) : ServiceResult<T>()
data class Error(val exception: Exception) : ServiceResult<Nothing>()
}
Run Code Online (Sandbox Code Playgroud)
T 中的所有内容都使用 @Serialized ex:
@Serializable
data class GalleryDTO(
override val id: Int,
override val dateCreated: Long,
override val dateUpdated: Long,
val name:String,
val description:String,
val photos:List<DTOMin>
) : DTO
Run Code Online (Sandbox Code Playgroud) kotlin ×8
json ×3
ktor ×3
android ×2
parsing ×1
sealed-class ×1
serializable ×1
spring-boot ×1