Skip to content
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 · 12 comments
Closed

Decodable Response Parsing #2180

jshier opened this issue Jun 27, 2017 · 12 comments

Comments

@jshier
Copy link
Contributor

@jshier jshier commented Jun 27, 2017

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

@mRs-
Copy link

@mRs- 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
Copy link
Contributor Author

@jshier 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
Copy link

@piyushnishanteu 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
Copy link

@sovanndyul sovanndyul commented Oct 16, 2018

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

@jshier
Copy link
Contributor Author

@jshier 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.

@krjw
Copy link

@krjw krjw commented Oct 24, 2019

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)
            }
        }

@ecarnevale
Copy link

@ecarnevale ecarnevale commented Jan 29, 2020

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?

@jshier
Copy link
Contributor Author

@jshier jshier commented Jan 29, 2020

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

@TomasHubelbauer
Copy link

@TomasHubelbauer TomasHubelbauer commented Mar 14, 2020

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.

@jshier
Copy link
Contributor Author

@jshier jshier commented Mar 14, 2020

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

AF.request(...).responseDecodable(of: SomeDecodableType.self) { response in
    //...
}
@EsmeraldiBejolli
Copy link

@EsmeraldiBejolli EsmeraldiBejolli commented Mar 31, 2020

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

@jshier
Copy link
Contributor Author

@jshier jshier commented Mar 31, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Alamofire 5
  
Done
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
8 participants