{"id":15038840,"url":"https://github.com/cnkcq/alamofirecodable","last_synced_at":"2025-04-10T00:02:40.815Z","repository":{"id":56901700,"uuid":"110684337","full_name":"CNKCQ/AlamofireCodable","owner":"CNKCQ","description":"An Alamofire extension which converts JSON response data into swift objects using Codable","archived":false,"fork":false,"pushed_at":"2019-05-23T13:07:36.000Z","size":4451,"stargazers_count":49,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-10T00:01:48.545Z","etag":null,"topics":["codable","jsonparse","swift-library","swift4"],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CNKCQ.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-14T11:56:41.000Z","updated_at":"2023-11-01T02:34:49.000Z","dependencies_parsed_at":"2022-08-21T02:50:52.633Z","dependency_job_id":null,"html_url":"https://github.com/CNKCQ/AlamofireCodable","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CNKCQ%2FAlamofireCodable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CNKCQ%2FAlamofireCodable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CNKCQ%2FAlamofireCodable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CNKCQ%2FAlamofireCodable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CNKCQ","download_url":"https://codeload.github.com/CNKCQ/AlamofireCodable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248131322,"owners_count":21052819,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["codable","jsonparse","swift-library","swift4"],"created_at":"2024-09-24T20:40:29.088Z","updated_at":"2025-04-10T00:02:40.780Z","avatar_url":"https://github.com/CNKCQ.png","language":"Swift","readme":"[![CI Status](https://img.shields.io/travis/CNKCQ/DigitalKeyboard.svg?style=flat)](https://travis-ci.org/CNKCQ/AlamofireCodable)\n[![Version](https://img.shields.io/cocoapods/v/AlamofireCodable.svg?style=flat)](http://cocoadocs.org/docsets/AlamofireCodable)\n[![Platform](https://img.shields.io/cocoapods/p/AlamofireCodable.svg?style=flat)](http://cocoadocs.org/docsets/AlamofireCodable)\n![](https://img.shields.io/github/stars/CNKCQ/AlamofireCodable.svg?style=social\u0026label=Star)\n\n[AlamofireCodable](https://github.com/CNKCQ/AlamofireCodable): An extension to [Alamofire](https://github.com/Alamofire/Alamofire) which automatically converts JSON response data into swift objects using Codable. \nThis project is heavily inspired by the popular [AlamofireObjectMapper](https://github.com/tristanhimmelman/AlamofireObjectMapper). \n\n![image.png](http://upload-images.jianshu.io/upload_images/121208-bcd3bb1a88fbe23c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/500)\n\n## Installation\nAlamofireCodable can be added to your project using [CocoaPods](https://cocoapods.org/) by adding the following line to your Podfile:\n```\npod 'AlamofireCodable'\n```\n\n\n## Example\n\nTo run the example project, clone the repo, and run `pod install` from the Example directory first.\n\n## Requirements\nXcode 9+ , Swift 4+\n\n\n## Usage\n\nGiven a URL which returns weather data in the following form:\n\n```\n{  \n   \"data\":{  \n      \"location\":\"Toronto, Canada\",\n      \"three_day_forecast\":[  \n         {  \n            \"conditions\":\"Partly cloudy\",\n            \"day\":\"Monday\",\n            \"temperature\":20\n         },\n         {  \n            \"conditions\":\"Showers\",\n            \"day\":\"Tuesday\",\n            \"temperature\":22\n         },\n         {  \n            \"conditions\":\"Sunny\",\n            \"day\":\"Wednesday\",\n            \"temperature\":28\n         }\n      ]\n   }\n}\n```\n\nYou can use the extension as the follows:\n\n```swift\nimport AlamofireCodable\n\n        let form = WeatherForm()\n        Alamofire.request(\n                form.url,\n                method: HTTPMethod.get,\n                parameters: form.parameters(),\n                encoding: form.encoding(),\n                headers: form.headers()\n            )\n            .responseObject(keyPath: \"data\",completionHandler: { (response: DataResponse\u003cWeather\u003e) in\n                switch response.result {\n                case .success(let object):\n                    debugPrint(\"🌹\", object.location)\n                case .failure(let error):\n                    debugPrint(\"🌹\", error.localizedDescription)\n                }\n            })\n\n```\n\nThe `Weather` object in the completion handler is a custom object which you define. The only requirement is that the object must conform to `Codable` protocol. In the above example, the `Weather` object looks like the following:\n\n\n```swift\n{  \n   \"data\":{  \n      \"location\":\"Toronto, Canada\",\n      \"three_day_forecast\":[  \n         {  \n            \"conditions\":\"Partly cloudy\",\n            \"day\":\"Monday\",\n            \"temperature\":20\n         },\n         {  \n            \"conditions\":\"Showers\",\n            \"day\":\"Tuesday\",\n            \"temperature\":22\n         },\n         {  \n            \"conditions\":\"Sunny\",\n            \"day\":\"Wednesday\",\n            \"temperature\":28\n         }\n      ]\n   }\n}\n```\n\nThe extension uses Generics to allow you to create your own custom response objects. Below is the `responseObject` function definition. Just replace `T` in the completionHandler with your custom response object and the extension handles the rest: \n\n```swift\npublic func responseObject\u003cT: Codable\u003e(queue: DispatchQueue? = nil, keyPath: String? = nil,  completionHandler: @escaping (DataResponse\u003cT\u003e) -\u003e Void) -\u003e Self \n```\n\nThe `responseObject` function has 2 optional parameters and a required completionHandler:\n- `queue`: The queue on which the completion handler is dispatched.\n- `keyPath`: The key path of the JSON where object mapping should be performed\n- `completionHandler`: A closure to be executed once the request has finished and the data has been decoded by JSONDecoder.\n\n### Easy decode of Nested Objects\n\nAlamofireCodable supports dot notation within keys for easy mapping of nested objects. Given the following JSON String:\n\n```json\n{  \n   \"data\":{  \n      \"location\":\"Toronto, Canada\",\n      \"three_day_forecast\":[  \n         {  \n            \"conditions\":\"Partly cloudy\",\n            \"day\":\"Monday\",\n            \"temperature\":20\n         },\n         {  \n            \"conditions\":\"Showers\",\n            \"day\":\"Tuesday\",\n            \"temperature\":22\n         },\n         {  \n            \"conditions\":\"Sunny\",\n            \"day\":\"Wednesday\",\n            \"temperature\":28\n         }\n      ]\n   }\n}\n```\nYou can access the nested objects as follows:\n\n```swift\n      let form = WeatherForm()\n        Alamofire.request(\n            form.url,\n            method: HTTPMethod.get,\n            parameters: form.parameters(),\n            encoding: form.encoding(),\n            headers: form.headers()\n            )\n            .responseObject(completionHandler: { (response: DataResponse\u003cRootModel\u003e) in\n                switch response.result {\n                case .success(let root):\n                    debugPrint(\"🌹\", root)\n                case .failure(let error):\n                    debugPrint(\"🌹\", error.localizedDescription)\n                }\n            })\n```\n\n\n### KeyPath\n\nThe `keyPath` variable is used to drill down into a JSON response and only map the data found at that `keyPath`. It supports nested values such as `data.three_day_forecast` to drill down several levels in a JSON response.\n\n```swift\nlet form = WeatherForm()\n        Alamofire.request(\n                form.url,\n                method: HTTPMethod.get,\n                parameters: form.parameters(),\n                encoding: form.encoding(),\n                headers: form.headers()\n            )\n            .responseArray(keyPath: \"data.three_day_forecast\", completionHandler: { (response: DataResponse\u003c[Forecast]\u003e) in\n                switch response.result {\n                case .success(let array):\n                    debugPrint(\"🌹\", array)\n                case .failure(let error):\n                    debugPrint(\"🌹\", error.localizedDescription)\n                }\n            })\n```\n\n## Array Responses\nIf you have an endpoint that returns data in `Array` form you can map it with the following function:\n\n```swift\npublic func responseArray\u003cT: Codable\u003e(queue: DispatchQueue? = nil, keyPath: String? = nil, completionHandler: @escaping (DataResponse\u003c[T]\u003e) -\u003e Void) -\u003e Self \n```\n\nFor example, if your endpoint returns the following:\n\n```\n[\n    { \n        \"conditions\": \"Partly cloudy\",\n        \"day\" : \"Monday\",\n        \"temperature\": 20 \n    },\n    { \n        \"conditions\": \"Showers\",\n        \"day\" : \"Tuesday\",\n        \"temperature\": 22 \n    },\n    { \n        \"conditions\": \"Sunny\",\n        \"day\" : \"Wednesday\",\n        \"temperature\": 28 \n    }\n]\n```\n\nYou can request and map it as follows:\n\n```swift\n        let form = WeatherForm()\n        Alamofire.request(\n                form.url,\n                method: HTTPMethod.get,\n                parameters: form.parameters(),\n                encoding: form.encoding(),\n                headers: form.headers()\n            )\n            .responseArray(keyPath: \"data.three_day_forecast\", completionHandler: { (response: DataResponse\u003c[Forecast]\u003e) in\n                switch response.result {\n                case .success(let array):\n                    debugPrint(\"🌹\", array)\n                case .failure(let error):\n                    debugPrint(\"🌹\", error.localizedDescription)\n                }\n            })\n```\n\n## Author\n\nwangchengqvan@gmail.com, chengquan.wang@ele.me\n\n## License\n\nAlamofireCodable is available under the MIT license. See the LICENSE file for more info.\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcnkcq%2Falamofirecodable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcnkcq%2Falamofirecodable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcnkcq%2Falamofirecodable/lists"}