Skills DirectorySkills Directory
SkillsLearnSecurityCategoriesDocsCommunityBlog
Sign InSubmit Skill
Skills Directory

Security-tested agent skills for Claude, coding agents, and AI workflows.

Directory

  • Browse Skills
  • All Skills A–Z
  • Claude Skills
  • Claude Code Skills
  • Agent Skills
  • Categories
  • Authors
  • Submit a Skill

Learn

  • Learn Hub
  • Install Claude Skills
  • Write SKILL.md
  • Skills vs MCP
  • Directories Compared

Security

  • Security
  • Methodology
  • Secure Claude Skills
  • Security Badges

Company

  • About
  • Community
  • Blog
  • API Docs
  • Advertise

2026 Skills Directory. All rights reserved.

ProTermsPrivacyRefunds
Back to skills

Swift Codable

ASecurity

Master serialization and deserialization in Swift with Codable. Use when implementing custom CodingKeys, date/data decoding strategies, polymorphic/mixed collections, lossy array decoding, property wrappers for default values, or bridging Codable with SwiftData/CoreData persistence.

3 stars
0 votes
0 copies
0 views
Added 9/28/2026
developmentswiftdebuggingdocumentation

Security Analysis

A100/100

Scanned 9/28/2026

Install to Claude Code

$npx -y skills add thiennc-tesoglobal/ios-skills --skill swift-codable --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Swift Codable?

Add the live security badge to your README — it updates automatically with every re-scan.

Security grade badge for Swift Codable
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/thiennc-tesoglobal-swift-codable/badge)](https://www.skillsdirectory.com/skills/thiennc-tesoglobal-swift-codable)

More formats (shields.io, HTML) on the badges page.

Files
SKILL.md
---
name: swift-codable
description: "Master serialization and deserialization in Swift with Codable. Use when implementing custom CodingKeys, date/data decoding strategies, polymorphic/mixed collections, lossy array decoding, property wrappers for default values, or bridging Codable with SwiftData/CoreData persistence."
---

# Swift Codable

Encode and decode Swift types with `Codable` (`Encodable` & `Decodable`). Covers custom keys, date/data strategies, polymorphic decoding, default values, and persistence compatibility. Targets Swift 6.3 / iOS 26+.

## Contents

- [Basic Conformance & CodingKeys](#basic-conformance--codingkeys)
- [Decoder Strategies](#decoder-strategies)
- [Custom Encoding & Decoding](#custom-encoding--decoding)
- [Polymorphic & Lossy Decoding](#polymorphic--lossy-decoding)
- [SwiftData & Persistence Boundaries](#swiftdata--persistence-boundaries)
- [Common Mistakes](#common-mistakes)
- [Review Checklist](#review-checklist)
- [References](#references)

## Basic Conformance & CodingKeys

Map serialized JSON field names to Swift property names using `CodingKey`:

```swift
struct UserProfile: Codable {
    let id: UUID
    var displayName: String
    var emailAddress: String

    enum CodingKeys: String, CodingKey {
        case id
        case displayName = "display_name"
        case emailAddress = "email"
    }
}
```

## Decoder Strategies

Configure decoder strategies rather than writing manual decoding logic:

```swift
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
decoder.dateDecodingStrategy = .iso8601
decoder.dataDecodingStrategy = .base64

let profiles = try decoder.decode([UserProfile].self, from: jsonData)
```

> **Note:** `.convertFromSnakeCase` does not handle acronyms well (e.g. `server_id` becomes `serverId`, but `server_url` becomes `serverUrl` not `serverURL`). Use explicit `CodingKeys` for non-standard mappings.

## Custom Encoding & Decoding

When custom transformations are needed, implement `init(from:)` and `encode(to:)`:

```swift
struct BoundedScore: Codable {
    let score: Int

    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        let raw = try container.decode(Int.self)
        self.score = min(max(raw, 0), 100)
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        try container.encode(score)
    }
}
```

## Polymorphic & Lossy Decoding

- **Lossy Arrays**: Decode valid elements while skipping corrupted items using an unkeyed container try/catch loop.
- **Polymorphic Types**: Decode a discriminator key (e.g. `"type"`) from a parent container, then decode the corresponding concrete type.

See [references/codable-advanced-patterns.md](references/codable-advanced-patterns.md) for complete implementations.

## SwiftData & Persistence Boundaries

SwiftData `@Model` classes can store complex structures that conform to `Codable`. When persisting `Codable` structs:
- Keep data types stable across app updates.
- Provide default values for newly added properties to prevent schema migration crashes.

## Common Mistakes

- **Omitting properties in custom init(from:)**: A custom decoder initializer must initialize all stored properties or compilation fails.
- **Using convertFromSnakeCase with explicit CodingKeys**: The key decoding strategy converts keys before matching `CodingKeys`, leading to double-conversion mismatches.
- **Failing entire collections on one bad item**: In resilient feeds, decode elements lossily rather than throwing on the entire array.
- **Decoding floating point dates without explicit strategies**: Default date decoding expects seconds since 2001; specify `.iso8601` or `.millisecondsSince1970`.
- **Ignoring DecodingError diagnostics**: Inspect `DecodingError.keyNotFound`, `.typeMismatch`, and `.valueNotFound` details for exact debugging.

## Review Checklist

- [ ] Automatic synthesis preferred over manual `init(from:)` where possible
- [ ] Appropriate `dateDecodingStrategy` and `keyDecodingStrategy` configured on decoder
- [ ] Acronyms in snake_case checked for conversion issues
- [ ] Resilient network arrays handle individual element decoding errors
- [ ] Codable models intended for SwiftData maintain stable schema types

## References

- [Advanced Codable patterns](references/codable-advanced-patterns.md) -- mixed arrays, lossy decoding, wrappers, defaults, configuration, and persistence boundaries
- [Codable](https://sosumi.ai/documentation/swift/codable/) -- protocol combining Encodable and Decodable
- [JSONDecoder](https://sosumi.ai/documentation/foundation/jsondecoder/) -- decodes JSON data into Codable types
- [JSONEncoder](https://sosumi.ai/documentation/foundation/jsonencoder/) -- encodes Codable types as JSON data
- [CodingKey](https://sosumi.ai/documentation/swift/codingkey/) -- protocol for encoding/decoding keys
- [JSONDecoder.KeyDecodingStrategy.convertFromSnakeCase](https://sosumi.ai/documentation/foundation/jsondecoder/keydecodingstrategy-swift.enum/convertfromsnakecase) -- snake-case conversion behavior and limitations
- [Encoding and Decoding Custom Types](https://sosumi.ai/documentation/foundation/encoding-and-decoding-custom-types/) -- Apple guide on custom Codable conformance
- [Using JSON with Custom Types](https://sosumi.ai/documentation/foundation/using-json-with-custom-types) -- Apple sample code for JSON patterns
- [Preserving your app's model data across launches](https://sosumi.ai/documentation/swiftdata/preserving-your-apps-model-data-across-launches) -- SwiftData model property compatibility

Attribution

thiennc-tesoglobalthiennc-tesoglobal
View sourceMore from thiennc-tesoglobal →
SSkills DirectorySkills Directory

Ship a skill? Prove it's safe.

Free 120-pattern security scan, letter grade, and an embeddable README badge.

Submit a skill

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments (0)

No comments yet. Be the first to comment!

SSkills DirectorySkills Directory

Ship a skill? Prove it's safe.

Free 120-pattern security scan, letter grade, and an embeddable README badge.

Submit a skill

Related Skills

Browser Extension Developer

Use this skill when developing or maintaining browser extension code in the `browser/` directory, including Chrome/Firefox/Edge compatibility, content scripts, background scripts, or i18n updates.

284972 votes

Seo Optimizer

SEO optimization with keyword analysis, readability assessment, technical validation, content quality. Use for search rankings, blog posts, content audits, or encountering keyword density, readability scores, meta tags, schema markup errors.

2222 votes

Google Official Seo Guide

Official Google SEO guide covering search optimization, best practices, Search Console, crawling, indexing, and improving website search visibility based on official Google documentation

1862 votes

Tanstack Start

Build a full-stack TanStack Start app on Cloudflare Workers from scratch — SSR, file-based routing, server functions, D1+Drizzle, better-auth, Tailwind v4+shadcn/ui. Use whenever the user mentions TanStack Start, asks to scaffold a full-stack Cloudflare app with SSR, wants an SSR dashboard, or asks for a React 19 + Cloudflare Workers app with file-based routing and server functions — even if they don't name TanStack Start specifically. No template repo — Claude generates every file fresh per ...

10311 votes

Pentest

PTES-aligned adversarial security audit for backend, frontend, and mobile applications. Produces a CVSS-scored Hacker Report with verified PoCs and phased remediation.

5491 votes
View all in development →