Skip to content

Commit 4a3a160

Browse files
committed
Added support for Alamofire 4 & SwiftyJSON 3.
1 parent 8cb1e13 commit 4a3a160

2 files changed

Lines changed: 70 additions & 43 deletions

File tree

Alamofire-SwiftyJSON.podspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ Pod::Spec.new do |s|
88

99
s.requires_arc = true
1010
s.osx.deployment_target = "10.9"
11-
s.ios.deployment_target = "8.0"
11+
s.ios.deployment_target = "9.0"
1212
s.source = { :git => "https://github.com/SwiftyJSON/Alamofire-SwiftyJSON.git", :tag => s.version }
1313
s.source_files = "Source/*.swift"
14-
s.dependency 'Alamofire', '1.3'
15-
s.dependency 'SwiftyJSON', '2.2.0'
14+
s.dependency 'Alamofire', '~> 4.0'
15+
s.dependency 'SwiftyJSON', '~> 3.0'
1616
end

Source/Alamofire-SwiftyJSON.swift

Lines changed: 67 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,47 +14,74 @@ import SwiftyJSON
1414
// MARK: - Request for Swift JSON
1515

1616
extension Request {
17-
18-
/**
19-
Adds a handler to be called once the request has finished.
20-
21-
:param: completionHandler A closure to be executed once the request has finished. The closure takes 4 arguments: the URL request, the URL response, if one was received, the SwiftyJSON enum, if one could be created from the URL response and data, and any error produced while creating the SwiftyJSON enum.
22-
23-
:returns: The request.
24-
*/
25-
public func responseSwiftyJSON(completionHandler: (NSURLRequest, NSHTTPURLResponse?, SwiftyJSON.JSON, ErrorType?) -> Void) -> Self {
26-
return responseSwiftyJSON(nil, options:NSJSONReadingOptions.AllowFragments, completionHandler:completionHandler)
27-
}
28-
29-
/**
30-
Adds a handler to be called once the request has finished.
31-
32-
:param: queue The queue on which the completion handler is dispatched.
33-
:param: options The JSON serialization reading options. `.AllowFragments` by default.
34-
:param: completionHandler A closure to be executed once the request has finished. The closure takes 4 arguments: the URL request, the URL response, if one was received, the SwiftyJSON enum, if one could be created from the URL response and data, and any error produced while creating the SwiftyJSON enum.
35-
36-
:returns: The request.
37-
*/
38-
public func responseSwiftyJSON(queue: dispatch_queue_t? = nil, options: NSJSONReadingOptions = .AllowFragments, completionHandler: (NSURLRequest, NSHTTPURLResponse?, JSON, ErrorType?) -> Void) -> Self {
39-
40-
// With Alamofire 3, completionHandler returns a Response struct instead of request, response, result.
41-
//For more information about Response struct see: https://github.com/Alamofire/Alamofire/blob/master/Documentation/Alamofire%203.0%20Migration%20Guide.md
42-
return response(queue: queue, responseSerializer: Request.JSONResponseSerializer(options: options), completionHandler: { (response) -> Void in
43-
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
44-
var responseJSON: JSON
45-
if response.result.isFailure
46-
{
47-
responseJSON = JSON.null
48-
} else {
49-
responseJSON = SwiftyJSON.JSON(response.result.value!)
50-
}
51-
dispatch_async(queue ?? dispatch_get_main_queue(), {
52-
completionHandler(response.request!, response.response, responseJSON, response.result.error)
53-
})
54-
})
55-
})
56-
17+
/// Returns a SwiftyJSON object contained in a result type constructed from the response data using `JSONSerialization`
18+
/// with the specified reading options.
19+
///
20+
/// - parameter options: The JSON serialization reading options. Defaults to `.allowFragments`.
21+
/// - parameter response: The response from the server.
22+
/// - parameter data: The data returned from the server.
23+
/// - parameter error: The error already encountered if it exists.
24+
///
25+
/// - returns: The result data type.
26+
public static func serializeResponseSwiftyJSON(
27+
options: JSONSerialization.ReadingOptions,
28+
response: HTTPURLResponse?,
29+
data: Data?,
30+
error: Error?)
31+
-> Result<JSON>
32+
{
33+
guard error == nil else { return .failure(error!) }
34+
35+
if let response = response, emptyDataStatusCodes.contains(response.statusCode) { return .success(JSON.null) }
36+
37+
guard let validData = data, validData.count > 0 else {
38+
return .failure(AFError.responseSerializationFailed(reason: .inputDataNilOrZeroLength))
39+
}
40+
41+
do {
42+
let json = try JSONSerialization.jsonObject(with: validData, options: options)
43+
return .success(JSON(json))
44+
} catch {
45+
return .failure(AFError.responseSerializationFailed(reason: .jsonSerializationFailed(error: error)))
46+
}
5747
}
5848
}
5949

50+
extension DataRequest {
51+
/// Creates a response serializer that returns a SwiftyJSON object result type constructed from the response data using
52+
/// `JSONSerialization` with the specified reading options.
53+
///
54+
/// - parameter options: The JSON serialization reading options. Defaults to `.allowFragments`.
55+
///
56+
/// - returns: A JSON object response serializer.
57+
public static func swiftyJSONResponseSerializer(
58+
options: JSONSerialization.ReadingOptions = .allowFragments)
59+
-> DataResponseSerializer<JSON>
60+
{
61+
return DataResponseSerializer { _, response, data, error in
62+
return Request.serializeResponseSwiftyJSON(options: options, response: response, data: data, error: error)
63+
}
64+
}
65+
66+
/// Adds a handler to be called once the request has finished.
67+
///
68+
/// - parameter options: The JSON serialization reading options. Defaults to `.allowFragments`.
69+
/// - parameter completionHandler: A closure to be executed once the request has finished.
70+
///
71+
/// - returns: The request.
72+
@discardableResult
73+
public func responseSwiftyJSON(
74+
queue: DispatchQueue? = nil,
75+
options: JSONSerialization.ReadingOptions = .allowFragments,
76+
completionHandler: @escaping (DataResponse<JSON>) -> Void)
77+
-> Self
78+
{
79+
return response(
80+
queue: queue,
81+
responseSerializer: DataRequest.swiftyJSONResponseSerializer(options: options),
82+
completionHandler: completionHandler
83+
)
84+
}
85+
}
6086

87+
private let emptyDataStatusCodes: Set<Int> = [204, 205]

0 commit comments

Comments
 (0)