https://kotlinlang.org logo
#serialization
Title
# serialization
m

Mohammed Akram Hussain

10/04/2023, 4:42 PM
I'm trying to deserialise a json string as enum but it is always null. Although the same model works when I replace
@SerialName
with
@SerialisedName
from gson for the enum values. Also, it works when I change the enum name to all lower case which matches the response value. Any ideas what I'm doing wrong?
Copy code
@Serializable
data class Colours(
    @SerialName("palette")
    val palette: Palette,
    @SerialName("theme")
    val theme: ColoursTheme = ColoursTheme.White
)

@Serializable
enum class ColoursTheme {
    @SerialName("white")
    White,

    @SerialName("bold")
    Bold,

    @SerialName("black")
    Black,

    @SerialName("accent")
    Accent
}
This is the JSON:
Copy code
{
	"colours": {
		"theme": "accent",
		"palette": {
			"primaryHex": "#537e66",
			"secondaryHex": "#f3f3eb",
			"accentHex": "#537e66"
		}
	},
}
c

Chris Fillmore

10/04/2023, 4:58 PM
What do you mean, it is always null? In your data class,
val theme: ColoursTheme
is not nullable
m

Mohammed Akram Hussain

10/04/2023, 4:59 PM
yeah but the object i get is
Colours(palette = paletterObj, theme = null)
which is strange.