Kotlin的`?:`在数据类字段/属性不为null但Gson将其设置为null时是否起作用?

huangapple go评论103阅读模式
英文:

Does Kotlin '?:' works when data class field/property is not null but Gson set it null?

问题

The code below shows that gson.fromJson() will ignore non-null properties of Kotlin data classes.

data class Test(val txt: String)

val gson = Gson()
val testClass = gson.fromJson("{\"txt\":null}", Test::class.java)

In the given code, the testClass.txt property will be null. When using val txtNotNull = testClass.txt ?: "not null", it's a way to handle null values. Whether it's bad code style or unwise to use Gson in Kotlin depends on the specific use case and project requirements.

英文:

The code bellow shows gson.fromJson() will ignore kotlin data class not null.

data class Test(val txt:String)

val gson=Gson()
val testClass=gson.fromJson("{\"txt\":null}",Test::class.java)

The testClass in the code will get a null txt property. It can be understood, because the null check is happened in compile.

But what if val txtNotNull=testClass.txt?:"not null"?

Is it a bad code style(undefined behavior), or is it unwise to use gson in Kotlin?

答案1

得分: 1

Gson使用反射来设置数据类的属性。这就是为什么在对象构造期间不运行Kotlin检查的原因。然后,你可以在Kotlin中将null转换为非空类型。

你的代码会工作,但你会收到编译器的警告:

Elvis运算符(?:)始终返回非空类型String的左操作数

还有一个很棒的官方库kotlinx.serialization,专为在Kotlin中使用而编写。

英文:

Gson uses reflection to set properties to the data class. That's why Kotlin checks aren't run during object construction. And then, you can get null into not-null type in Kotlin.

Your code will work, but you will get a warning from the compiler:
> Elvis operator (?:) always returns the left operand of non-nullable type String

There is a cool official library kotlinx.serialization which is written for use in Kotlin.

答案2

得分: 1

Gson不了解Kotlin的非空类型。

您可以创建自己的TypeAdapterFactory,如果JSON中的任何值(在Kotlin对象中应为非空)为null,则抛出异常。

请参阅此文章:

https://medium.com/swlh/using-gson-with-kotlins-non-null-types-468b1c66bd8b

英文:

Gson doesn't know about kotlin's non-null types.

You can create you own TypeAdapterFactory to throw an exception if any of the values in json (which should be not null in Kotlin object) is null.

See this post :

https://medium.com/swlh/using-gson-with-kotlins-non-null-types-468b1c66bd8b

答案3

得分: 0

The fact that Gson is sneaking a null value in there is not the problem. The problem is that you’re marking a property as non-nullable that should be nullable because it can be omitted in the JSON that you’re importing. You should make the property nullable, and then it will actually make sense if you use something like val txtNotNull = testClass.txt ?: "not null".

英文:

The fact that Gson is sneaking a null value in there is not the problem. The problem is that you’re marking a property as non-nullable that should be nullable because it can be omitted in the JSON that you’re importing. You should make the property nullable, and then it will actually make sense if you use something like val txtNotNull=testClass.txt?:"not null".

huangapple
  • 本文由 发表于 2023年5月11日 14:38:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76224766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定