{"id":1725,"url":"https://github.com/tristanhimmelman/AlamofireObjectMapper","last_synced_at":"2025-08-06T13:32:06.287Z","repository":{"id":31294856,"uuid":"34856963","full_name":"tristanhimmelman/AlamofireObjectMapper","owner":"tristanhimmelman","description":"An Alamofire extension which converts JSON response data into swift objects using ObjectMapper","archived":false,"fork":false,"pushed_at":"2023-11-10T09:04:28.000Z","size":254,"stargazers_count":2664,"open_issues_count":62,"forks_count":473,"subscribers_count":50,"default_branch":"master","last_synced_at":"2024-10-29T15:34:06.557Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/tristanhimmelman.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-04-30T13:52:26.000Z","updated_at":"2024-10-22T12:57:52.000Z","dependencies_parsed_at":"2023-02-13T04:45:49.085Z","dependency_job_id":"c9588705-8902-40ef-b9db-64ed8adac9a3","html_url":"https://github.com/tristanhimmelman/AlamofireObjectMapper","commit_stats":{"total_commits":238,"total_committers":41,"mean_commits":5.804878048780488,"dds":"0.33613445378151263","last_synced_commit":"ed1958fb83d680cfa22bfff4b955fe19ce21f41f"},"previous_names":[],"tags_count":32,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tristanhimmelman%2FAlamofireObjectMapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tristanhimmelman%2FAlamofireObjectMapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tristanhimmelman%2FAlamofireObjectMapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tristanhimmelman%2FAlamofireObjectMapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tristanhimmelman","download_url":"https://codeload.github.com/tristanhimmelman/AlamofireObjectMapper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222081927,"owners_count":16928116,"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":[],"created_at":"2024-01-05T20:15:54.329Z","updated_at":"2024-12-09T14:30:44.515Z","avatar_url":"https://github.com/tristanhimmelman.png","language":"Swift","funding_links":[],"categories":["Parsing","Libs","Swift","Data and Storage","Data Management [🔝](#readme)","JSON"],"sub_categories":["JSON","Data Management","Other free courses"],"readme":"AlamofireObjectMapper\n============\n[![Build Status](https://travis-ci.org/tristanhimmelman/AlamofireObjectMapper.svg?branch=master)](https://travis-ci.org/tristanhimmelman/AlamofireObjectMapper)\n[![CocoaPods](https://img.shields.io/cocoapods/v/AlamofireObjectMapper.svg)](https://github.com/tristanhimmelman/AlamofireObjectMapper)\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n\n\nAn extension to [Alamofire](https://github.com/Alamofire/Alamofire) which automatically converts JSON response data into swift objects using [ObjectMapper](https://github.com/Hearst-DD/ObjectMapper/). \n\n# Usage\n\nGiven a URL which returns weather data in the following form:\n```\n{\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 use the extension as the follows:\n```swift\nimport AlamofireObjectMapper\n\nlet URL = \"https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/d8bb95982be8a11a2308e779bb9a9707ebe42ede/sample_json\"\nAlamofire.request(URL).responseObject { (response: DataResponse\u003cWeatherResponse\u003e) in\n\n    let weatherResponse = response.result.value\n    print(weatherResponse?.location)\n    \n    if let threeDayForecast = weatherResponse?.threeDayForecast {\n        for forecast in threeDayForecast {\n            print(forecast.day)\n            print(forecast.temperature)           \n        }\n    }\n}\n```\n\nThe `WeatherResponse` object in the completion handler is a custom object which you define. The only requirement is that the object must conform to [ObjectMapper's](https://github.com/Hearst-DD/ObjectMapper/) `Mappable` protocol. In the above example, the `WeatherResponse` object looks like the following:\n\n```swift\nimport ObjectMapper\n\nclass WeatherResponse: Mappable {\n    var location: String?\n    var threeDayForecast: [Forecast]?\n    \n\trequired init?(map: Map){\n\n\t}\n    \n    func mapping(map: Map) {\n        location \u003c- map[\"location\"]\n        threeDayForecast \u003c- map[\"three_day_forecast\"]\n    }\n}\n\nclass Forecast: Mappable {\n    var day: String?\n    var temperature: Int?\n    var conditions: String?\n    \n\trequired init?(map: Map){\n\n\t}\n    \n    func mapping(map: Map) {\n        day \u003c- map[\"day\"]\n        temperature \u003c- map[\"temperature\"]\n        conditions \u003c- map[\"conditions\"]\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```swift\npublic func responseObject\u003cT: BaseMappable\u003e(queue: DispatchQueue? = nil, keyPath: String? = nil, mapToObject object: T? = nil, context: MapContext? = nil, completionHandler: @escaping (DataResponse\u003cT\u003e) -\u003e Void) -\u003e Self\n```\nThe `responseObject` function has 4 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- `mapToObject`: An object to perform the mapping on to.\n- `context`: A [context object](https://github.com/Hearst-DD/ObjectMapper/#mapping-context) that is passed to the mapping function.\n- `completionHandler`: A closure to be executed once the request has finished and the data has been mapped by ObjectMapper.\n\n### Easy Mapping of Nested Objects\n\nAlamofireObjectMapper supports dot notation within keys for easy mapping of nested objects. Given the following JSON String:\n```json\n\"distance\" : {\n     \"text\" : \"102 ft\",\n     \"value\" : 31\n}\n```\nYou can access the nested objects as follows:\n```swift\nfunc mapping(map: Map) {\n    distance \u003c- map[\"distance.value\"]\n}\n```\n[See complete documentation](https://github.com/Hearst-DD/ObjectMapper#easy-mapping-of-nested-objects)\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.weather` to drill down several levels in a JSON response.\n```swift\nlet URL = \"https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/2ee8f34d21e8febfdefb2b3a403f18a43818d70a/sample_keypath_json\"\nlet expectation = expectationWithDescription(\"\\(URL)\")\n\nAlamofire.request(URL).responseObject(keyPath: \"data\") { (response: DataResponse\u003cWeatherResponse\u003e) in\n    expectation.fulfill()\n    \n    let weatherResponse = response.result.value\n    print(weatherResponse?.location)\n    \n    if let threeDayForecast = weatherResponse?.threeDayForecast {\n        for forecast in threeDayForecast {\n            print(forecast.day)\n            print(forecast.temperature)           \n        }\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```swift\npublic func responseArray\u003cT: Mappable\u003e(queue queue: dispatch_queue_t? = nil, keyPath: String? = nil, completionHandler: DataResponse\u003c[T]\u003e -\u003e Void) -\u003e Self\n```\n\nFor example, if your endpoint returns the following:\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```\nYou can request and map it as follows:\n```swift\nlet URL = \"https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/f583be1121dbc5e9b0381b3017718a70c31054f7/sample_array_json\"\nAlamofire.request(URL).responseArray { (response: DataResponse\u003c[Forecast]\u003e) in\n\n    let forecastArray = response.result.value\n    \n    if let forecastArray = forecastArray {\n        for forecast in forecastArray {\n            print(forecast.day)\n            print(forecast.temperature)           \n        }\n    }\n}\n\n```\n\n# Installation\nAlamofireObjectMapper can be added to your project using [CocoaPods](https://cocoapods.org/) by adding the following line to your Podfile:\n```\npod 'AlamofireObjectMapper', '~\u003e 5.2'\n```\n\nIf you're using [Carthage](https://github.com/Carthage/Carthage) you can add a dependency on AlamofireObjectMapper by adding it to your Cartfile:\n```\ngithub \"tristanhimmelman/AlamofireObjectMapper\" ~\u003e 5.2\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftristanhimmelman%2FAlamofireObjectMapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftristanhimmelman%2FAlamofireObjectMapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftristanhimmelman%2FAlamofireObjectMapper/lists"}