Skip to content
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

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

Open
mdonati opened this issue Jul 17, 2017 · 1 comment
Open

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

mdonati opened this issue Jul 17, 2017 · 1 comment

Comments

@mdonati
Copy link

mdonati commented Jul 17, 2017

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
Copy link
Author

mdonati commented Jul 17, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant