New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Decoding array of arrays #174
Comments
I ended up writing this code. infix operator <||<|| : MultiplicationPrecedence
/// - Throws: DecodeError or an arbitrary ErrorType
public func <||<|| <T: Decodable>(e: Extractor, keyPath: KeyPath) throws -> [[T]] {
let json: [String: Any] = try castOrFail(e.rawValue)
let array: [Any] = try castOrFail(json[keyPath.components.joined(separator: ".")])
return try array.map { c -> [T] in
return try castOrFail(c)
}
} I would add this to your App, rather than to Himotoki. I think it's better to keep the core code small. I also needed to add Optional version of public func castOrFail<T>(_ any: Any?) throws -> T {
guard let result = any as? T else {
throw typeMismatch("\(T.self)", actual: any)
}
return result
} So now you can do this. extension Summary: Decodable {
static func decode(_ e: Extractor) throws -> Summary {
return try Summary(items: e <||<|| "items")
}
} |
Sorry for the late response, and thanks to @toshi0383 for sharing your way! Seems that this is a duplicate of #149 and I'd rather write something like this: #149 (comment). |
Wow, that looks neat😮 |
Thank you both guys, I'll have a look at both solutions |
Hi,
I have an issue decoding array of arrays,
how would you write a decode function which decode a JSON like this:
I've tried writing a struct like this:
and an extension:
but I'm getting an error:
Type '[Int]' does not conform to protocol 'Decodable'
I know the JSON could be built better but this is what I'm getting from the server and this is what I have to decode.
Thanks,
Oron
The text was updated successfully, but these errors were encountered: