I'm using GSON to serialize some platform data. When I use @SerialName to capture platform data with a different naming convention in my app, it works for other types, but not Boolean types. As a simple example, if I have a class like...
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class Person (
@SerialName("first_name") val firstName: String? = null,
@SerialName("last_name") val lastName: String? = null,
val age: Int? = null
)
... everything works fine. The serializer finds first_name, last_name and age in the data and properly set the properties for the Person.
However, when I try to add a Boolean...
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class Person (
@SerialName("first_name") val firstName: String? = null,
@SerialName("last_name") val lastName: String? = null,
val age: Int? = null,
@SerialName("can_sing") val canSing: Boolean? = null
)
... the serializer does not catch and assign can_sing. It is strange that it works with a String but not a Boolean. Can any explain why I am seeing this behavior? I can work around this (for example, I can do val can_sing: Boolean? = null and it works), but I'm just wonder why @SerialName doesn't seem to work for a Boolean, or if I'm just missing something obvious.