{"id":16699991,"url":"https://github.com/dscyrescotti/codablex","last_synced_at":"2025-06-24T09:33:06.774Z","repository":{"id":48510479,"uuid":"315204325","full_name":"dscyrescotti/CodableX","owner":"dscyrescotti","description":"Make Swift's Codable easier using Property Wrappers.","archived":false,"fork":false,"pushed_at":"2021-10-30T04:54:07.000Z","size":245,"stargazers_count":13,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-02T19:05:19.135Z","etag":null,"topics":["codable","json-parsing","propertywrapper","spm","swift","swift-codable","swiftpackagemanager"],"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/dscyrescotti.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-11-23T04:56:02.000Z","updated_at":"2022-03-16T03:38:56.000Z","dependencies_parsed_at":"2022-08-30T14:50:51.685Z","dependency_job_id":null,"html_url":"https://github.com/dscyrescotti/CodableX","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dscyrescotti%2FCodableX","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dscyrescotti%2FCodableX/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dscyrescotti%2FCodableX/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dscyrescotti%2FCodableX/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dscyrescotti","download_url":"https://codeload.github.com/dscyrescotti/CodableX/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239132545,"owners_count":19587106,"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-parsing","propertywrapper","spm","swift","swift-codable","swiftpackagemanager"],"created_at":"2024-10-12T18:08:49.149Z","updated_at":"2025-02-16T12:30:56.847Z","avatar_url":"https://github.com/dscyrescotti.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp  align=\"center\"\u003e\n\u003cimg  width=\"60%\"  src=\"https://github.com/dscyrescotti/CodableX/blob/main/Assets/CodableX.png?raw=true\"\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://swiftpackageindex.com/dscyrescotti/CodableX\"\u003e\n        \u003cimg src=\"https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fdscyrescotti%2FCodableX%2Fbadge%3Ftype%3Dplatforms\" alt=\"Platform Compatibility\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://swiftpackageindex.com/dscyrescotti/CodableX\"\u003e\n        \u003cimg src=\"https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fdscyrescotti%2FCodableX%2Fbadge%3Ftype%3Dswift-versions\" alt=\"Swift Compatibility\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://github.com/dscyrescotti/CodableX/actions/workflows/swift.yml\"\u003e\n     \t\u003cimg src=\"https://github.com/dscyrescotti/CodableX/actions/workflows/swift.yml/badge.svg\" alt=\"Action Status\"/\u003e\n    \u003c/a\u003e\n    \u003ca href=\"LICENSE\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/license-MIT-brightgreen.svg\" alt=\"MIT License\"\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n__CodableX__ provides the property wrappers that are pretty handy to decode and encode structs or classes as you desire without implementing your custom codable structs or classes from scratch.\n\n## Installation\n\n### Swift Package Manager\nAdd it as a dependency within your `Package.swift`,\n```swift\ndependencies: [\n    .package(url: \"https://github.com/dscyrescotti/CodableX.git\", from: \"0.2.0\")\n]\n```\n\n### CocoaPods\nInside your Podfile,\n```ruby\npod 'CodableX', :git =\u003e 'https://github.com/dscyrescotti/CodableX.git'\n```\n\nCurrently, __CodableX__ can be installed via __Swift Package Manager__ and __CocoaPods__.\n\n## @AnyValuable\n`@AnyValuable` is pretty similar to the original `@Anyable` property wrapper. It wraps decoded value inside `AnyValue` which can hold the type that conforms to `AnyCodable` and also provide easier access to value than `Any`. \n\n### Usage\nUsing the default options of __CodableX__.\n\n```swift\nstruct  AnyValuableExample: Codable {\n    @AnyValuable\u003cDefaultOptions\u003e var value: AnyValue  // Int, String, Bool or Double inside AnyValue\n}\n\nlet data = #\"{ \"value\": 1 }\"#.data(using: .utf8)!\n\nlet decoded = try  JSONDecoder().decode(AnyValuableExample.self, from: data)\n\nprint(decoded) // AnyValuableExample(value: AnyValue(value: 1))\n```\n\n___Note: `DefaultOptions` only supports for `Int`, `String`, `Bool` and `Double`.___\n\n### Accessing the value inside `AnyValue`\n\n```swift\n// You can directly access data via value\nprint(decoded.value) // 1\n```\nOr\n```swift\n// You can access data via type casting. It's helpful for optional unwrapping and is also clear to read.\nprint(decoded.value.int) // Optional(1)\n```\n\n___Note: `AnyValue` already has type casting for Swift built-in types. For your custom types, you can extend `AnyValue` to declare them.___\n  \n\nUsing the custom options.\n\n```swift\nstruct  Custom: AnyCodable {\n    let value: String\n}\n\n// For type casting\nextension AnyValue {\n\tvar custom: Custom? {\n\t\tvalue as? Custom\n\t}\n}\n\n\nstruct  CustomOptions: OptionConfigurable {\n    static  var options: [Option] = [\n        .init(Int.self),\n        .init(Custom.self),\n        // add more\n    ]\n}\n\nstruct  AnyValuableExample: Codable {\n    @AnyValuable\u003cCustomOptions\u003e var value: AnyValue  // Int, Custom or types you specify inside AnyValue\n}\n```\n\n___Note: All the options of structs or classes must conform to `AnyCodable`.___\n\nFor the array of `AnyValuable` and the optional `AnyValuable`, you can use __`@ArrayAnyValuable`__ and __`@OptionalAnyValuable`__.\n  \n\n## @Anyable\n\n`@Anyable` is designed to decode and encode any value that matches one of the types that you pre-configure. It is very handy when the value of API response will be sure one of the values that API sends.\n\n  \n\n### Usage\n\nUsing the default options of __CodableX__.\n\n```swift\nstruct  AnyableExample: Codable {\n    @Anyable\u003cDefaultOptions\u003e var value: Any  // Int, String, Bool or Double\n}\n\nlet data = #\"{ \"value\": 1 }\"#.data(using: .utf8)!\n\nlet decoded = try  JSONDecoder().decode(AnyableExample.self, from: data)\n\nprint(decoded) // AnyableExample(value: 1)\n```\n\n___Note: `DefaultOptions` only supports for `Int`, `String`, `Bool` and `Double`.___\n\n  \n\nUsing the custom options.\n\n```swift\nstruct  Custom: AnyCodable {\n    let value: String\n}\n\nstruct  CustomOptions: OptionConfigurable {\n    static  var options: [Option] = [\n        .init(Int.self),\n        .init(Custom.self),\n        // add more\n    ]\n}\n\nstruct  AnyableExample: Codable {\n    @Anyable\u003cCustomOptions\u003e var value: Any  // Int, Custom or types you specify\n}\n```\n___Note: All the options of structs or classes must conform to `AnyCodable`.___\n\nFor the array of `Anyable` and the optional `Anyable`, you can use __`@ArrayAnyable`__ and __`@OptionalAnyable`__.\n\n## @Forcable\nAll credits to __[BetterCodable](https://github.com/marksands/BetterCodable)__.\n\n`@Forcable` is useful to force the value to be the specific type that you set when it decodes.\n\n### Usage\n```swift\nstruct  ForceValue: Codable {\n    @Forcable\u003cBool, DefaultOptions\u003e var value: Bool\n}\n\nlet data = #\"{ \"value\": \"true\" }\"#.data(using: .utf8)!\n\nlet decoded = try  JSONDecoder().decode(ForceValue.self, from: data)\n\nprint(decoded) // ForceValue(value: true)\n```\n\nIt allows you to customize the list of options just like `@Anyable`. It will find the type that match the data from API response from your list and then force to a specific type that you want.\n\nFor the array of `Forcable` and the optional `Forcable`, you can use __`@ArrayForcable`__ and __`@OptionalForcable`__.\n\n## @Nullable\n\n`@Nullable` serves as the traditional `Optional` (aka ?) of `Swift`. When encoding, it is able to encode `nil` as `null` in JSON.\n\n### Usage\n```swift\nstruct  NullValue: Codable {\n    @Nullable  var value: Int?\n}\n\nlet data = #\"{ \"value\": null }\"#.data(using: .utf8)!\n\nlet decoded = try  JSONDecoder().decode(NullValue.self, from: data)\n\nprint(decoded) // NullValue(value: nil)\n```\n\n## @Defaultable\n`@Defaultable` provides the default value when the coding key is not found or the value is missing.\n\n### Usage\nFor `Swift` built-in types, it will use the default `init()` method. For your custom structs or classes, you must make them conform to `DefaultCodable` and set the default value.\n\n```swift\nstruct  DefaultValue: Codable {\n    @Defaultable  var value: String\n}\n\nlet data = #\"{ \"value\": null }\"#.data(using: .utf8)!\n\nlet decoded = try  JSONDecoder().decode(DefaultValue.self, from: data)\n\nprint(decoded) // DefaultValue(value: \"\")\n```\n\nIf you want different default values of the same struct or class, or you need the custom default value for built-in types, __`@CustomDefaultable`__ will solve it.\n\n```swift\nstruct  CustomDefault: DefaultConfigurable {\n    static  var defaultValue: String = \"dope\"\n}\n\nstruct  DefaultValue: Codable {\n    @CustomDefaultable\u003cString, CustomDefault\u003e var value: String\n}\n\n```\n\n## @Compactable\n\n`@Compactable` is designed to decode the array of optional values and store values that are not null. Its name comes from `compactMap(_:)` of Swift because it removes null and invalid values from array.\n\n### Usage\n```swift\nstruct  CompactValue: Codable {\n    @Compactable  var array: [Int]\n}\n```\n\n## @Jsonable\n\n`@Jsonable` is handy to decode data into JSON object structure using dictionary of `Swift`. Literally, it works like `JSON.parse()` in `JavaScript`.\n\n### Usage\n```swift\nstruct  JsonValue: Codable {\n    @Jsonable  var json: Any\n}\n\n```\n\n## Author\n\n__Dscyre Scotti__ (__[@dscyrescotti](https://twitter.com/dscyrescotti)__)\n\n## Credits\n\n__CodableX__ is inspired by __[BetterCodable](https://github.com/marksands/BetterCodable)__ and __[AnyCodable](https://github.com/levantAJ/AnyCodable)__.\n\n## Contributions\n\n__CodableX__ welcomes all developers to contribute if you have any idea to improve and open an issue if you find any kind of bug.\n\n## License\n\nCodableX is available under the MIT license. See the [LICENSE](LICENSE) file for more info.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdscyrescotti%2Fcodablex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdscyrescotti%2Fcodablex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdscyrescotti%2Fcodablex/lists"}