zt
02/08/2023, 10:04 PM@Serializable
data class Teaser(
val text: Text
) {
@Serializable
data class Text(val content: String)
}
How can I get rid of the boxing of text and be able to use it just like a string?Adam S
02/08/2023, 10:58 PM@Serializable
@JvmInline
value class Text(val content: String)
do you mean like a value class?
then you can do
Json.decodeFromString<Teaser>("""
{
"text": "blah blah"
}
""")
zt
02/08/2023, 11:50 PM{
"text": {
"content": "g"
}
}
to this
Teaser(text=g)
Mitchell Syer
02/09/2023, 1:54 AM@Serializable
data class Teaser(
@SerialName("text")
val textObj: Text
) {
constructor(text: String) : this(Text(text))
@Transient // not sure if this is needed
val text: String
get() = textObj.content
@Serializable
data class Text(val content: String)
}
zt
02/09/2023, 2:51 AMMitchell Syer
02/09/2023, 2:52 AMSerializable
annotation is for both serializing and deserializing