Hi, I’ve a use case wherein I want to parse two different attributes from a json into a single variable. It is guaranteed that I’ll be getting only one of such attribute at a time. For example my json will either be:
{"id1" : "1", "name" : "xyz"}
or
{"id2" : "1", "name" : "xyz"}
and my DTO is
data class MyObj(val id: String, val name: String)
. Is it possible to address such a use case using kotlinx serialization?
h
hfhbd
07/21/2022, 6:40 AM
Only deserialization?
Copy code
fun main() {
Json.decodeFromString(Main.serializer(), """{"id1": 1, "name": "foo"}""")
Json.decodeFromString(Main.serializer(), """{"id2": 1, "name": "foo"}""")
}
@Serializable
data class Main(
@JsonNames("id1", "id2")
val id: Int,
val name: String
)