{"id":32151854,"url":"https://github.com/iwill/generic-json-swift","last_synced_at":"2025-12-11T23:01:40.637Z","repository":{"id":24397629,"uuid":"101390267","full_name":"iwill/generic-json-swift","owner":"iwill","description":"A simple Swift library for working with generic JSON structures","archived":false,"fork":false,"pushed_at":"2022-11-07T03:46:24.000Z","size":87,"stargazers_count":181,"open_issues_count":6,"forks_count":26,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-10-21T10:53:58.903Z","etag":null,"topics":["codable","json","swift","type-safety"],"latest_commit_sha":null,"homepage":"https://stackoverflow.com/questions/43843604","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/iwill.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2017-08-25T09:46:39.000Z","updated_at":"2025-09-10T08:13:20.000Z","dependencies_parsed_at":"2022-07-25T10:47:43.868Z","dependency_job_id":null,"html_url":"https://github.com/iwill/generic-json-swift","commit_stats":null,"previous_names":["zoul/generic-json-swift"],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/iwill/generic-json-swift","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iwill%2Fgeneric-json-swift","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iwill%2Fgeneric-json-swift/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iwill%2Fgeneric-json-swift/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iwill%2Fgeneric-json-swift/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iwill","download_url":"https://codeload.github.com/iwill/generic-json-swift/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iwill%2Fgeneric-json-swift/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280248570,"owners_count":26297925,"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","status":"online","status_checked_at":"2025-10-21T02:00:06.614Z","response_time":58,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","swift","type-safety"],"created_at":"2025-10-21T10:54:14.182Z","updated_at":"2025-10-21T10:54:15.217Z","avatar_url":"https://github.com/iwill.png","language":"Swift","readme":"# Generic JSON\n\n[![Build Status](https://travis-ci.org/zoul/generic-json-swift.svg?branch=master)](https://travis-ci.org/zoul/generic-json-swift)\n\nGeneric JSON makes it easy to deal with freeform JSON strings without creating a separate, well-typed structure.\n\n## Codable and freeform JSON\n\nSwift 4 introduced a new JSON encoding and decoding machinery represented by the `Codable` protocol. The feature is very nice and very type-safe, meaning it’s no longer possible to just willy-nilly decode a JSON string pulling random untyped data from it. Which is good™ most of the time – but what should you do when you _do_ want to just willy-nilly encode or decode a JSON string without introducing a separate, well-typed structure for it? For example:\n\n```swift\n// error: heterogeneous collection literal could only be inferred to '[String : Any]';\n// add explicit type annotation if this is intentional\nlet json = [\n    \"foo\": \"foo\",\n    \"bar\": 1,\n]\n\n// Okay then:\nlet json: [String:Any] = [\n    \"foo\": \"foo\",\n    \"bar\": 1,\n]\n\n// But: fatal error: Dictionary\u003cString, Any\u003e does not conform to Encodable because Any does not conform to Encodable.\nlet encoded = try JSONEncoder().encode(json)\n```\n\nSo this doesn’t work very well. Also, the `json` value can’t be checked for equality with another, although arbitrary JSON values _should_ support equality. Enter `JSON`.\n\n## Usage\n\n### Create a `JSON` structure\n\n```swift\nlet json: JSON = [\n    \"foo\": \"foo\",\n    \"bar\": 1,\n]\n\n// \"{\"bar\":1,\"foo\":\"foo\"}\"\nlet str = try String(data: try JSONEncoder().encode(json), encoding: .utf8)!\nlet hopefullyTrue = (json == json) // true!\n```\n\n### Convert `Encodable` objects into a generic JSON structure\n\n```swift\nstruct Player: Codable {\n    let name: String\n    let swings: Bool\n}\n\nlet val = try JSON(encodable: Player(name: \"Miles\", swings: true))\nval == [\n    \"name\": \"Miles\",\n    \"swings\": true,\n] // true\n```\n\n### Query Values\n\nConsider the following `JSON` structure:\n\n```swift\nlet json: JSON = [\n    \"num\": 1,\n    \"str\": \"baz\",\n    \"bool\": true,\n    \"obj\": [\n        \"foo\": \"jar\",\n        \"bar\": 1,\n    ]\n]\n```\n\nQuerying values can be done using optional property accessors, subscripting or dynamic member subscripting:\n\n```swift\n// Property accessors\nif let str = json.objectValue?[\"str\"]?.stringValue { … }\nif let foo = json.objectValue?[\"obj\"]?.objectValue?[\"foo\"]?.stringValue { … }\n\n// Subscripting\nif let str = json[\"str\"]?.stringValue { … }\nif let foo = json[\"obj\"]?[\"foo\"]?.stringValue { … }\n\n// Dynamic member subscripting\nif let str = json.str?.stringValue { … }\nif let foo = json.obj?.foo?.stringValue { … }\n```\n\nYou may even drill through nested structures using a dot-separated key path:\n\n```swift\nlet val = json[keyPath: \"obj.foo\"] // \"jar\"\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiwill%2Fgeneric-json-swift","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiwill%2Fgeneric-json-swift","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiwill%2Fgeneric-json-swift/lists"}