Justin
09/25/2019, 3:06 PMList<Any?>
.
In practice, the Any?
can be (a) null
, (b) a String
, (c) an Int
, or (d) another `List<Any?>.
What's the best practice for deserializing this with kotlinx.serialization
?
Currently, I'm doing @ContextualSerialization
like this:
@Serializable
data class Items(
val id: Int,
val values: List<@ContextualSerialization Any?> = emptyList()
)
This seems like the right direction, except I'm not sure how to get from this to an array of usable types (e.g. String, Int, List<Any>)?Dominaezzz
09/25/2019, 3:14 PM@Serializable
data class Items(
val id: Int,
val values: JsonArray
)
?Justin
09/25/2019, 3:15 PMvalues
array into their actual types (when it could be a string, int, or list)?Justin
09/25/2019, 3:17 PMJustin
09/25/2019, 3:18 PM@Serializable
data class Items(
val id: Int,
@Polymorphic val values: Value? = null
) {
sealed class Value {
@Serializable
data class Array(val index: Int, val string: String? = null)
@Serializable
data class String(val text: String? = null)
@Serializable
data class Int(val index: Int? = null)
}
}
Dominaezzz
09/25/2019, 3:18 PMDominaezzz
09/25/2019, 3:18 PMJustin
09/25/2019, 3:18 PMJustin
09/25/2019, 3:18 PMJustin
09/25/2019, 3:19 PMDominaezzz
09/25/2019, 3:19 PMDecoder
to a JsonInput
and call decodeJson()
, then you can inspect the small part of the json tree.Dominaezzz
09/25/2019, 3:20 PM@Polymorphic
wont help here.Justin
09/25/2019, 3:20 PMJustin
09/25/2019, 3:20 PMDominaezzz
09/25/2019, 3:20 PMJustin
09/25/2019, 3:20 PMDominaezzz
09/25/2019, 3:21 PMJustin
09/25/2019, 3:21 PM