Skip to content

Instantly share code, notes, and snippets.

@elenipapanik
Last active May 29, 2019 13:56
Show Gist options
  • Save elenipapanik/86573e4d05c7d05172db051a30a16cc6 to your computer and use it in GitHub Desktop.
Save elenipapanik/86573e4d05c7d05172db051a30a16cc6 to your computer and use it in GitHub Desktop.
// For models that are ALREADY Swift.Decodable and want to be used inside Argo decodable models
protocol SwiftToArgoDecodable: Argo.Decodable, Swift.Decodable where Self.DecodedType == Self {}
extension SwiftToArgoDecodable {
static func decode(_ json: JSON) -> Decoded<DecodedType> {
do {
let jsonData = try JSONSerialization.data(withJSONObject: json.JSONObject(), options: [])
let decodedValue = try JSONDecoder().decode(Self.self, from: jsonData)
return .success(decodedValue)
} catch let error {
return .failure(.custom((error as NSError).description))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment