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

kotlin : toJson return null #1828

Closed
zhaoxiong123 opened this issue Nov 27, 2020 · 3 comments
Closed

kotlin : toJson return null #1828

zhaoxiong123 opened this issue Nov 27, 2020 · 3 comments

Comments

@zhaoxiong123
Copy link

zhaoxiong123 commented Nov 27, 2020

`
val gson = Gson()

        data class TestModel(
            val id: Int,
            val description: String
        )

        val jsonString = gson.toJson(TestModel(1,"Test"))

        println(jsonString)

`
why jsonString is null ?
who knows ?
my kotlin version is 1.4.10.
my jdk version is 1.8

@yerlikayao
Copy link

Gson doesn't support serializing anonymous types.

https://stackoverflow.com/questions/39538677/why-does-gson-tojson-always-return-null

@Richyeoh
Copy link

test_gson

我测试了一下,匿名类或者局部类都会有这个问题。
可能Kotlin不支持吧。

@Marcono1234
Copy link
Collaborator

As mentioned above, most likely Gson's default behavior for anonymous and local classes caused the null return value.

However, in the latest Gson versions this might actually be fixed for local Kotlin data classes due to #1969.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants