Decodable
Decodable copied to clipboard
Unable to "ignore" an element. Instead it always throws
I have an array of dictionaries, each with a "Type" field that is used to determine which struct should be used for decoding it. Currently I have some unsupported types. I would like to just ignore those and keep parsing the rest of the array instead of throwing an error and cancelling the whole parsing operation at one point. I also don't want to end up with a dummy object just for the sole purpose of returning a non-nil object in the decode function.
Is there anyway to do that with the latest version?
I was able to implement a custom operator for this. Its implementation looks like this:
public func ~> <A: Decodable>(json: Any, keyPath: KeyPath) throws -> [A] {
return try [A?].decoder { try? A.decode($0) }(try parse(json, keyPath)).flatMap { $0 }
}