{"id":19904648,"url":"https://github.com/nixzhu/ananda","last_synced_at":"2026-02-07T23:12:22.648Z","repository":{"id":65518763,"uuid":"587703270","full_name":"nixzhu/Ananda","owner":"nixzhu","description":"JSON model decoding based on yyjson.","archived":false,"fork":false,"pushed_at":"2024-09-30T03:48:08.000Z","size":3059,"stargazers_count":77,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-10-29T23:38:42.557Z","etag":null,"topics":["json","macro","swift"],"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/nixzhu.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-01-11T11:38:59.000Z","updated_at":"2024-09-30T03:48:12.000Z","dependencies_parsed_at":"2023-02-14T15:00:32.343Z","dependency_job_id":"638b34f5-a488-4df6-8388-12b0478d1728","html_url":"https://github.com/nixzhu/Ananda","commit_stats":{"total_commits":27,"total_committers":1,"mean_commits":27.0,"dds":0.0,"last_synced_commit":"3b92f695e0621a9b46897dc5d03510018a74057e"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nixzhu%2FAnanda","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nixzhu%2FAnanda/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nixzhu%2FAnanda/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nixzhu%2FAnanda/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nixzhu","download_url":"https://codeload.github.com/nixzhu/Ananda/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246837683,"owners_count":20841903,"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":["json","macro","swift"],"created_at":"2024-11-12T20:29:15.353Z","updated_at":"2026-02-07T23:12:22.642Z","avatar_url":"https://github.com/nixzhu.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fnixzhu%2FAnanda%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/nixzhu/Ananda) \n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fnixzhu%2FAnanda%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/nixzhu/Ananda)\n\n# Ananda\n\nJSON model decoding based on [yyjson](https://github.com/ibireme/yyjson).\n\n## Example\n\nConsider the following JSON:\n\n```json\n{\n    \"profile\": {\n        \"nickname\": \"NIX\",\n        \"username\": \"@nixzhu@mastodon.social\",\n        \"avatar\": {\n            \"url\": \"https://example.com/nixzhu.png\",\n            \"width\": 200,\n            \"height\": 200\n        }\n    },\n    \"toots\": [\n        {\n            \"id\": 1,\n            \"content\": \"Hello World!\",\n            \"created_at\": \"2024-10-05T09:41:00.789Z\"\n        },\n        {\n            \"id\": 2,\n            \"content\": \"How do you do?\",\n            \"created_at\": \"2025-04-29T22:23:24.567Z\"\n        }\n    ]\n}\n```\n\nWe can create models conforming to the `AnandaModel` protocol as follows:\n\n```swift\nimport Foundation\nimport Ananda\n\nstruct Mastodon: AnandaModel {\n    let profile: Profile\n    let toots: [Toot]\n\n    init(json: AnandaJSON) {\n        profile = .decode(from: json.profile)\n        toots = json.toots.array().map { .decode(from: $0) }\n    }\n}\n\nextension Mastodon {\n    struct Profile: AnandaModel {\n        let nickname: String\n        let username: String\n        let avatar: Avatar\n\n        init(json: AnandaJSON) {\n            nickname = json.nickname.string()\n            username = json.username.string()\n            avatar = .decode(from: json.avatar)\n        }\n    }\n}\n\nextension Mastodon.Profile {\n    struct Avatar: AnandaModel {\n        let url: URL\n        let width: Double\n        let height: Double\n\n        init(json: AnandaJSON) {\n            url = json[\"url\"].url()\n            width = json.width.double()\n            height = json.height.double()\n        }\n    }\n}\n\nextension Mastodon {\n    struct Toot: AnandaModel {\n        let id: Int\n        let content: String\n        let createdAt: Date\n\n        init(json: AnandaJSON) {\n            id = json.id.int()\n            content = json.content.string()\n            createdAt = json.created_at.date()\n        }\n    }\n}\n```\n\nTo decode a `Mastodon` instance from a JSON string:\n\n```swift\nlet mastodon = Mastodon.decode(from: jsonString)\n```\n\nOr, if you already have JSON data:\n\n```swift\nlet mastodon = Mastodon.decode(from: jsonData)\n```\n\nTo decode a specific JSON branch, for example `profile.avatar`, specify its path:\n\n```swift\nlet avatar = Mastodon.Profile.Avatar.decode(from: jsonData, path: [\"profile\", \"avatar\"])\n```\n\nTo decode an array (e.g., `toots`):\n\n```swift\nlet toots = [Mastodon.Toot].decode(from: jsonData, path: [\"toots\"])\n```\n\nOr decode only the first toot:\n\n```swift\nlet toot = Mastodon.Toot.decode(from: jsonData, path: [\"toots\", 0])\n```\n\n## Installation\n\n### Swift Package Manager\n\nThe [Swift Package Manager](https://swift.org/package-manager/) is a tool for automating the distribution of Swift code and is integrated into the `swift` compiler.\n\nOnce you have a Swift package set up, add Ananda as a dependency in your `Package.swift` file:\n\n```swift\n// In Package.swift\n\ndependencies: [\n    .package(url: \"https://github.com/nixzhu/Ananda.git\", from: \"1.2.0\"),\n]\n```\n\nTypically, you will want to depend on the `Ananda` product:\n\n```swift\n// In a target's dependencies\n\n.product(name: \"Ananda\", package: \"Ananda\")\n```\n\n## Swift Macro\n\nWith [AnandaMacros](https://github.com/nixzhu/AnandaMacros), you can use macros to eliminate the need for initialization methods, as shown below:\n\n```swift\nimport Foundation\nimport Ananda\nimport AnandaMacros\n\n@AnandaInit\nstruct Mastodon: AnandaModel {\n    let profile: Profile\n    let toots: [Toot]\n}\n\nextension Mastodon {\n    @AnandaInit\n    struct Profile: AnandaModel {\n        let nickname: String\n        let username: String\n        let avatar: Avatar\n    }\n}\n\nextension Mastodon.Profile {\n    @AnandaInit\n    struct Avatar: AnandaModel {\n        let url: URL\n        let width: Double\n        let height: Double\n    }\n}\n\nextension Mastodon {\n    @AnandaInit\n    struct Toot: AnandaModel {\n        let id: Int\n        let content: String\n        @AnandaKey(\"created_at\")\n        let createdAt: Date\n    }\n}\n```\n\nSimple and clean, right?\n\n## Benchmark\n\nSee [AnandaBenchmark](https://github.com/nixzhu/AnandaBenchmark).\n\n## Tool\n\nYou can use [Ducky Model Editor](https://apps.apple.com/us/app/ducky-model-editor/id1525505933) to generate AnandaModel from JSON, saving you time.\n\n![Ducky Model Editor](https://raw.githubusercontent.com/nixzhu/Ananda/main/images/ducky-ananda.png)\n\n![Ducky Model Editor](https://raw.githubusercontent.com/nixzhu/Ananda/main/images/ducky-ananda-macros.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnixzhu%2Fananda","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnixzhu%2Fananda","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnixzhu%2Fananda/lists"}