{"id":16906858,"url":"https://github.com/sunshinejr/moya-modelmapper","last_synced_at":"2025-07-16T05:07:41.212Z","repository":{"id":45351146,"uuid":"50993030","full_name":"sunshinejr/Moya-ModelMapper","owner":"sunshinejr","description":"ModelMapper bindings for Moya.","archived":false,"fork":false,"pushed_at":"2021-12-18T20:46:49.000Z","size":743,"stargazers_count":151,"open_issues_count":9,"forks_count":31,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-21T11:45:48.915Z","etag":null,"topics":["mapper","modelmapper","moya","reactivecocoa","reactiveswift","rxswift"],"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/sunshinejr.png","metadata":{"files":{"readme":"README.md","changelog":"Changelog.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["sunshinejr"]}},"created_at":"2016-02-03T10:41:38.000Z","updated_at":"2025-06-06T08:15:18.000Z","dependencies_parsed_at":"2022-08-30T03:40:24.151Z","dependency_job_id":null,"html_url":"https://github.com/sunshinejr/Moya-ModelMapper","commit_stats":null,"previous_names":[],"tags_count":33,"template":false,"template_full_name":null,"purl":"pkg:github/sunshinejr/Moya-ModelMapper","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunshinejr%2FMoya-ModelMapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunshinejr%2FMoya-ModelMapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunshinejr%2FMoya-ModelMapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunshinejr%2FMoya-ModelMapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sunshinejr","download_url":"https://codeload.github.com/sunshinejr/Moya-ModelMapper/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunshinejr%2FMoya-ModelMapper/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265483399,"owners_count":23774201,"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":["mapper","modelmapper","moya","reactivecocoa","reactiveswift","rxswift"],"created_at":"2024-10-13T18:44:57.487Z","updated_at":"2025-07-16T05:07:41.178Z","avatar_url":"https://github.com/sunshinejr.png","language":"Swift","funding_links":["https://github.com/sponsors/sunshinejr"],"categories":[],"sub_categories":[],"readme":"# Moya-ModelMapper\n\n[![CocoaPods](https://img.shields.io/cocoapods/v/Moya-ModelMapper.svg)](https://github.com/sunshinejr/Moya-ModelMapper)\n\n[ModelMapper](https://github.com/lyft/mapper) bindings for\n[Moya](https://github.com/Moya/Moya) for easier JSON serialization with [RxSwift](https://github.com/ReactiveX/RxSwift) and [ReactiveCocoa](https://github.com/ReactiveCocoa/ReactiveCocoa) bindings.\n\n# Installation\n\n## CocoaPods\n\n```\npod 'Moya-ModelMapper', '~\u003e 10.0'\n```\n\nThe subspec if you want to use the bindings over RxSwift.\n```\npod 'Moya-ModelMapper/RxSwift', '~\u003e 10.0'\n```\n\nAnd the subspec if you want to use the bindings over ReactiveSwift.\n```\npod 'Moya-ModelMapper/ReactiveSwift', '~\u003e 10.0'\n```\n\n## Carthage\n\nSpecify in Cartfile:\n\n```\ngithub \"sunshinejr/Moya-ModelMapper\" ~\u003e 10.0\n```\n\nCarthage users can point to this repository and use whichever generated framework they'd like, Moya-ModelMapper, RxMoya-ModelMapper, or ReactiveMoya-ModelMapper.\n\n## Swift Package Manager\n\nAdd the following as a dependency to your `Package.swift`.\n\n```swift\n.package(url: \"https://github.com/sunshinejr/Moya-ModelMapper.git\", .upToNextMajor(from: \"10.0.0\"))\n```\n\nThe bindings are available through `Moya_ModelMapper` module. If you are interested in reactive extensions, use `ReactiveMoya_ModelMapper` or `RxMoya_ModelMapper` respectively.\n\n# Usage\n\nCreate a model struct or class. It needs to implement protocol Mappable.\n\n```swift\nimport Foundation\nimport Mapper\n\nstruct Repository: Mappable {\n\n    let identifier: Int\n    let language: String? // Optional property\n    let url: String? // Optional property\n\n    init(map: Mapper) throws {\n        try identifier = map.from(\"id\")\n        language = map.optionalFrom(\"language\")\n        url = map.optionalFrom(\"url\")\n    }\n\n}\n```\n\nThen you have methods that extends the response from Moya. These methods are:\n```swift\nmap(to:)\nmap(to:keyPath:)\ncompactMap(to:)\ncompactMap(to:keyPath)\n```\n\nWhile using `map(to:)` tries to map whole response data to object/array,\nwith `map(to:keyPath:)` you can specify nested object in a response to\nfetch. For example `map(to: User.self, keyPath: \"data.response.user\")` will go through\ndictionary of data, through dictionary of response to dictionary of user, which it\nwill parse. `compactMap` is a variant of array `map` that doesn't fail the whole operation\nif one of the objects fails, it will just remove the object from the array. \n`RxSwift` and `ReactiveCocoa` extensions also have all of the methods, but `RxSwift` have \noptional mapping additionally. See examples below, or in a Demo project.\n\n## 1. Normal usage (without RxSwift or ReactiveCocoa)\n\n```swift\nprovider = MoyaProvider\u003cGitHub\u003e(endpointClosure: endpointClosure)\nprovider.request(GitHub.repos(\"mjacko\")) { (result) in\n    if case .success(let response) = result {\n        do {\n            let repos = try response.map(to: [Repository].self)\n            print(repos)\n        } catch Error.jsonMapping(let error) {\n            print(try? error.mapString())\n        } catch {\n            print(\":(\")\n        }\n    }\n}\n```\n\n## 2. RxSwift\n```swift\nprovider = MoyaProvider\u003cGitHub\u003e(endpointClosure: endpointClosure)\nprovider.rx.request(GitHub.repo(\"Moya/Moya\"))\n    .map(to: User.self, keyPath: \"owner\")\n    .subscribe { event in\n        switch event {\n        case .success(let user):\n            print(user)\n        case .error(let error):\n            print(error)\n        }\n}\n```\n\nAdditionally, modules for `RxSwift` contains optional mappings. It basically means that if the mapping fails, mapper doesn't throw errors but returns nil. For instance:\n\n```swift\nprovider = MoyaProvider\u003cGitHub\u003e(endpointClosure: endpointClosure)\nprovider.rx.request(GitHub.repos(\"mjacko\"))\n    .mapOptional(to: [Repository].self)\n    .subscribe { event in\n        switch event {\n        case .success(let repos):\n            // Here we can have either nil or [Repository] object.\n            print(repos)\n        case .error(let error):\n            print(error)\n        }\n}\n```\n\n\n## 3. ReactiveSwift\n```swift\nprovider = MoyaProvider\u003cGitHub\u003e(endpointClosure: endpointClosure)\nprovider.reactive.request(GitHub.repos(\"mjacko\"))\n    .map(to: [Repository].self)\n    .observeOn(UIScheduler())\n    .start { event in\n        switch event {\n        case .value(let repos):\n            print(repos)\n        case .failed(let error):\n            print(error)\n        default: break\n        }\n}\n```\n\n## Author\n\nSunshinejr, thesunshinejr@gmail.com, \u003ca href=\"https://twitter.com/thesunshinejr\"\u003e@thesunshinejr\u003c/a\u003e\n\n## License\n\nMoya-ModelMapper is available under the MIT license. See the LICENSE file for more info.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsunshinejr%2Fmoya-modelmapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsunshinejr%2Fmoya-modelmapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsunshinejr%2Fmoya-modelmapper/lists"}