Skip to content

Instantly share code, notes, and snippets.

@benaneesh
Last active August 22, 2016 01:58
Show Gist options
  • Save benaneesh/059c1d3342ec57115528e035698bf8a7 to your computer and use it in GitHub Desktop.
Save benaneesh/059c1d3342ec57115528e035698bf8a7 to your computer and use it in GitHub Desktop.
import Foundation
import Alamofire
import SwiftyJSON
// MARK: - Request for Swift JSON
extension Request {
public static func SwiftyJSONResponseSerializer(options options: NSJSONReadingOptions = .AllowFragments)
-> ResponseSerializer<JSON, NSError> {
return ResponseSerializer { _, _, data, error in
guard error == nil else { return .Failure(error!) }
guard let validData = data where validData.length > 0 else {
let failureReason = "JSON could not be serialized. Input data was nil or zero length."
let error = Error.errorWithCode(.JSONSerializationFailed, failureReason: failureReason)
return .Failure(error)
}
let json:JSON = SwiftyJSON.JSON(data: validData)
if let jsonError = json.error {
return Result.Failure(jsonError)
}
return Result.Success(json)
}
}
public func responseSwiftyJSON( options options: NSJSONReadingOptions = .AllowFragments,
completionHandler: Response<JSON, NSError> -> Void) -> Self {
return response(
responseSerializer: Request.SwiftyJSONResponseSerializer(options: options),
completionHandler: completionHandler
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment