{"id":18333477,"url":"https://github.com/sochalewski/easycodable","last_synced_at":"2025-12-11T22:59:07.027Z","repository":{"id":63920543,"uuid":"512176487","full_name":"sochalewski/EasyCodable","owner":"sochalewski","description":"Easy failable properties in Swift Codable.","archived":false,"fork":false,"pushed_at":"2023-11-09T00:43:12.000Z","size":16,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-21T16:58:26.642Z","etag":null,"topics":["codable","json","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/sochalewski.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}},"created_at":"2022-07-09T12:10:11.000Z","updated_at":"2023-11-08T23:06:06.000Z","dependencies_parsed_at":"2023-12-08T16:09:42.698Z","dependency_job_id":null,"html_url":"https://github.com/sochalewski/EasyCodable","commit_stats":{"total_commits":3,"total_committers":1,"mean_commits":3.0,"dds":0.0,"last_synced_commit":"1a3a1de78b0ddc2c23e4f94bf706411ccc64fdef"},"previous_names":["sochalewski/easycodable","sochalewski/codablenilonfail"],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sochalewski%2FEasyCodable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sochalewski%2FEasyCodable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sochalewski%2FEasyCodable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sochalewski%2FEasyCodable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sochalewski","download_url":"https://codeload.github.com/sochalewski/EasyCodable/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247430838,"owners_count":20937873,"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","swift"],"created_at":"2024-11-05T19:42:31.545Z","updated_at":"2025-12-11T22:59:07.001Z","avatar_url":"https://github.com/sochalewski.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# EasyCodable\n\nEasy failable optional enum properties in Swift Codable.\n\n## Installation\n\nYou can add EasyCodable to an Xcode project by adding it as a package dependency.\n\n  1. From the **File** menu, select **Add Packages…**\n  2. Enter `https://github.com/sochalewski/EasyCodable` into the package repository URL text field.\n  3. Add the package to your app target.\n\n## Why?\n\nConsider the following data model:\n\n```swift\nstruct Car: Codable {\n    enum Manufacturer: String, Codable {\n        case toyota, volkswagen, ford, honda, generalMotors\n    }\n    \n    var manufacturer: Manufacturer?\n    var vin: String?\n    var owner: String?\n}\n```\n\nEverything works perfectly if the received JSON matches the available `Manufacturer` enum values, like this:\n\n```json\n{\n    \"manufacturer\": \"toyota\",\n    \"vin\": \"JT4RN67S0G0002845\",\n    \"owner\": null\n}\n```\n\nBut `Car.manufacturer` is optional, so it makes sense to have a valid model, even if the received manufacturer doesn't match one of the currently supported ones:\n\n```json\n{\n    \"manufacturer\": \"tesla\",\n    \"vin\": \"5YJSA2DP8DFP22249\",\n    \"owner\": \"Elon Musk\"\n}\n```\n\nUnfortunately decoding this with `JSONDecoder` results in getting `nil` for the whole `Car` instead of just `Car.manufacturer`.\n\nThis can be solved by custom `init(from:)` of the `Decodable` protocol, but that's a lot of boilerplate, especially when you have dozens of enums in your models.\n\nThis is where EasyCodable comes in!\n\n## Usage\n\nJust add the `@EasyNil` property wrapper to your optional `RawRepresentable` properties.\n\n```swift\nstruct Car: Codable, Equatable {\n    enum Manufacturer: String, Codable {\n        case toyota, volkswagen, ford, honda, generalMotors\n    }\n    \n    @EasyNil var manufacturer: Manufacturer?\n    var vin: String?\n    var owner: String?\n}\n```\n\n## Author\n\nPiotr Sochalewski, \u003ca href=\"http://sochalewski.github.io\"\u003esochalewski.github.io\u003c/a\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsochalewski%2Feasycodable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsochalewski%2Feasycodable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsochalewski%2Feasycodable/lists"}