Skip to content

Instantly share code, notes, and snippets.

@mtnbarreto
Created April 12, 2016 17:12
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 mtnbarreto/10a1484c648503acb6245764b2b6f0ff to your computer and use it in GitHub Desktop.
Save mtnbarreto/10a1484c648503acb6245764b2b6f0ff to your computer and use it in GitHub Desktop.
Adapter to make Argo JSON parsing library works with Opera
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