{"id":15038648,"url":"https://github.com/gperdomor/moyaobjectmapper","last_synced_at":"2025-10-04T03:31:59.569Z","repository":{"id":62448055,"uuid":"82612580","full_name":"gperdomor/MoyaObjectMapper","owner":"gperdomor","description":"ObjectMapper bindings for Moya, RxSwift and ReactiveSwift","archived":true,"fork":false,"pushed_at":"2017-05-29T07:55:03.000Z","size":87,"stargazers_count":11,"open_issues_count":4,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-29T08:34:32.600Z","etag":null,"topics":["json","mapper","moya","objectmapper","reactiveswift","rxswift","swift","swift3"],"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/gperdomor.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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-02-20T23:15:30.000Z","updated_at":"2024-05-21T19:52:54.000Z","dependencies_parsed_at":"2022-11-29T13:20:56.048Z","dependency_job_id":null,"html_url":"https://github.com/gperdomor/MoyaObjectMapper","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/gperdomor%2FMoyaObjectMapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gperdomor%2FMoyaObjectMapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gperdomor%2FMoyaObjectMapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gperdomor%2FMoyaObjectMapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gperdomor","download_url":"https://codeload.github.com/gperdomor/MoyaObjectMapper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235216209,"owners_count":18954253,"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":["json","mapper","moya","objectmapper","reactiveswift","rxswift","swift","swift3"],"created_at":"2024-09-24T20:39:29.853Z","updated_at":"2025-10-04T03:31:54.247Z","avatar_url":"https://github.com/gperdomor.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MoyaObjectMapper\n[![Build Status](https://travis-ci.org/gperdomor/MoyaObjectMapper.svg?branch=master)](https://travis-ci.org/gperdomor/MoyaObjectMapper)\n[![codecov](https://codecov.io/gh/gperdomor/MoyaObjectMapper/branch/master/graph/badge.svg)](https://codecov.io/gh/gperdomor/MoyaObjectMapper)\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n[![CocoaPods compatible](https://img.shields.io/cocoapods/v/MoyaObjectMapper.svg)](https://cocoapods.org/pods/MoyaObjectMapper)\n[![Swift Package Manager compatible](https://img.shields.io/badge/Swift%20Package%20Manager-compatible-brightgreen.svg)](https://github.com/apple/swift-package-manager)\n\n[ObjectMapper](https://github.com/Hearst-DD/ObjectMapper) bindings for \n[Moya](https://github.com/Moya/Moya) for easier JSON serialization. Includes\n[RxSwift](https://github.com/ReactiveX/RxSwift) and [ReactiveSwift](https://github.com/ReactiveCocoa/ReactiveSwift) bindings as well.\n\n# Installation\n\n## CocoaPods\nUse the following entry in your Podfile\n```\npod 'MoyaObjectMapper', '1.0.3'\n```\n\nThe subspec if you want to use the bindings over RxSwift.\n```\npod 'MoyaObjectMapper/RxSwift', '1.0.3'\n```\n\nAnd the subspec if you want to use the bindings over ReactiveSwift.\n```\npod 'MoyaObjectMapper/ReactiveSwift', '1.0.3'\n```\n\n# Usage\n\nCreate a model struct or class. It needs to implement protocol Mappable. More details about model creation [here](https://github.com/lyft/mapper/)\n\n```swift\nimport Foundation\nimport ObjectMapper\n\nstruct Repository: Mappable {\n    var identifier: Int!\n    var name: String!\n    var fullName: String!\n    var language: String?\n    \n    init?(map: Map) {\n        if map.JSON[\"id\"] == nil {\n            return nil\n        }\n    }\n    \n    mutating func mapping(map: Map) {\n        identifier \u003c- map[\"id\"]\n        name \u003c- map[\"name\"]\n        fullName \u003c- map[\"full_name\"]\n        language \u003c- map[\"language\"]\n    }\n}\n```\n\nThen you have methods that extends the response from Moya. These methods are:\n```swift\nmap(to: type, context: MapContext? = nil) // map object\nmap(to: [type], context: MapContext? = nil) // map array of objects\n```\n\nFor `RxSwift` and `ReactiveCocoa` additionally methods for optional mapping are provided.\nThese methods are:\n\n```swift\nmapOptional(to: type, context: MapContext? = nil)\nmapOptional(to: [type], context: MapContext? = nil)\n```\n\nSee examples below, or in a Demo project.\n\n## 1. Normal usage (without RxSwift or ReactiveCocoa)\n\n```swift\nprovider = MoyaProvider\u003cGitHub\u003e()\nprovider.request(GitHub.repos(username: \"gperdomor\")) { (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 = RxMoyaProvider\u003cGitHub\u003e()\nprovider.request(GitHub.repo(fullName: \"gperdomor/sygnaler\"))\n        .map(to: Repository.self)\n        .subscribe { event in\n            switch event {\n            case .next(let repo):\n                print(repo)\n            case .error(let error):\n                print(error)\n            default: break\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 = RxMoyaProvider\u003cGitHub\u003e()\nprovider\n    .request(GitHub.repos(username: \"gperdomor\"))\n    .mapOptional(to: [Repository.self])\n    .subscribe { event in\n        switch event {\n        case .next(let repos):\n            // Here we can have either nil or [Repository] object.\n            print(repos)\n        case .error(let error):\n            print(error)\n        default: break\n        }\n    }\n```\n\n## 3. ReactiveSwift\n```swift\nprovider = ReactiveSwiftMoyaProvider\u003cGitHub\u003e()\nprovider\n    .request(GitHub.repos(username: \"gperdomor\"))\n    .map(to: [Repository.self])\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\nAdditionally, modules for `ReactiveSwift` 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 = ReactiveSwiftMoyaProvider\u003cGitHub\u003e()\nprovider\n    .request(GitHub.repos(username: \"gperdomor\"))\n    .mapOptional(to: [Repository.self])\n    .start { event in\n        switch event {\n        case .value(let repos):\n            // Here we can have either nil or [Repository] object.\n            print(repos)\n        case .failed(let error):\n            print(error)\n        default: break\n        }\n    }\n```\n\n## Sample Project\n\nThere's a sample project in the Demo directory. To use it, run `pod install` to download the required libraries. Have fun!\n\n## Other Mappers\n - [MoyaModelMapper](https://github.com/gperdomor/MoyaModelMapper): ModelMapper bindings for Moya\n - [MoyaUnbox](https://github.com/gperdomor/MoyaUnbox): Unbox bindings for Moya\n\n## Contributing\n\nHey! Like MoyaObjectMapper? Awesome! We could actually really use your help!\n\nOpen source isn't just writing code. MoyaObjectMapper could use your help with any of the\nfollowing:\n\n- Finding (and reporting!) bugs.\n- New feature suggestions.\n- Answering questions on issues.\n- Documentation improvements.\n- Reviewing pull requests.\n- Helping to manage issue priorities.\n- Fixing bugs/new features.\n\nIf any of that sounds cool to you, send a pull request! After a few\ncontributions, we'll add you as an admin to the repo so you can merge pull\nrequests and help steer the ship :ship:\n\n## License\n\nMoyaObjectMapper 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%2Fgperdomor%2Fmoyaobjectmapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgperdomor%2Fmoyaobjectmapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgperdomor%2Fmoyaobjectmapper/lists"}