-
Notifications
You must be signed in to change notification settings - Fork 500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG]Kotlin数据类is开头字段序列化丢失 #2069
Comments
补充下jackson的结果, jackson能按照预期输出 import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.kotlinModule
import org.junit.jupiter.api.Test
class TestJackson {
data class User(
val id: Long?,
val username: String?,
val isTrue: String?,
val isMain: Int?,
val isBoolean: Boolean?
) {
var isNormal: String? = null
override fun toString(): String {
return "User(id=$id, username=$username, isTrue=$isTrue, isMain=$isMain, isBoolean=$isBoolean, isNormal=$isNormal)"
}
}
@Test
fun test() {
val mapper = ObjectMapper()
mapper.registerModule(kotlinModule())
val user = User(1, "lili", "是", 1, true)
user.isNormal = "否"
var jsonStr = mapper.writeValueAsString(user)
println(jsonStr) // 输出 {"id":1,"username":"lili","isTrue":"是","isMain":1,"isBoolean":true,"isNormal":"否"}
jsonStr = "{\"id\":1,\"username\":\"lili\",\"isTrue\":\"是\",\"isMain\":1,\"isBoolean\": true,\"isNormal\":\"否\"}"
val parseObject = mapper.readValue(jsonStr, User::class.java)
println(parseObject) // 输出 User(id=1, username=lili, isTrue=是, isMain=1, isBoolean=true, isNormal=否)
}
} |
新版本is开头的字段正常了,但是测试还发现一个问题,就是is开头的字段@JSONField(name=)取别名不生效 import com.alibaba.fastjson2.JSON
import com.alibaba.fastjson2.annotation.JSONField
import org.junit.Test
class TestJSONField {
class TestBean {
@JSONField(name = "nnn")
var isNormal: String? = "yes"
@JSONField(name = "nnn2")
var normal2: String? = "yes"
override fun toString(): String {
return "User(isNormal=$isNormal, normal2=$normal2)"
}
}
@Test
fun test() {
val testBean = TestBean()
println(testBean)
println(JSON.toJSONString(testBean)) // 预期输出 {"nnn":"yes","nnn2":"yes"}
val json = """{"nnn":"no","nnn2":"yes"}"""
println(JSON.parseObject(json, TestBean::class.java)) // 预期输出User(isNormal=no, normal2=yes)
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
问题描述
简要描述您碰到的问题。
data class中如果有字段是以is开头的,序列化会丢失,反序列化正常;
不在构造方法里的字段,序列化和反序列化都丢失;
kotlin反射依赖已经添加。
环境信息
请填写以下信息:
重现步骤
如何操作可以重现该问题:
期待的正确结果
对您期望发生的结果进行清晰简洁的描述。
序列化输出{"id":1,"username":"lili","isTrue":"是","isMain":1,"isBoolean": true,"isNormal":"否"}
反序列化输出User(id=1, username=lili, isTrue=是, isMain=1, isBoolean=true, isNormal=否)
相关日志输出
请复制并粘贴任何相关的日志输出。
附加信息
如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。
The text was updated successfully, but these errors were encountered: