{"id":1719,"url":"https://github.com/lyft/mapper","last_synced_at":"2025-09-29T00:32:14.717Z","repository":{"id":2699861,"uuid":"44777644","full_name":"lyft/mapper","owner":"lyft","description":"A JSON deserialization library for Swift","archived":true,"fork":false,"pushed_at":"2024-05-17T01:37:34.000Z","size":263,"stargazers_count":1175,"open_issues_count":7,"forks_count":88,"subscribers_count":91,"default_branch":"master","last_synced_at":"2024-10-23T13:34:55.535Z","etag":null,"topics":["lyft"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lyft.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","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-10-22T22:46:01.000Z","updated_at":"2024-07-19T23:10:11.000Z","dependencies_parsed_at":"2024-06-18T12:40:56.343Z","dependency_job_id":null,"html_url":"https://github.com/lyft/mapper","commit_stats":{"total_commits":127,"total_committers":14,"mean_commits":9.071428571428571,"dds":"0.25984251968503935","last_synced_commit":"c8f848c8a496e8dbfafa4fba5bb44723bacbe853"},"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lyft%2Fmapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lyft%2Fmapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lyft%2Fmapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lyft%2Fmapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lyft","download_url":"https://codeload.github.com/lyft/mapper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234575225,"owners_count":18854925,"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":["lyft"],"created_at":"2024-01-05T20:15:54.188Z","updated_at":"2025-09-29T00:32:09.339Z","avatar_url":"https://github.com/lyft.png","language":"Swift","funding_links":[],"categories":["Parsing","Swift"],"sub_categories":["JSON","Other free courses"],"readme":"# Mapper\n\nMapper is a simple Swift library to convert JSON to strongly typed\nobjects. One advantage to Mapper over some other libraries is you can\nhave immutable properties.\n\n## Installation\n\n#### With [CocoaPods](http://cocoapods.org/)\n\n```ruby\nuse_frameworks!\n\npod \"ModelMapper\"\n```\n\n#### With [Carthage](https://github.com/Carthage/Carthage)\n\n```\ngithub \"lyft/mapper\"\n```\n\n## Usage\n\n### Simple example:\n\n```swift\nimport Mapper\n// Conform to the Mappable protocol\nstruct User: Mappable {\n  let id: String\n  let photoURL: URL?\n\n  // Implement this initializer\n  init(map: Mapper) throws {\n    try id = map.from(\"id\")\n    photoURL = map.optionalFrom(\"avatar_url\")\n  }\n}\n\n// Create a user!\nlet JSON: NSDictionary = ...\nlet user = User.from(JSON) // This is a 'User?'\n```\n\n### Using with enums:\n\n```swift\nenum UserType: String {\n  case Normal = \"normal\"\n  case Admin = \"admin\"\n}\n\nstruct User: Mappable {\n  let id: String\n  let type: UserType\n\n  init(map: Mapper) throws {\n    try id = map.from(\"id\")\n    try type = map.from(\"user_type\")\n  }\n}\n```\n\n### Nested `Mappable` objects:\n\n```swift\nstruct User: Mappable {\n  let id: String\n  let name: String\n\n  init(map: Mapper) throws {\n    try id = map.from(\"id\")\n    try name = map.from(\"name\")\n  }\n}\n\nstruct Group: Mappable {\n  let id: String\n  let users: [User]\n\n  init(map: Mapper) throws {\n    try id = map.from(\"id\")\n    users = map.optionalFrom(\"users\") ?? []\n  }\n}\n```\n\n### Use `Convertible` to transparently convert other types from JSON:\n\n```swift\nextension CLLocationCoordinate2D: Convertible {\n  public static func fromMap(_ value: Any) throws -\u003e CLLocationCoordinate2D {\n    guard let location = value as? NSDictionary,\n      let latitude = location[\"lat\"] as? Double,\n      let longitude = location[\"lng\"] as? Double else\n      {\n         throw MapperError.convertibleError(value: value, type: [String: Double].self)\n      }\n\n      return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)\n  }\n}\n\nstruct Place: Mappable {\n  let name: String\n  let location: CLLocationCoordinate2D\n\n  init(map: Mapper) throws {\n    try name = map.from(\"name\")\n    try location = map.from(\"location\")\n  }\n}\n\nlet JSON: NSDictionary = [\n  \"name\": \"Lyft HQ\",\n  \"location\": [\n    \"lat\": 37.7603392,\n    \"lng\": -122.41267249999999,\n  ],\n]\n\nlet place = Place.from(JSON)\n```\n\n### Custom Transformations\n\n```swift\nprivate func extractFirstName(object: Any?) throws -\u003e String {\n  guard let fullName = object as? String else {\n    throw MapperError.convertibleError(value: object, type: String.self)\n  }\n\n  let parts = fullName.characters.split { $0 == \" \" }.map(String.init)\n  if let firstName = parts.first {\n    return firstName\n  }\n\n  throw MapperError.customError(field: nil, message: \"Couldn't split the string!\")\n}\n\nstruct User: Mappable {\n  let firstName: String\n\n  init(map: Mapper) throws {\n    try firstName = map.from(\"name\", transformation: extractFirstName)\n  }\n}\n```\n\n### Parse nested or entire objects\n\n```swift\nstruct User: Mappable {\n  let name: String\n  let JSON: AnyObject\n\n  init(map: Mapper) throws {\n    // Access the 'first' key nested in a 'name' dictionary\n    try name = map.from(\"name.first\")\n    // Access the original JSON (maybe for use with a transformation)\n    try JSON = map.from(\"\")\n  }\n}\n```\n\nSee the docstrings and tests for more information and examples.\n\n## Open Radars\n\nThese radars have affected the current implementation of Mapper\n\n- [rdar://23376350](http://www.openradar.me/radar?id=5669622346940416)\n  Protocol extensions with initializers do not work in extensions\n- [rdar://23358609](http://www.openradar.me/radar?id=4926300410085376)\n  Protocol extensions with initializers do not play well with classes\n- [rdar://23226135](http://www.openradar.me/radar?id=5066210983018496)\n  Can't conform to protocols with similar generic function signatures\n- [rdar://23147654](http://www.openradar.me/radar?id=4991483920777216)\n  Generic functions are not differentiated by their ability to throw\n- [rdar://23695200](http://www.openradar.me/radar?id=5060084212170752)\n  Using the `??` operator many times is unsustainable.\n- [rdar://23697280](http://www.openradar.me/radar?id=5049833333194752)\n  Lazy collection elements can be evaluated multiple times.\n- [rdar://23718307](http://www.openradar.me/radar?id=4926133845884928)\n  Non final class with protocol extensions returning `Self` don't work\n\n## License\n\nMapper is maintained by [Lyft](https://www.lyft.com/) and released under\nthe Apache 2.0 license. See LICENSE for details\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flyft%2Fmapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flyft%2Fmapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flyft%2Fmapper/lists"}