{"id":20590853,"url":"https://github.com/alickbass/swiftyjsonmodel","last_synced_at":"2025-04-14T22:35:33.753Z","repository":{"id":62456906,"uuid":"68551532","full_name":"alickbass/SwiftyJSONModel","owner":"alickbass","description":"Better way to use SwiftyJSON with custom models","archived":false,"fork":false,"pushed_at":"2018-01-28T10:33:11.000Z","size":148,"stargazers_count":29,"open_issues_count":3,"forks_count":11,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-12T04:05:11.523Z","etag":null,"topics":["json","json-model-mapping","protocol-oriented","swiftyjson","type-safe"],"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/alickbass.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-09-18T22:50:45.000Z","updated_at":"2025-03-28T21:27:47.000Z","dependencies_parsed_at":"2022-11-02T00:15:16.708Z","dependency_job_id":null,"html_url":"https://github.com/alickbass/SwiftyJSONModel","commit_stats":null,"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alickbass%2FSwiftyJSONModel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alickbass%2FSwiftyJSONModel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alickbass%2FSwiftyJSONModel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alickbass%2FSwiftyJSONModel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alickbass","download_url":"https://codeload.github.com/alickbass/SwiftyJSONModel/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248974484,"owners_count":21192152,"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","json-model-mapping","protocol-oriented","swiftyjson","type-safe"],"created_at":"2024-11-16T07:38:26.930Z","updated_at":"2025-04-14T22:35:33.702Z","avatar_url":"https://github.com/alickbass.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SwiftyJSONModel\nA microframework that helps to use [Swifty JSON](https://github.com/SwiftyJSON/SwiftyJSON) with your custom models in a easy and type-safe way\n\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) \n[![Build Status](https://travis-ci.org/alickbass/SwiftyJSONModel.svg?branch=master)](https://travis-ci.org/alickbass/SwiftyJSONModel)\n[![codecov](https://codecov.io/gh/alickbass/SwiftyJSONModel/branch/master/graph/badge.svg)](https://codecov.io/gh/alickbass/SwiftyJSONModel)\n\n## Motivation:\n\n### The full motivation and description is [here](https://medium.com/@alickdikan/type-safe-json-in-swift-with-swiftyjsonmodel-89d432a8c1ee#.d07xuncxy)\n\nFor the following `JSON`:\n\n```json\n{\n  \"name\": \"John\",\n  \"age\": 25,\n  \"isMarried\": false,\n  \"height\": 170.0,\n  \"address\": {\n  \t\"city\": \"San-Fransisco\",\n  \t\"country\": \"USA\"\n  },\n  \"hobbies\": [\"bouldering\", \"guitar\", \"swift:)\"]\n}\n```\n\nto map to the following model:\n\n```swift\nstruct Person {\n    let name: String\n    let age: Int\n    let isMarried: Bool\n    let height: Double\n    let city: String\n    let country: String\n    let hobbies: [String]?\n}\n```\n\n**Instead** of the following:\n\n```swift\nextension Person {\n    init?(jsonDict: [String: Any]) {\n        guard let name = jsonDict[\"name\"] as? String,\n            let age = jsonDict[\"age\"] as? Int,\n            let isMarried = jsonDict[\"isMarried\"] as? Bool,\n            let height = jsonDict[\"height\"] as? Double,\n            let address = jsonDict[\"address\"] as? [String: Any],\n            let city = address[\"city\"] as? String,\n            let country = address[\"country\"] as? String\n            else {\n            return nil\n        }\n        \n        self.name = name\n        self.age = age\n        self.isMarried = isMarried\n        self.height = height\n        self.city = city\n        self.country = country\n        hobbies = jsonDict[\"hobbies\"] as? [String]\n    }\n}\n```\n\n**We get the following:**\n\n```swift\nimport SwiftyJSONModel\n\nextension Person: JSONModelType {\n    enum PropertyKey: String {\n        case name, age, isMarried, height, hobbies\n        case address, country, city\n    }\n    \n    init(object: JSONObject\u003cPropertyKey\u003e) throws {\n        name = try object.value(for: .name)\n        age = try object.value(for: .age)\n        isMarried = try object.value(for: .isMarried)\n        height = try object.value(for: .height)\n        city = try object.value(for: .address, .city) // Accessing nested json\n        country = try object.value(for: .address, .country) // Accessing nested json\n        hobbies = try object.value(for: .hobbies)\n    }\n    \n    var dictValue: [PropertyKey : JSONRepresentable?] {\n        return [\n            .name: name,\n            .age: age,\n            .isMarried: isMarried,\n            .height: height,\n            .city: city,\n            .country: country,\n            .hobbies: hobbies?.jsonRepresantable,\n        ]\n    }\n}\n```\n\n## What we improved:\n* Keys are now restricted to the `PropertyKey` enum and we will have a compile error if we try to use something different.\n* Autocomplition will help us navigate through available keys\n* The constructor now `throws` which means that the init will fail if some value or it's type was different from what we expected and the `Error` will tell us exactly which property in `JSON` was wrong\n* The type of the value for key is now inferred from the property we specify. That means we do not need to have all this boilerplate code with casting to `String` or `Int`. It will be done for us.\n\n#Integration\n\n## CocoaPods (iOS 8+)\n\nYou can use CocoaPods to install SwiftyJSONModel by adding it to your Podfile:\n\n```swift\nplatform :ios, '8.0'\nuse_frameworks!\n\ntarget 'MyApp' do\npod 'SwiftyJSONModel'\nend\n```\n\nNote that this requires CocoaPods version 36, and your iOS deployment target to be at least 8.0:\n\n## Carthage (iOS 8+)\n\nYou can use Carthage to install SwiftyJSONModel by adding it to your Cartfile:\n\n```swift\ngithub \"alickbass/SwiftyJSONModel\"\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falickbass%2Fswiftyjsonmodel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falickbass%2Fswiftyjsonmodel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falickbass%2Fswiftyjsonmodel/lists"}