Alamofire: Decodable Response Parsing

Created on 27 Jun 2017  路  12Comments  路  Source: Alamofire/Alamofire

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

feature request

Most helpful comment

This has been merged into Alamofire 5.

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

All 12 comments

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
}

This has been merged into Alamofire 5.

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

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 .

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

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.

I couldn't find responseJSONDecodablein Alamofire 5 but this works for me:

        AF.request(Blub.url).validate().responseJSON { response in
            guard response.error == nil else {
                print(response.error!)
                return
            }
            guard let data = response.data else {
                print("No Data")
                return
            }
            do {
                let decoder = JSONDecoder()
                let info = try decoder.decode(Blub.self, from: data)
                print(info)
            } catch {
                print(error)
            }
        }

it seems it disappeared from Alamofire 5 (at least rc-3 doesn't have it): commits history for Source/ResponseSerialization.swift jump form Sep 2017 to Feb 2018

https://github.com/Alamofire/Alamofire/commits/5.0.0-rc.3?before=2cbf59935fbb1f26e352ce4db53f1cf9408d5313+35&path%5B%5D=Source&path%5B%5D=ResponseSerialization.swift

any idea if it will be reintroduced?

It was renamed responseDecodable a long time ago. Read our Usage documentation for more.

Working example for future Google-faring visitors:

AF.request("https://api.com/items").responseDecodable { (response: DataResponse<[Item], AFError>) in
    self.items = response.value!
}

In case of using an array, the item type must conform to Decodable.

I would recommend reading our documentation. But the recommended pattern is:

AF.request(...).responseDecodable(of: SomeDecodableType.self) { response in
    //...
}

I would recommend reading our documentation. But the recommended pattern is:

AF.request(...).responseDecodable(of: SomeDecodableType.self) { response in
    //...
}

I need to pass SomeDecodableType as reference to my method and then pass here but it throws an error.
Generic parameter 'T' could not be inferred

You can pass it through the same way responseDecodable does: T.Type.

func wrapper<T>(_ type: T.Type) {
    AF.request(...).responseDecodable(of: T.self) { ... }
}

It's a bit odd that you aren't using the parameter, but it should work this way.

Was this page helpful?
0 / 5 - 0 ratings