{"id":20217875,"url":"https://github.com/daltoniam/jsonjoy-swift","last_synced_at":"2025-04-05T13:06:08.004Z","repository":{"id":20872789,"uuid":"24159833","full_name":"daltoniam/JSONJoy-Swift","owner":"daltoniam","description":"Convert JSON to Swift objects.","archived":false,"fork":false,"pushed_at":"2017-11-08T18:44:15.000Z","size":76,"stargazers_count":354,"open_issues_count":2,"forks_count":62,"subscribers_count":27,"default_branch":"master","last_synced_at":"2025-03-29T12:05:51.661Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/daltoniam.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":"2014-09-17T19:47:38.000Z","updated_at":"2024-10-29T16:08:17.000Z","dependencies_parsed_at":"2022-07-26T12:47:08.679Z","dependency_job_id":null,"html_url":"https://github.com/daltoniam/JSONJoy-Swift","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daltoniam%2FJSONJoy-Swift","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daltoniam%2FJSONJoy-Swift/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daltoniam%2FJSONJoy-Swift/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daltoniam%2FJSONJoy-Swift/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/daltoniam","download_url":"https://codeload.github.com/daltoniam/JSONJoy-Swift/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247339155,"owners_count":20923014,"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":[],"created_at":"2024-11-14T06:36:13.345Z","updated_at":"2025-04-05T13:06:07.979Z","avatar_url":"https://github.com/daltoniam.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"JSONJoy\n=============\n\nConvert JSON to Swift objects. The Objective-C counterpart can be found here: [JSONJoy](https://github.com/daltoniam/JSONJoy).\n\nParsing JSON in Swift has be likened to a trip through Mordor, then JSONJoy would be using eagles for that trip.\n\n#### Deprecation\n\nThe release of Swift 4 brought support for the new `Codable` protocol. This renders most of JSONJoy unneccessary and thus will be deprecated. Version 3.0.2 has been update to support Swift 4 as a means of backward compatibility, but I would encourage the adoption of the new `Codable` protocol. I wrote a post about it [here](vluxe.io/codable-json-swift.html) \n\n\nFirst thing is to import the framework. See the Installation instructions on how to add the framework to your project.\n\n```swift\nimport JSONJoy\n```\n\n## Example\n\nFirst here is some example JSON we have to parse.\n\n```javascript\n{\n    \"id\" : 1,\n    \"first_name\": \"John\",\n    \"last_name\": \"Smith\",\n    \"age\": 25,\n    \"address\": {\n        \"id\": 1,\n        \"street_address\": \"2nd Street\",\n        \"city\": \"Bakersfield\",\n        \"state\": \"CA\",\n        \"postal_code\": 93309\n     }\n\n}\n```\n\nWe want to translate that JSON to these Swift objects:\n\n```swift\nstruct Address {\n    let objID: Int?\n    let streetAddress: String?\n    let city: String?\n    let state: String?\n    let postalCode: String?\n    init() {\n\n    }\n}\n\nstruct User {\n    let objID: Int?\n    let firstName: String?\n    let lastName: String?\n    let age: Int?\n    let address = Address()\n    init() {\n\n    }\n}\n```\n\nNormally this would put us in a validation nightmare:\n\n```\nvar user = User()\nvar error: NSError?\nvar response: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(), error: \u0026error)\nif let userDict = response as? NSDictionary {\n    if let addressDict = userDict[\"address\"] as? NSDictionary {\n        user.address.city = addressDict[\"city\"] as? String\n        user.address.streetAddress = addressDict[\"street_address\"] as? String\n        //etc, etc\n    }\n    user.firstName = userDict[\"first_name\"] as? String\n    user.lastName = userDict[\"last_name\"] as? String\n    //etc, etc\n}\n```\n\nJSONJoy makes this much simpler. We have our Swift objects implement the JSONJoy protocol:\n\n```swift\nstruct Address : JSONJoy {\n    let objID: Int\n    let streetAddress: String\n    let city: String\n    let state: String\n    let postalCode: String\n    let streetTwo: String?\n\n    init(_ decoder: JSONLoader) throws {\n        objID = try decoder[\"id\"].get()\n        streetAddress = try decoder[\"street_address\"].get()\n        city = try decoder[\"city\"].get()\n        state = try decoder[\"state\"].get()\n        postalCode = try decoder[\"postal_code\"].get()\n        streetTwo = decoder[\"street_two\"].getOptional()\n        \n        //just an example of \"checking\" for a property. \n        if let meta: String = decoder[\"meta\"].getOptional() {\n            print(\"found some meta info: \\(meta)\")\n        }\n    }\n}\n\nstruct User : JSONJoy {\n    let objID: Int\n    let firstName: String\n    let lastName: String\n    let age: Int\n    let address: Address\n    let addresses: [Address]\n\n    init(_ decoder: JSONLoader) throws {\n        objID = try decoder[\"id\"].get()\n        firstName = try decoder[\"first_name\"].get()\n        lastName = try decoder[\"last_name\"].get()\n        age = try decoder[\"age\"].get()\n        address = try Address(decoder[\"address\"])\n        addresses = try decoder[\"addresses\"].get() //infers the type and returns a valid array\n    }\n}\n```\n\nThen when we get the JSON back:\n\n```swift\ndo {\n\tvar user = try User(JSONLoader(data))\n\tprintln(\"city is: \\(user.address.city)\")\n\t//That's it! The object has all the appropriate properties mapped.\n} catch {\n\tprint(\"unable to parse the JSON\")\n}\n```\n\nThis also has automatic optional validation like most Swift JSON libraries.\n\n```swift\n//some randomly incorrect key. This will work fine and the property will just be nil.\nfirstName = decoder[5][\"wrongKey\"][\"MoreWrong\"].getOptional()\n//firstName is nil, but no crashing!\n```\n\n## Custom Types\n\nIf you to extend a standard Foundation type (you probably won't need to though)\n\n```swift\nextension UInt64:   JSONBasicType {}\n```\n\n## SwiftHTTP\n\nThis can be combined with SwiftHTTP to make API interaction really clean and easy.\n\nhttps://github.com/daltoniam/SwiftHTTP#clientserver-example\n\n## Requirements\n\nJSONJoy requires at least iOS 7/OSX 10.10 or above.\n\n## Installation\n\n### Swift Package Manager\n\nAdd the project as a dependency to your Package.swift:\n```swift\nimport PackageDescription\n\nlet package = Package(\n    name: \"YourProject\",\n    dependencies: [\n        .Package(url: \"https://github.com/daltoniam/JSONJoy-Swift\", majorVersion: 3)\n    ]\n)\n```\n\n### CocoaPods\n\nCheck out [Get Started](http://cocoapods.org/) tab on [cocoapods.org](http://cocoapods.org/).\n\nTo use JSONJoy-Swift in your project add the following 'Podfile' to your project\n\n\tsource 'https://github.com/CocoaPods/Specs.git'\n\tplatform :ios, '8.0'\n\tuse_frameworks!\n\n\tpod 'JSONJoy-Swift', '~\u003e 3.0.2'\n\nThen run:\n\n    pod install\n\n\n### Carthage\n\nCheck out the [Carthage](https://github.com/Carthage/Carthage) docs on how to add a install. The `JSONJoy` framework is already setup with shared schemes.\n\n[Carthage Install](https://github.com/Carthage/Carthage#adding-frameworks-to-an-application)\n\nYou can install Carthage with [Homebrew](http://brew.sh/) using the following command:\n\n```bash\n$ brew update\n$ brew install carthage\n```\n\nTo integrate JSONJoy into your Xcode project using Carthage, specify it in your `Cartfile`:\n\n```\ngithub \"daltoniam/JSONJoy-Swift\" \u003e= 3.0.2\n```\n\n### Rogue\n\nFirst see the [installation docs](https://github.com/acmacalister/Rogue) for how to install Rogue.\n\nTo install JSONJoy run the command below in the directory you created the rogue file.\n\n```\nrogue add https://github.com/daltoniam/JSONJoy-Swift\n```\n\nNext open the `libs` folder and add the `JSONJoy.xcodeproj` to your Xcode project. Once that is complete, in your \"Build Phases\" add the `JSONJoy.framework` to your \"Link Binary with Libraries\" phase. Make sure to add the `libs` folder to your `.gitignore` file.\n\n### Other\n\nSimply grab the framework (either via git submodule or another package manager).\n\nAdd the `JSONJoy.xcodeproj` to your Xcode project. Once that is complete, in your \"Build Phases\" add the `JSONJoy.framework` to your \"Link Binary with Libraries\" phase.\n\n### Add Copy Frameworks Phase\n\nIf you are running this in an OSX app or on a physical iOS device you will need to make sure you add the `JSONJoy.framework` included in your app bundle. To do this, in Xcode, navigate to the target configuration window by clicking on the blue project icon, and selecting the application target under the \"Targets\" heading in the sidebar. In the tab bar at the top of that window, open the \"Build Phases\" panel. Expand the \"Link Binary with Libraries\" group, and add `JSONJoy.framework`. Click on the + button at the top left of the panel and select \"New Copy Files Phase\". Rename this new phase to \"Copy Frameworks\", set the \"Destination\" to \"Frameworks\", and add `JSONJoy.framework`.\n\n## TODOs\n\n- [ ] Add Unit Tests\n\n## License\n\nJSONJoy is licensed under the Apache v2 License.\n\n## Contact\n\n### Dalton Cherry ###\n* https://github.com/daltoniam\n* http://twitter.com/daltoniam\n* http://daltoniam.com\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaltoniam%2Fjsonjoy-swift","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdaltoniam%2Fjsonjoy-swift","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaltoniam%2Fjsonjoy-swift/lists"}