Skip to content

Instantly share code, notes, and snippets.

@xr1337
Last active December 9, 2022 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xr1337/7bccf3dac76e56f2fa2555a992f96685 to your computer and use it in GitHub Desktop.
Save xr1337/7bccf3dac76e56f2fa2555a992f96685 to your computer and use it in GitHub Desktop.
A json wrapper that holds the decode status of json objects. Generally used to decode JSON container items that may fail such as JSON Arrays.
/// This is wrapper that holds the decode status of json objects
/// It is useful for JSON arrays where if one child JSON node fails to decode, the whole array is not nil
/// Example use: try JSONDecoder().decode([DecodableResult<MyProduct>]).compactMap { try? $0.result.get() }
struct DecodableResult<T: Decodable>: Decodable {
let result: Result<T, Error>
init(from decoder: Decoder) throws {
result = Result(catching: { try T(from: decoder) })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment