{"id":25021933,"url":"https://github.com/gerzer/jsonparser","last_synced_at":"2025-04-13T06:20:51.097Z","repository":{"id":63910828,"uuid":"304707322","full_name":"Gerzer/JSONParser","owner":"Gerzer","description":"Elegant, type-safe JSON parsing in Swift","archived":false,"fork":false,"pushed_at":"2021-10-17T00:46:22.000Z","size":16,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-26T22:51:18.799Z","etag":null,"topics":["codable","json","json-parser","parser","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Gerzer.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-10-16T18:23:37.000Z","updated_at":"2022-04-20T07:19:06.000Z","dependencies_parsed_at":"2023-01-14T13:16:04.076Z","dependency_job_id":null,"html_url":"https://github.com/Gerzer/JSONParser","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gerzer%2FJSONParser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gerzer%2FJSONParser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gerzer%2FJSONParser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gerzer%2FJSONParser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Gerzer","download_url":"https://codeload.github.com/Gerzer/JSONParser/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248671375,"owners_count":21143078,"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":["codable","json","json-parser","parser","swift"],"created_at":"2025-02-05T13:37:41.109Z","updated_at":"2025-04-13T06:20:51.075Z","avatar_url":"https://github.com/Gerzer.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JSONParser\nElegant, type-safe JSON parsing in Swift\n\nIf you're a clever programmer, then you probably use `Codable` types wherever you can do so. However, sometimes you need to parse some JSON data the structure of which you don't control. Previously, this was a painful operation:\n\n```swift\nlet object = try (JSONSerialization.jsonObject(with: data) as! [String: Any])\nguard let foo = object[\"foo\"] as? Int, let bar = object[\"bar\"] as? String, let array = object[\"array\"] as? [Any] else {\n\treturn\n}\nguard let number = array[0] as? Double, let word = array[1] as? String, let dictionary = array[3] as? [String: Int] else {\n\treturn\n}\nguard let hello = dictionary[\"hello\"] else {\n\treturn\n}\n```\n\nWith JSONParser, this process is **elegant and effortless**:\n\n```swift\nlet parser = data.dictionaryParser // `data` is an instance of `Data` from `Foundation`\nlet foo = parser[\"foo\", as: Int.self]\nlet bar = parser[\"bar\", as: String.self]\nlet number = parser[arrayAt: \"array\"]?[0, as: Double.self]\nlet word = parser[arrayAt: \"array\"]?[1, as: String.self]\nlet hello = parser[arrayAt: \"array\"]?[dictionaryAt: 2]?[\"hello\", as: Int.self]\n```\n\nYou can also **iterate over heterogenous JSON arrays *and* dictionaries**:\n\n```swift\ntry data.arrayParser.iterate { (proxy) in\n\tif let value = proxy.get(as: Int.self) {\n\t\tprint(value * 2)\n\t} else if let value = proxy.get(as: Bool.self) {\n\t\tprint(!value)\n\t}\n}\n```\n\nJSONParser can **work with other providers**, not just `Data` from `Foundation`. Just have your custom class or struct conform to the `JSONProvider` protocol. It has only one mandatory interface member:\n\n```swift\n// This `Set` extension is already provided in the JSONParser package; use it as a guide for writing your own extensions for other types\nextension Set: JSONProvider {\n\n\tpublic var parser: ArrayJSONParser? {\n\t\tget {\n\t\t\treturn Array(self).parser\n\t\t}\n\t}\n\n}\n```\n\nConvenient functions and subscript accessors are automatically provided for free, requiring **no manual implementation**:\n\n```swift\nlet array = [\n\t0,\n\t1,\n\t[\n\t\t\"2\": true\n\t]\n] as [AnyHashable]\nlet set = Set(array)\ntry set.iterate { (proxy) in\n\tprint(proxy.get(as: Int.self) ?? proxy.get(as: [String: Bool].self)!)\n}\n```\n\nJSONParser builds on top of Swift's robust generic-typing system to provide an **easy, intuitive API** for interacting with JSON data. It has **no external dependencies** and relies solely on `Foundation` APIs, so it **works on Linux and Windows** in addition to the various Apple platforms.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgerzer%2Fjsonparser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgerzer%2Fjsonparser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgerzer%2Fjsonparser/lists"}