I suspect the answer is "no", but I wanted to see if anyone else had a path for this that could leverage Decodable. I'm working with some JSON data that's not in an object format, just arrays of values. For example [0,1, "A"]. In this case, I've been told index position 0, 1, and 2 have specific meanings, so I wanted to bring this into a struct format with properties identified after it's been "decoded/parsed" into Swift objects.
I didn't see any ways of mapping from index position within a JSON array - and treating that like a property, but is there?
I know it's a corner case in the JSON world, more about reading dense structured data - and right now I'm using swift-json-extra to convert this into "JSONValues" and then writing a little parser to vet the returned structure and convert it into the struct format I'd like to use.
You can do this by manually providing an implementation for your Decodable type.
import Foundation
struct PositionalStruct: Decodable {
let a: Int
let b: Int
let c: String
init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
if container.count != 3 {
throw DecodingError.typeMismatch(Self.self, .init(codingPath: decoder.codingPath, debugDescription: "Incorrect number of elements in array"))
}
a = try container.decode(Int.self)
b = try container.decode(Int.self)
c = try container.decode(String.self)
}
}
let data = """
[0, 1, "A"]
""".data(using: .utf8)!
let decoder = JSONDecoder()
let decoded = try decoder.decode(PositionalStruct.self, from: data)
print(decoded)
this won't be of much immediate help to you but i've also run into this problem a lot, and iโve been working on an API for swift-json that would make this use case a lot easier to express.
the PR is pretty much ready to merge, what i am actually blocked on is i don't really have the testing infrastructure to verify that the decoder emits the correct diagnostics, because any Error cannot be compared for equality, and it is very difficult to conform specific error types to Equatable because they often contain nested errors. this is not specific to swift-json, but if you have any advice for how to unit test error throwing in a scalable manner that would be a big help
With the details/reminder from @ksemianov about providing your own Decoable implementations, I was able to do exactly what I was after.
I'd already hand-cobbled a ruthless parser - but it's a hell of a lot easier to read with the decoder implementation structure. Interestingly, the decoders (even with Swift Extras or other optimized libraries like ZippyJSON) are notably slower than my brutalist parser.
I got a little overexcited, and applied some new experiments with package-benchmark to this exploration: