{"id":17648758,"url":"https://github.com/chris-swift-dev/JSON-DSL","last_synced_at":"2025-03-30T07:42:26.445Z","repository":{"id":63907251,"uuid":"228914950","full_name":"chris-swift-dev/JSON-DSL","owner":"chris-swift-dev","description":"A simple JSON DSL for Swift","archived":false,"fork":false,"pushed_at":"2020-02-21T10:04:00.000Z","size":16,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-30T07:42:22.143Z","etag":null,"topics":["dsl","json","swift","swift-package"],"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/chris-swift-dev.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":"2019-12-18T20:18:05.000Z","updated_at":"2025-02-23T14:04:13.000Z","dependencies_parsed_at":"2022-11-28T22:52:27.209Z","dependency_job_id":null,"html_url":"https://github.com/chris-swift-dev/JSON-DSL","commit_stats":null,"previous_names":["chris-swift-dev/json-dsl","crelies/json-dsl"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chris-swift-dev%2FJSON-DSL","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chris-swift-dev%2FJSON-DSL/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chris-swift-dev%2FJSON-DSL/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chris-swift-dev%2FJSON-DSL/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chris-swift-dev","download_url":"https://codeload.github.com/chris-swift-dev/JSON-DSL/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246290583,"owners_count":20753724,"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":["dsl","json","swift","swift-package"],"created_at":"2024-10-23T11:20:23.078Z","updated_at":"2025-03-30T07:42:26.397Z","avatar_url":"https://github.com/chris-swift-dev.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JSON-DSL\n\n[![Swift 5.1](https://img.shields.io/badge/swift5.1-compatible-green.svg?longCache=true\u0026style=flat-square)](https://developer.apple.com/swift)\n[![Platform](https://img.shields.io/badge/platform-independent-lightgrey.svg?longCache=true\u0026style=flat-square)](https://www.apple.com)\n[![Current Version](https://img.shields.io/github/v/tag/crelies/JSON-DSL?longCache=true\u0026style=flat-square)](https://github.com/crelies/JSON-DSL)\n[![License](https://img.shields.io/badge/license-MIT-lightgrey.svg?longCache=true\u0026style=flat-square)](https://en.wikipedia.org/wiki/MIT_License)\n\nA simple domain-specific-language (**DSL**) for writing *type-safe* **JSON** in **Swift**\n\n## Motivation 🚀\n\nI wanted to implement a simple DSL in Swift 🙂\n\n## Result 🎁\n\nThe result is a very *lightweight* **API**:\n\n```swift\nlet json = JSON(\n    .array(\n        // 🚀 parses JSON encoded string and injects JSON elements\n        .json(\"[\\\"Developer\\\",{\\\"name\\\":\\\"crelies\\\",\\\"language\\\":\\\"swift\\\",\\\"iOS versions\\\":[8.0,9.0,10.0,11.0,12.0,13.0],\\\"projects\\\":{\\\"name\\\":\\\"AdvancedList\\\",\\\"developers\\\":[{\\\"name\\\":\\\"crelies\\\"}]}},\\\"Age\\\",29,\\\"Swift\\\",5.1,{\\\"isActive\\\":true},null]\"),\n\n        \"Developer\",\n        .object(.property(\"name\", \"crelies\"),\n                .property(\"language\", \"swift\"),\n                .property(\"iOS versions\", .array(8.0, 9.0, 10.0, 11.0, 12.0, 13.0)),\n                .property(\"projects\", .object(.property(\"name\", \"AdvancedList\"),\n                                              .property(\"developers\", .array(.object(.property(\"name\", \"crelies\"))))))\n        ),\n        \"Age\",\n        29,\n        \"Swift\",\n        5.1,\n        .object(.property(\"isActive\", true)),\n        nil\n    )\n)\n\nlet renderedString = json.render()\n```\n\n**Output:**\n\n```json\n[\n  [\n    \"Developer\",\n    {\n      \"name\": \"crelies\",\n      \"language\": \"swift\",\n      \"iOS versions\": [\n        8.0,\n        9.0,\n        10.0,\n        11.0,\n        12.0,\n        13.0\n      ],\n      \"projects\": {\n        \"name\": \"AdvancedList\",\n        \"developers\": [\n          {\n            \"name\": \"crelies\"\n          }\n        ]\n      }\n    },\n    \"Age\",\n    29,\n    \"Swift\",\n    5.1,\n    {\n      \"isActive\": true\n    },\n    null\n  ],\n  \"Developer\",\n  {\n    \"name\": \"crelies\",\n    \"language\": \"swift\",\n    \"iOS versions\": [\n      8.0,\n      9.0,\n      10.0,\n      11.0,\n      12.0,\n      13.0\n    ],\n    \"projects\": {\n      \"name\": \"AdvancedList\",\n      \"developers\": [\n        {\n          \"name\": \"crelies\"\n        }\n      ]\n    }\n  },\n  \"Age\",\n  29,\n  \"Swift\",\n  5.1,\n  {\n    \"isActive\": true\n  },\n  null\n]\n```\n\n**⚠️ Caution: *If you use `.json()` you could end up with invalid JSON. You need to know if a JSON encoded string is allowed in a specific hierarchy (example: properties are not allowed in an array)!***\n\n### Advanced example 🔬\n\nWhen it comes to `Strings` it's getting complicated 💣\n\nYou can use\n\n- *escaped JSON strings* through `.json()` (*decodes string and injects JSON elements 🚀*) or\n\n- *raw strings* through `.raw(\"Test\")` or `\"Test\"` (`\"Test\"` is shorthand syntax for `.raw(\"Test\")`).\n\n**Hint: *Keep in mind that you could get invalid JSON if you use an unescaped JSON string (see ❌ examples below).***\n\n**Examples:**\n\n```swift\nlet json = JSON(\n    .array(\n        // ✅ parses JSON string and injects JSON elements\n        .json(\"[\\\"Developer\\\",{\\\"name\\\":\\\"crelies\\\",\\\"language\\\":\\\"swift\\\",\\\"iOS versions\\\":[8.0,9.0,10.0,11.0,12.0,13.0],\\\"projects\\\":{\\\"name\\\":\\\"AdvancedList\\\",\\\"developers\\\":[{\\\"name\\\":\\\"crelies\\\"}]}},\\\"Age\\\",29,\\\"Swift\\\",5.1,{\\\"isActive\\\":true},null]\"),\n\n        // ########################################################################################\n        // ✅ raw strings, surrounding double quotes will be escaped\n\n        // - 1. Escaped characters\n        #\"[\\\"Developer\\\",{\\\"name\\\":\\\"crelies\\\",\\\"language\\\":\\\"swift\\\",\\\"iOS versions\\\":[8.0,9.0,10.0,11.0,12.0,13.0],\\\"projects\\\":{\\\"name\\\":\\\"AdvancedList\\\",\\\"developers\\\":[{\\\"name\\\":\\\"crelies\\\"}]}},\\\"Age\\\",29,\\\"Swift\\\",5.1,{\\\"isActive\\\":true},null]\"#,\n\n        // - 2. No escaped characters\n        #\"Without escaped characters\"#,\n        // ########################################################################################\n\n        // ✅ plain string\n        \"Without escaped characters\",\n\n        // ########################################################################################\n        /*\n            Not working because surrounding double quotes are not escaped.\n            (conflict with required JSON double quotes)\n            ---\u003e Will result in invalid JSON 💣\n        */\n\n        // ❌\n        // \"[\\\"Developer\\\",{\\\"name\\\":\\\"crelies\\\",\\\"language\\\":\\\"swift\\\",\\\"iOS versions\\\":[8.0,9.0,10.0,11.0,12.0,13.0],\\\"projects\\\":{\\\"name\\\":\\\"AdvancedList\\\",\\\"developers\\\":[{\\\"name\\\":\\\"crelies\\\"}]}},\\\"Age\\\",29,\\\"Swift\\\",5.1,{\\\"isActive\\\":true},null]\"\n        // ########################################################################################\n    )\n)\n```\n\n**Output:**\n\n```json\n[\n  [\n    \"Developer\",\n    {\n      \"name\": \"crelies\",\n      \"language\": \"swift\",\n      \"iOS versions\": [\n        8.0,\n        9.0,\n        10.0,\n        11.0,\n        12.0,\n        13.0\n      ],\n      \"projects\": {\n        \"name\": \"AdvancedList\",\n        \"developers\": [\n          {\n            \"name\": \"crelies\"\n          }\n        ]\n      }\n    },\n    \"Age\",\n    29,\n    \"Swift\",\n    5.1,\n    {\n      \"isActive\": true\n    },\n    null\n  ],\n  \"[\\\"Developer\\\",{\\\"name\\\":\\\"crelies\\\",\\\"language\\\":\\\"swift\\\",\\\"iOS versions\\\":[8.0,9.0,10.0,11.0,12.0,13.0],\\\"projects\\\":{\\\"name\\\":\\\"AdvancedList\\\",\\\"developers\\\":[{\\\"name\\\":\\\"crelies\\\"}]}},\\\"Age\\\",29,\\\"Swift\\\",5.1,{\\\"isActive\\\":true},null]\",\n  \"Without escaped characters\",\n  \"Without escaped characters\"\n]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchris-swift-dev%2FJSON-DSL","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchris-swift-dev%2FJSON-DSL","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchris-swift-dev%2FJSON-DSL/lists"}