Decodable icon indicating copy to clipboard operation
Decodable copied to clipboard

Unable to "ignore" an element. Instead it always throws

Open mdonati opened this issue 7 years ago • 1 comments

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?

mdonati avatar Jul 17 '17 12:07 mdonati

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 }
}

mdonati avatar Jul 17 '17 14:07 mdonati