Created
March 12, 2020 12:29
-
-
Save artemnovichkov/b31e369fd29ba43643f3cddc700c3c56 to your computer and use it in GitHub Desktop.
JSONDecoder + Result
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
public extension JSONDecoder { | |
func decode<T>(_ type: T.Type, from data: Data) -> Result<T, Error> where T: Decodable { | |
do { | |
let decodedData: T = try decode(T.self, from: data) | |
return .success(decodedData) | |
} | |
catch { | |
return .failure(error) | |
} | |
} | |
func decode<T>(_ result: Result<Data, Error>) -> Result<T, Error> where T: Decodable { | |
do { | |
let decodedData: T = try decode(T.self, from: try result.get()) | |
return .success(decodedData) | |
} | |
catch { | |
return .failure(error) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment