{"id":13818409,"url":"https://github.com/PhonePe/BetterMappable","last_synced_at":"2025-05-15T23:31:54.594Z","repository":{"id":39365160,"uuid":"242764521","full_name":"PhonePe/BetterMappable","owner":"PhonePe","description":"Better Mappable through Property Wrappers using ObjectMapper","archived":false,"fork":false,"pushed_at":"2020-04-29T10:30:53.000Z","size":66,"stargazers_count":47,"open_issues_count":0,"forks_count":4,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-05-07T00:38:07.516Z","etag":null,"topics":["carthage","carthage-support","cocoapods","ios","json-parser","macos","objectmapper","phonepe","property-wrappers","propertywrapper","swift","swift-package-manager","swift5","swiftpackage","swiftpackagemanager","xcode"],"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/PhonePe.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":"2020-02-24T14:58:01.000Z","updated_at":"2024-03-04T12:52:18.000Z","dependencies_parsed_at":"2022-08-21T02:50:17.098Z","dependency_job_id":null,"html_url":"https://github.com/PhonePe/BetterMappable","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/PhonePe%2FBetterMappable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PhonePe%2FBetterMappable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PhonePe%2FBetterMappable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PhonePe%2FBetterMappable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PhonePe","download_url":"https://codeload.github.com/PhonePe/BetterMappable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254442317,"owners_count":22071863,"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":["carthage","carthage-support","cocoapods","ios","json-parser","macos","objectmapper","phonepe","property-wrappers","propertywrapper","swift","swift-package-manager","swift5","swiftpackage","swiftpackagemanager","xcode"],"created_at":"2024-08-04T07:00:45.312Z","updated_at":"2025-05-15T23:31:54.301Z","avatar_url":"https://github.com/PhonePe.png","language":"Swift","readme":"# BetterMappable\n[![Swift 5.1](https://img.shields.io/badge/Swift-5.1-orange.svg)](https://swift.org)\n[![CocoaPods](https://img.shields.io/cocoapods/v/BetterMappable.svg)](https://github.com/PhonePe/BetterMappable)\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n[![Swift Package Manager](https://rawgit.com/jlyonsmith/artwork/master/SwiftPackageManager/swiftpackagemanager-compatible.svg)](https://swift.org/package-manager/)\n\nBetter Mappable utilising Swift 5 [`Property Wrappers`](https://nshipster.com/propertywrapper/) is a μframework written on top of [`ObjectMapper`](https://github.com/tristanhimmelman/ObjectMapper) that makes it easy to convert model objects (classes and structs) to and from JSON. Results in reduction of a lot of boiler plate code in models. \n\n# Index\n- [JSONProperty](#JSONProperty)\n- [JSONObject](#JSONObject)\n- [JSONObjectArray](#JSONObjectArray)\n- [JSONObjectDictionary](#JSONObjectDictionary)\n- [JSONPropertyWithTransform](#JSONPropertyWithTransform)\n- [JSONObjectDictionaryArrayValue](#JSONObjectDictionaryArrayValue)\n\n# JSONProperty\nUtilise this property wrapper to map any primitive variables (`Int`, `String`, `Bool` etc...)\n```swift\nstruct User: Mappable {\n    @JSONProperty var name: String?\n    @JSONProperty var age: Int?\n    @JSONProperty var onPhonePe: Bool?\n\n    init?(map: Map) {\n    }\n}\n```\n\nThat's it! For those who are familiar with `ObjectMapper`, yes, you do not have to implement `func mapping(map: Map)` at all. We automatically take the variable name and assign right value from/to JSON. Incase your variable name is different from the `key` in the JSON, you can provide it as a parameter in `JSONProperty`.\n```swift\n@JSONProperty(key: \"on_phonepe\") \nvar onPhonePe: Bool?\n```\n\nYou can provide default value to a variable like the way you generally do with `ObjectMapper`.\n```swift\n@JSONProperty \nvar age: Int = 0\n    \n@JSONProperty(key: \"on_phonepe\")\nvar onPhonePe: Bool = false\n```\n\n# JSONObject\nUse this property wrapper to map custom classes/structs which confirms to `Mappable` protocol\n```swift\nstruct Student: Mappable {\n    @JSONObject var address: Address?\n    \n    @JSONProperty var firstName: String?\n    @JSONProperty var lastName: String?\n    \n    init?(map: Map) {\n    }\n}\n\nstruct Address: Mappable {\n    @JSONProperty var street: String?\n    @JSONProperty var building: String?\n    @JSONProperty var city: String?\n    @JSONProperty var state: String?\n    \n    init?(map: Map) {\n        \n    }\n}\n```\n\nLike the `JSONProperty` example, if your variable name is different from the `key` in the JSON, you can provide it as a parameter in `JSONObject`.\n```swift\n@JSONObject(key: \"student_address\") \nvar address: Address?\n```\n\n# JSONObjectArray\nUse this property wrapper to map array of custom classes/structs which confirms to `Mappable` protocol\n```swift\nstruct Store: Mappable {\n    @JSONObjectArray var transactions: [Transaction]?\n    \n    @JSONProperty var name: String?\n    \n    init?(map: Map) {\n    }\n}\n\nstruct Transaction: Mappable {\n    @JSONProperty var id: String?\n    \n    @JSONProperty var amount: Int = 0\n    \n    init?(map: Map) {\n    }\n}\n```\n\n# JSONObjectDictionary\nUse this property wrapper to map dictionary of custom classes/structs which confirms to `Mappable` protocol\n```swift\nstruct Categories: Mappable {\n    @JSONObjectDictionary var data: [String: Category]?\n    \n    init?(map: Map) {\n    }\n}\n\nstruct Category: Mappable {\n    @JSONProperty var id: String?\n    @JSONProperty var name: String?\n    \n    init?(map: Map) {\n    }\n}\n```\n\n# JSONObjectDictionaryArrayValue\nUse this property wrapper to map dictionary of custom array classes/structs which confirms to `Mappable` protocol\n```swift\nstruct Categories: Mappable {\n    @JSONObjectDictionaryArrayValue var data: [String: [Category]]?\n    \n    init?(map: Map) {\n    }\n}\n\nstruct Category: Mappable {\n    @JSONProperty var id: String?\n    @JSONProperty var name: String?\n    \n    init?(map: Map) {\n    }\n}\n```\n\n# JSONPropertyWithTransform\nUse this property wrapper to map variables with transformation.\n```swift\nstruct Organization: Mappable {\n    enum Sector: String {\n        case `private` = \"PRIVATE\"\n        case `public` = \"PUBLIC\"\n    }\n    \n    @JSONPropertyWithTransform(transformer: DateTransform())\n    var startDate: Date?\n    \n    @JSONPropertyWithTransform(transformer: EnumTransform\u003cSector\u003e())\n    var sector: Sector?\n    \n    @JSONProperty var name: String?\n    \n    init?(map: Map) {\n    }\n}\n```\n\n# Does this work with StaticMappable?`\nYes it does. \n```swift\nclass Vehicle {\n    enum VehicleType: String {\n        case car = \"CAR\"\n        case bicycle = \"BICYCLE\"\n    }\n    \n    @JSONProperty var numberOfWheels: Int?\n}\n\nextension Vehicle: StaticMappable {\n    static func objectForMapping(map: Map) -\u003e BaseMappable? {\n        if let rawType = map.JSON[\"type\"] as? String,\n            let type = VehicleType(rawValue: rawType) {\n            switch type {\n            case .car:\n                return Car()\n                \n            case .bicycle:\n                return Bicycle()\n            }\n        }\n        \n        return nil\n    }\n}\n\nclass Car: Vehicle {\n    @JSONProperty var hasBonet: Bool?\n}\n\nclass Bicycle: Vehicle {\n    @JSONProperty var modelName: String?\n}\n```\n\n# Subclasses\n```swift\nclass Base: Mappable {\n    @JSONProperty var base: String?\n    \n    required init?(map: Map) {\n    }\n}\n\nclass Subclass: Base {\n    @JSONProperty var sub: String?\n\n    required init?(map: Map) {\n        super.init(map: map)\n    }\n}\n```\n\n## Installation\n\n### CocoaPods\n\n[CocoaPods](https://cocoapods.org) is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate BetterMappable into your Xcode project using CocoaPods, specify it in your `Podfile`:\n\n```ruby\npod 'BetterMappable', '~\u003e 1.0'\n```\n\n### Carthage\n\n[Carthage](https://github.com/Carthage/Carthage) is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate BetterMappable into your Xcode project using Carthage, specify it in your `Cartfile`:\n\n```ogdl\ngithub \"PhonePe/BetterMappable\" \"1.0.1\"\n```\n\n### Swift Package Manager\n\nThe [Swift Package Manager](https://swift.org/package-manager/) is a tool for automating the distribution of Swift code and is integrated into the `swift` compiler. It is in early development, but BetterMappable does support its use on supported platforms.\n\nOnce you have your Swift package set up, adding BetterMappable as a dependency is as easy as adding it to the `dependencies` value of your `Package.swift`.\n\n```swift\ndependencies: [\n    .package(url: \"https://github.com/PhonePe/BetterMappable.git\", .upToNextMajor(from: \"1.0.0\"))\n]\n```\n\n# Credits\n[Srikanth KV](https://twitter.com/SrikanthVKabadi)\n","funding_links":[],"categories":["Awesome Repositories"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPhonePe%2FBetterMappable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FPhonePe%2FBetterMappable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPhonePe%2FBetterMappable/lists"}