Swift Codable
Swift Codable teaches you to serialize and deserialize Swift types to and from JSON using the standard Codable protocol. Learn automatic synthesis, custom key mapping, nested container navigation, date and data strategies, and integration patterns with URLSession, SwiftData, and UserDefaults.
Swift Codable helps you implement JSON serialization and deserialization in Swift using Codable, JSONEncoder, and JSONDecoder.
AI-generated summary based on this skill's SKILL.md
Decision gist · record as of 2026-07-15
Swift Codable helps you implement JSON serialization and deserialization in Swift using Codable, JSONEncoder, and JSONDecoder. Swift Codable teaches you to serialize and deserialize Swift types to and from JSON using the standard Codable protocol. Learn automatic synthesis, custom key mapping, nested container navigation, date and data strategies, and integration patterns with URLSession, SwiftData, and UserDefaults.
Use it when
- Swift Codable best practices include: use `CodingKeys` enum for custom key mapping when JSON field names differ from your Swift properties.
- Swift Codable automatically handles nested structures and arrays through recursive synthesis.
Install
dpearson2699/swift-ios-skills/swift-codable · repository language: Python
generated, unverified - the skill's exact subdirectory could not be determined; check the repository on GitHub
Open directory. Skills are indexed for reading, not audited. Review a skill's body before installing it.
Frequently asked questions
AI-generated answers based on this skill's SKILL.md and metadata
How to use Codable in Swift for JSON encoding and decoding?
Swift Codable enables automatic serialization and deserialization of Swift types to and from JSON. To use it, declare your struct or class as conforming to Codable, and Swift synthesizes the required Encoder and Decoder implementations automatically. For example, a struct with String and Int properties marked `Codable` can be encoded to JSON via `JSONEncoder()` and decoded back using `JSONDecoder()`. This eliminates manual parsing boilerplate while maintaining type safety.
What are Swift Codable best practices for production code?
Swift Codable best practices include: use `CodingKeys` enum for custom key mapping when JSON field names differ from your Swift properties; leverage `DateDecodingStrategy` and `DataDecodingStrategy` to handle temporal and binary data; implement custom `init(from:)` and `encode(to:)` only when automatic synthesis is insufficient; validate decoded data in post-init logic; and handle decoding errors gracefully with try-catch blocks. Test edge cases like null values, missing fields, and type mismatches.
How does Swift Codable handle nested objects and arrays?
Swift Codable automatically handles nested structures and arrays through recursive synthesis. Nested types must also conform to Codable. Use `KeyedDecodingContainer` and `UnkeyedDecodingContainer` for custom navigation when needed. For arrays of Codable types, Swift synthesizes array encoding/decoding directly. When JSON structure doesn't match your model, define intermediate wrapper types or use custom `init(from:)` to manually navigate containers and reconstruct your target structure.
What is the Swift Codable protocol and what conformance requires?
Swift Codable is a protocol combining Encodable and Decodable, enabling bidirectional JSON transformation. Conformance requires implementing `encode(to:)` and `init(from:)` methods, though Swift synthesizes these automatically for structs and classes with all-Codable properties and no custom logic. Manual conformance is needed for types with computed properties, custom initialization, or non-standard JSON mappings. The protocol works with Encoder and Decoder implementations like JSONEncoder and JSONDecoder.
How do you solve custom encoding and decoding challenges with Codable?
Swift Codable custom encoding/decoding is handled by implementing `encode(to:)` and `init(from:)` manually. Use `KeyedEncodingContainer` and `KeyedDecodingContainer` to map properties to JSON keys. For non-standard formats, configure `JSONEncoder` and `JSONDecoder` strategies—`dateEncodingStrategy`, `dataEncodingStrategy`, `keyEncodingStrategy`. Handle type mismatches and missing fields by catching `DecodingError`. For complex transformations, create intermediate types or use custom container navigation to reshape data during encoding/decoding.
Can you map custom JSON keys to Swift properties using Codable?
Yes, Swift Codable supports custom key mapping via the `CodingKeys` enum. Define a nested enum conforming to `CodingKey` inside your type, listing each property with its corresponding JSON key as a string raw value. For example, a property `userName` can map to JSON key `user_name` by declaring `case userName = "user_name"` in CodingKeys. This allows your Swift code to follow camelCase conventions while JSON uses snake_case or other formats without manual parsing.
Let your AI agent find skills like this
Example. Real query, live index.
You found this page by searching. An agent finds it by wishing: SkillFed indexes 56,283 agent skills by what they can do, searchable in plain language.
wish › “Learn how to implement Codable for JSON encoding/decoding in Swift”
Give your agent the search over MCP, or paste the wish link into any chat. No install? Search from any chat →
Related skills
Swift Language covers contemporary syntax and idioms for core Swift development, excluding concurrency and SwiftUI. Learn if/switch expressions, typed throws, result builders, property wrappers, opaque versus existential types, guard patterns, the Never type, regex builders, Codable techniques, modern collection methods, FormatStyle basics, and string interpolation. Routes specialized topics like deep decoding, formatting, API naming, concurrency, and SwiftUI state to dedicated skills.
This skill teaches you to build robust network communication in iOS and macOS apps using URLSession with async/await and structured concurrency. You'll learn practical patterns for HTTP requests, REST API clients, file transfers, streaming, and handling network errors—plus architecture strategies like middleware, API client protocols, and testable designs.
Swift is a powerful, modern programming language designed for building iOS, macOS, and other Apple platform applications. This skill covers core language concepts, syntax patterns, and contemporary best practices to help you write efficient, type-safe code. Whether you're starting fresh or deepening your expertise, you'll gain practical knowledge for real-world development scenarios.
This skill equips you with advanced Swift development knowledge covering the latest 5.9+ language features and modern programming patterns. Explore expert techniques for building robust, performant applications while mastering contemporary best practices in the Swift ecosystem.
Core Data is Apple's object-oriented database framework for managing persistent storage in iOS applications. This skill covers the essential concepts, architecture, and implementation patterns you need to build robust data layers. Explore how to model entities, perform queries, and optimize performance in your apps.
This skill transforms technical specifications into detailed, actionable implementation roadmaps complete with pseudo-code scaffolding. Designed for Apple platform developers, it bridges the gap between planning and execution by breaking down complex requirements into manageable development steps. Perfect for iOS, macOS, watchOS, and visionOS projects.
More skills swift-rules (Apache-2.0) · swift-actor-persistence (MIT)