Created
April 12, 2016 17:12
-
-
Save mtnbarreto/10a1484c648503acb6245764b2b6f0ff to your computer and use it in GitHub Desktop.
Adapter to make Argo JSON parsing library works with Opera
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
extension Argo.Decodable where Self.DecodedType == Self, Self: OperaDecodable { | |
static func decode(json: AnyObject) throws -> Self { | |
let decoded = decode(JSON.parse(json)) | |
switch decoded { | |
case .Success(let value): | |
return value | |
case .Failure(let error): | |
throw error | |
} | |
} | |
} | |
extension NSDate: Decodable { | |
public typealias DecodedType = NSDate | |
public class func decode(j: JSON) -> Decoded<NSDate> { | |
switch j { | |
case .String(let dateString): | |
return dateString.toDate(DateFormat.ISO8601Format(.Full)).map(pure) ?? .typeMismatch("NSDate", actual: j) | |
default: return .typeMismatch("NSDate", actual: j) | |
} | |
} | |
} | |
extension NSURL: Decodable { | |
public typealias DecodedType = NSURL | |
public class func decode(j: JSON) -> Decoded<NSURL> { | |
switch j { | |
case .String(let urlString): | |
return NSURL(string: String(urlString)).map(pure) ?? .typeMismatch("URL", actual: j) | |
default: return .typeMismatch("URL", actual: j) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment