New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decodable Response Parsing #2180

Closed
jshier opened this Issue Jun 27, 2017 · 5 comments

Comments

Projects
4 participants
@jshier
Contributor

jshier commented Jun 27, 2017

Alamofire should support both generic Decodable response parsing and specific JSONDecoder and PropertyListDecoder based response handlers.

@jshier jshier added this to Idea in Alamofire 5 Jun 27, 2017

@jshier jshier referenced this issue Jun 27, 2017

Closed

Native Support for Swift 4's Codable type #2177

0 of 2 tasks complete

@jshier jshier added this to the 5.0.0 milestone Aug 7, 2017

@mRs-

This comment has been minimized.

mRs- commented Aug 21, 2017

Implementation for Decodable response request done with JSON is pretty easy.

My goto snippet for enabling this in NSURLSession

@discardableResult
func responseDecodable<T: Decodable>(request: Request, completion: @escaping (T?, URLResponse?, Error?) -> Void) -> URLSessionDataTask {
    return response(request: request, completion: { (data, response, error) in
        if let data = data {
            DispatchQueue.global(qos: .background).async {
                do {
                    let codable = try JSONDecoder().decode(T.self, from: data)
                    DispatchQueue.main.async {
                        completion(codable, response, error)
                    }
                } catch {
                    DispatchQueue.main.async {
                        completion(nil, response, error)
                    }
                }
            }
        } else if let error = error {
            completion(nil, response, error)
        } else {
            fatalError("Something went terrible wrong :(")
        }
    })
}

protocol NetworkServiceRequestable {
    @discardableResult
    func response(request: Request, completion: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
}
@jshier

This comment has been minimized.

Contributor

jshier commented Dec 21, 2017

This has been merged into Alamofire 5.

Alamofire.request(request).responseJSONDecodable { (response: DataResponse<HTTPBinResponse>) in
    print(response)
}

@jshier jshier closed this Dec 21, 2017

Alamofire 5 automation moved this from Review to Done Dec 21, 2017

@piyushnishanteu

This comment has been minimized.

piyushnishanteu commented May 28, 2018

Hi,
I have installed Alamofire using Pod it installed Alamofire (4.7.2). There is no responseJSONDecodable method in Alamofire version 4.7.2.
It would be better if you suggest me installation instruction alamofire5 using pod .

@sovanndyul

This comment has been minimized.

sovanndyul commented Oct 16, 2018

There is no responseJSONDecodable in Alamofire, 4.7.3. any update about responseJSONDecodable in Alamofire, 4.7.3 ?

@jshier

This comment has been minimized.

Contributor

jshier commented Oct 16, 2018

This feature won't be added to Alamofire 4, as not all users can support it on that version. That said, it's pretty trivial to add yourself, and you can take a look at how it's implemented in Alamofire 5 and almost directly copy it over.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment