0

My response returning colors as strings. So I can get background color like "home_background" etc. I then take string and convert it to color from resource with same name. I do not want to do this in View ( Composables ) I want to create gson deserializer to parse it. Here is my data class:

data class QuestionTile(
    override val id: String,
    override val type: String,
    override val author: Author,
    override val createdDate: String,
    override var text: PostText?,
    override val topComment: Comment?,
    override val seeAllCommentsAction: DeeplinkAction?,
    override val allComments: List<Comment?>?,
    override val tags: List<Tag>?,
    val reactions: PostReactions,
    val description: String?,
    val backgroundColor: Color?,
    val textColor: Color?
)

data class Color(
    val color: Int
)

And i am not sure how to make deserializer that will take jsonObject["backgroundColor"].asString or jsonObject["textColor"].asString. Maybe tomorrow i will have new field like weirdColor and i just want to make my deserializer somehow dynamic.

Jalson1982
  • 287
  • 4
  • 14
  • You'll not map `String` to `Color?`, unless statically assigning it... just send integer ARGB values. – Martin Zeitler Jul 06 '22 at 21:40
  • My backend do not provide me integer ARGB values then string color name. Or I misunderstood you? I have update question. – Jalson1982 Jul 06 '22 at 21:41
  • `Color` nevertheless has no annotations nor an idea what these strings mean... the most simple might be to add `string-array` and `integer-array`, so that these strings can be mapped to `@IdRes`. – Martin Zeitler Jul 06 '22 at 21:43
  • Hmm not sure that I 100% follow you. Can you maybe provide some code? Sorry maybe I just dumb. – Jalson1982 Jul 06 '22 at 21:47
  • You can't deserialize dynamic keys... Try with map... Might be helpful this answer https://stackoverflow.com/questions/6796662/using-gson-to-parse-a-json-with-dynamic-key-and-value-in-android – Gobu CSG Jul 07 '22 at 07:38

0 Answers0