google / google/gson

Support users to handle null types

Open
#2,119 3 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Java
Stars
24.2k
Forks
4.5k
Avg merge
6d 4h
Merged PRs (30d)
12

Description

# Problem solved by the feature

`class Hello {
@SerializedName("worlds")
@JvmField
val worlds = ArrayList()
@SerializedName("world")
@JvmField
val world = World()
@SerializedName("word")
@JvmField
var word = Word()
}
fun main() {
val hello = Gson().fromJson("{\"worlds\":null, \"world\":null, \"word\":null}", Hello::class.java)

println(hello.worlds) // null, this is not expected
println(hello.world) // null, this is not expected
println(hello.word) // null, ok
}
`
Since the field is final, I hope that when the value of "worlds" in json is null, no processing will be done.
When the value of "worlds" is not empty, the effect is like "worlds.add(xxx)".
Now the ArrayList instance is created and replaced by java.lang.reflect.Field.set(xxx,xxx).

# Feature description

excludeField cannot satisfy, because it's not based on parsing the scene.
ReflectiveExTypeAdapterFactory.java:162
`
if (customPolicy.shouldFieldSet(value, fieldValue, field, isPrimitive)) {
field.set(value, fieldValue);
}
`

# Alternatives / workarounds

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.