solonovamax
06/04/2023, 6:52 PM{
"player": {
"foo": "abcd",
"bar": "abcd",
"selected_killstreak_0": "data",
"selected_killstreak_1": "data",
"selected_killstreak_2": "data",
"selected_killstreak_3": "data"
}
}
and, I would like to deserialize it into a class that looks like the following:
@Serializable
data class Player(
val foo: String,
val bar: String,
val selectedKillStreaks: List<String>
)
is this possible? Or, would I have to deserialize it into a class like this:
@Serializable
data class Player(
val foo: String,
val bar: String,
@SerialName("selected_killstreak_0")
val selectedKillstreak0: String,
@SerialName("selected_killstreak_1")
val selectedKillstreak1: String,
@SerialName("selected_killstreak_2")
val selectedKillstreak2: String,
@SerialName("selected_killstreak_3")
val selectedKillstreak3: String,
)
(note: in some cases there will always be a known number of items in the form name_[n]
, where [n]
is the index, however in other cases there are an arbitrary number of items in that same form. I would like to ideally be able to deserialize for an arbitrary length)
yes, I know that this json is very cursed, but there's nothing I can do about itAdam S
06/04/2023, 7:52 PMAdam S
06/04/2023, 8:11 PMsolonovamax
06/04/2023, 8:13 PMselected_killsteak_n
values aren't necessarily in order. sometimes they can be in the order, selected_killstreak_2
, selected_killstreak_3
, selected_killstreak_1
, selected_killstreak_0
(because why not!)solonovamax
06/04/2023, 8:17 PMname_[n]
occurs many times in the json I want to decode, and I'd prefer to avoid having to re-write this every time I need to deserialized a cursed list like this
would there be any way to make it a json transforming serializer for List<T>
(it's not always string, and sometimes can be an object) so that I can just do smth like
// ...
@SerialName("selected_killstreak_")
@Serializable(CursedListDeserializer::class)
val selectedKillstreak: List<String>,
// ...
Adam S
06/04/2023, 8:45 PMselectedKillStreaks.sortedBy { it.key }
in there somewhere to make the sorting workAdam S
06/04/2023, 8:47 PMephemient
06/05/2023, 12:26 AM