Skip to content
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

Closed
initchen opened this issue Dec 2, 2023 · 3 comments
Closed

[BUG]Kotlin数据类is开头字段序列化丢失 #2069

initchen opened this issue Dec 2, 2023 · 3 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@initchen
Copy link

initchen commented Dec 2, 2023

问题描述

简要描述您碰到的问题。
data class中如果有字段是以is开头的,序列化会丢失,反序列化正常;
不在构造方法里的字段,序列化和反序列化都丢失;
kotlin反射依赖已经添加。

环境信息

请填写以下信息:

  • OS信息: [e.g.:Android]
  • JDK信息: [e.g.:Openjdk 1.8.0_312]
  • 版本信息:[e.g.:Fastjson2 2.0.40.android4]

重现步骤

如何操作可以重现该问题:

class TestJson {

    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 user = User(1, "lili", "是", 1, true)
        user.isNormal = "否"
        var jsonStr = JSON.toJSONString(user)
        println(jsonStr) // 输出 {"boolean":true,"id":1,"username":"lili"}
        jsonStr = "{\"id\":1,\"username\":\"lili\",\"isTrue\":\"\",\"isMain\":1,\"isBoolean\": true,\"isNormal\":\"\"}"
        val parseObject = JSON.parseObject(jsonStr, User::class.java)
        println(parseObject)
    }
}

期待的正确结果

对您期望发生的结果进行清晰简洁的描述。
序列化输出{"id":1,"username":"lili","isTrue":"是","isMain":1,"isBoolean": true,"isNormal":"否"}
反序列化输出User(id=1, username=lili, isTrue=是, isMain=1, isBoolean=true, isNormal=否)

相关日志输出

请复制并粘贴任何相关的日志输出。

附加信息

如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。

@initchen initchen added the bug Something isn't working label Dec 2, 2023
@initchen
Copy link
Author

initchen commented Dec 2, 2023

补充下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=否)
    }
}

@wenshao wenshao modified the milestones: 2.0.43, 2.0.44 Dec 2, 2023
@wenshao wenshao added the fixed label Dec 23, 2023
@wenshao
Copy link
Member

wenshao commented Dec 24, 2023

@wenshao wenshao closed this as completed Dec 24, 2023
@initchen
Copy link
Author

https://github.com/alibaba/fastjson2/releases/tag/2.0.44 请用新版本

新版本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
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants