In my project I want to deserialize JSON that has androidx.compose.ui.graphics.Color using Gson. My simplified data class looks like this:
data class Colors(val color: Color)
I wanted to add custom adapter for Color type:
fun main() {
val colors = GsonBuilder()
.registerTypeAdapter(Color::class.java, ColorAdapter())
.create()
.fromJson("{color: \"#FFFFFF\"}", Colors::class.java)
println(colors)
}
class ColorAdapter : TypeAdapter<Color>() {
override fun write(out: JsonWriter?, value: Color?) {
TODO("Not yet implemented")
}
override fun read(reader: JsonReader): Color {
val color = reader.nextString()
return Color(color.removePrefix("#").toLong(radix = 16))
}
}
And its read was never invoked, and app crashed because of format, so I tried some other approach. To add adapter like this
.registerTypeHierarchyAdapter(Long::class.java, ColorAdapter())
In that case, read was invoked, but error happened: Exception in thread "main" com.google.gson.JsonSyntaxException: Expected a long but was androidx.compose.ui.graphics.Color
Reason for this is probably inside definition of Color:
@Immutable
@kotlin.jvm.JvmInline
value class Color(val value: ULong)
but in any case, I did not find any solution for that.
Question is how could I accomplish this without changing field and adapter to use Long, as that is my only option for now.
Also defining a
TypeAdapter(orJsonDeserializer) for theColorsclass seems to do the trick. This for example works: