{"id":1717,"url":"https://github.com/hodinkee/alexander","last_synced_at":"2025-08-02T04:32:26.507Z","repository":{"id":56901802,"uuid":"39414275","full_name":"hodinkee/alexander","owner":"hodinkee","description":"An extremely simple JSON helper written in Swift.","archived":true,"fork":false,"pushed_at":"2019-03-29T12:54:13.000Z","size":137,"stargazers_count":36,"open_issues_count":2,"forks_count":4,"subscribers_count":14,"default_branch":"master","last_synced_at":"2024-11-07T07:47:36.168Z","etag":null,"topics":[],"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/hodinkee.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":"2015-07-20T23:59:26.000Z","updated_at":"2023-02-02T22:53:49.000Z","dependencies_parsed_at":"2022-08-20T18:50:34.793Z","dependency_job_id":null,"html_url":"https://github.com/hodinkee/alexander","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hodinkee%2Falexander","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hodinkee%2Falexander/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hodinkee%2Falexander/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hodinkee%2Falexander/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hodinkee","download_url":"https://codeload.github.com/hodinkee/alexander/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228439110,"owners_count":17920018,"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-01-05T20:15:54.145Z","updated_at":"2024-12-06T08:31:31.740Z","avatar_url":"https://github.com/hodinkee.png","language":"Swift","funding_links":[],"categories":["Parsing","Libs"],"sub_categories":["JSON","Other free courses","Data Management"],"readme":"# Alexander\n\n[![Carthage Compatible](https://img.shields.io/badge/carthage-compatible-4BC51D.svg)](https://github.com/Carthage/Carthage)\n[![CocoaPods Compatible](https://img.shields.io/cocoapods/v/Alexander.svg)](https://cocoapods.org/pods/Alexander)\n\nAlexander is an extremely simple JSON helper written in Swift. It brings type safety and Foundation helpers to the cumbersome task of JSON unpacking.\n\n## Requirements\n\n|  Xcode  |  Swift  |  iOS  |  tvOS  |  OS X  |\n| :-----: | :-----: | :---: | :----: | :----: |\n| 10.2    | 5.0     | 8.0   | 9.0    | 10.9   |\n\n## Installation\n\n##### [Carthage](https://github.com/carthage/carthage)\n\n\u003e `github \"hodinkee/alexander\"`\n\n##### [CocoaPods](https://github.com/cocoapods/cocoapods)\n\n\u003e `pod 'Alexander'`\n\n## Usage\n\n### DecoderType\n\nMake a new `DecoderType` that can unpack your object.\n\n```swift\nstruct User {\n    var ID: String\n    var name: String\n    var email: String\n}\n\nstruct UserDecoder: DecoderType {\n    typealias Value = User\n    static func decode(JSON: Alexander.JSON) -\u003e Value? {\n        guard\n            let ID = JSON[\"id\"]?.stringValue,\n            let name = JSON[\"name\"]?.stringValue,\n            let email = JSON[\"email\"]?.stringValue\n        else {\n            return nil\n        }\n        return User(ID: ID, name: name, email: email)\n    }\n}\n```\n\nNow you can do `let author = JSON[\"user\"]?.decode(UserDecoder)` to get a single user, or `let users = JSON[\"users\"]?.decodeArray(UserDecoder)` to get an array of users.\n\nYou can make `DecodableType`s for all kinds of things.\n\n```swift\nstruct SizeDecoder {\n    typealias Value = CGSize\n    static func decode(JSON: Alexander.JSON) -\u003e Value? {\n        guard\n            let width = JSON[\"width\"]?.doubleValue,\n            let height = JSON[\"height\"]?.doubleValue\n        else {\n            return nil\n        }\n        return CGSize(width: width, height: height)\n    }\n}\n```\n\nAlexander ships with a handful of decoders for common types:\n\n- `DateTimeIntervalSince1970Decoder`\n- `DateTimeIntervalSinceReferenceDateDecoder`\n- `URLDecoder`\n- `RawRepresentableDecoder`\n\n### Nested Objects\n\nMost of Alexander's power comes from its two subscript operators: `subscript[key: String] -\u003e JSON?` and `subscript[index: Int] -\u003e JSON?`. These operators allow you to unpack nested objects without having to refer to each intermediate step by hand. Something like `let nextCursor = JSON[\"meta\"]?[\"pagination\"]?[\"next_cursor\"]?.stringValue` is a single line of code.\n\n### Enums \u0026 RawRepresentable\n\nYou can also decode anything that conforms to the `RawRepresentable` type. For example, assume the following enum:\n\n```swift\nenum Planet: String {\n    case Mercury = \"mercury\"\n    case Venus = \"venus\"\n    case Earth = \"earth\"\n    case Mars = \"mars\"\n    case Jupiter = \"jupiter\"\n    case Saturn = \"saturn\"\n    case Uranus = \"uranus\"\n    case Neptune = \"neptune\"\n    // case Pluto = \"pluto\" =(\n}\n```\n\nBecause `Planet` is backed by a `String` raw value type, it is inheriently `RawRepresentable`. This means you can do `let planet = JSON[\"planet\"]?.decode(RawRepresentableDecoder\u003cPlanet\u003e)` or `let planets = JSON[\"planets\"]?.decodeArray(RawRepresentableDecoder\u003cPlanet\u003e)`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhodinkee%2Falexander","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhodinkee%2Falexander","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhodinkee%2Falexander/lists"}