Situation
I'm trying to decode a simple JSON object of the form:
{
"firstArray": [
"element1",
"element2"
],
"secondArray": [
"elementA",
"elementB",
"elementC"
],
"*": [
"bestElement"
]
}
The result should be a custom object named ChainStore
that contains an array of Chain
instances. Each Chain
holds an identifier and array of strings. See below.
I'd expect the array of chains in my ChainStore
to be of the same order defined in the input JSON. For some reason, though, it gets the position of the special *
chain wrong. All other elements stay in their defined position, so I'm pretty confident that it's not just ordered for some reason.
The actual result, if I just print the arrays, looks like this:
*
bestElement
secondArray
elementA
elementB
elementC
firstArray
element1
element2
I'm pretty sure I missed something in the documentation here.
Any idea why it doesn't respect my order or why it applies this seemingly random order?
Thanks!
Full example
var json = """
{
"firstArray": [
"element1",
"element2"
],
"secondArray": [
"elementA",
"elementB",
"elementC"
],
"*": [
"bestElement"
]
}
""".data(using: .utf8)!
struct ChainService: Decodable {
let chains: [Chain]
struct ChainKey: CodingKey {
var stringValue: String
init?(stringValue: String) {
self.stringValue = stringValue
}
var intValue: Int? { return nil }
init?(intValue: Int) { return nil }
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: ChainKey.self)
var chains = [Chain]()
for key in container.allKeys {
let identifier = key.stringValue
let elements = try container.decode([String].self, forKey: key)
let chain = Chain(identifier: identifier, providers: elements)
chains.append(chain)
}
self.chains = chains
}
}
struct Chain {
let identifier: String
let providers: [String]
}
let decoder = JSONDecoder()
let chainStore = try decoder.decode(ChainService.self, from: json)
for chain in chainStore.chains {
print(chain.identifier)
for element in chain.providers {
print("\t\(element)")
}
}