swift-codable
Implement Swift Codable models for JSON and property-list encoding and decoding with JSONDecoder, JSONEncoder, CodingKeys, and custom init(from:) or encode(to:). Use when parsing API responses, remapping keys, flattening nested JSON, handling date or data decoding strategies, decoding heterogeneous arrays, or integrating Codable with URLSession, SwiftData, or UserDefaults.
Security Assessment
About swift-codable
The swift-codable skill covers encoding and decoding Swift types using Codable (Encodable and Decodable) together with JSONEncoder, JSONDecoder, and related property-list APIs, targeting Swift 6.3 and iOS 26+. It is used when parsing API responses, remapping keys, flattening nested JSON, handling date or data decoding strategies, decoding heterogeneous arrays, and integrating Codable with URLSession, SwiftData, or UserDefaults. The guidance is organized as a table of contents that moves from automatic conformance through increasingly custom scenarios.
When all stored properties are themselves Codable, the compiler synthesizes conformance automatically, and the skill advises preferring Decodable for read-only responses and Encodable for write-only, reserving Codable for cases needing both. It shows renaming JSON keys without a custom decoder via a CodingKeys enum (noting every stored property must appear, and omitting one excludes it from encoding/decoding), and overriding init(from:) and encode(to:) for transformations the synthesized conformance cannot handle, such as converting a Unix timestamp to a Date or defaulting a missing array via decodeIfPresent. It demonstrates navigating and flattening nested JSON with nestedContainer(keyedBy:forKey:) and nestedUnkeyedContainer(forKey:), and decoding heterogeneous arrays of mixed types using a discriminator field with a switch.
The skill details configuration strategies: dateDecodingStrategy options including .iso8601, .secondsSince1970, .formatted with a DateFormatter, and a .custom closure for multiple formats, with matching encoder settings; dataDecodingStrategy of .base64; and keyDecodingStrategy of .convertFromSnakeCase. It warns that the snake-case strategy maps by spelling rather than Swift acronym policy, so image_url maps to imageUrl but not imageURL, requiring explicit CodingKeys for names like imageURL, baseURI, or userID. Additional topics include a LossyArray wrapper to skip invalid elements rather than failing the whole array, single value containers, default values for missing keys, encoder/decoder configuration, and integration patterns with URLSession, SwiftData, and UserDefaults. It closes with common mistakes, a review checklist, and references.
FAQ
When does Swift synthesize Codable conformance automatically?
When all stored properties are themselves Codable, the compiler synthesizes conformance automatically, so you can decode and encode without writing custom code. Prefer Decodable for read-only responses and Encodable for write-only.
How do I rename JSON keys without writing a custom decoder?
Declare a CodingKeys enum that maps property names to JSON keys, for example case displayName = "display_name". Every stored property must appear in the enum; omitting one excludes it from encoding and decoding, so provide a default or compute it separately.
Can convertFromSnakeCase handle keys like image_url mapping to imageURL?
No. convertFromSnakeCase maps by spelling, not Swift acronym policy, so image_url, base_uri, and user_id map to imageUrl, baseUri, and userId only. For imageURL, baseURI, or userID you must declare explicit CodingKeys.
How do I decode an array of mixed types?
Use a discriminator field: read a 'type' key with a keyed container, then switch on its value to decode the appropriate associated data into an enum case, throwing DecodingError.dataCorruptedError for unknown types.
What date decoding strategies are supported?
JSONDecoder.dateDecodingStrategy supports .iso8601, .secondsSince1970, .formatted with a DateFormatter, and a .custom closure for handling multiple formats. Set the matching strategy on JSONEncoder, for example .iso8601.
Install swift-codable
Quick Setup:
- Copy the skill folder to
.claude/skills/ - Claude will automatically detect and use the skill
Repository
dpearson2699/swift-ios-skills