Swift Codable and JSON
Decode a fixed document, then validate the resulting domain value.
How it works
Codable combines encoding and decoding contracts. Synthesised decoding checks representation types, but it does not automatically apply business rules from an unrelated custom initializer. Decode a raw transfer record and explicitly validate its fields before returning accepted data.
Run this Swift example
Install the toolchain for your platform from Swift.org, then check swift --version. The reviewed portable baseline is Swift 6.4.2 in Swift 6 mode on Ubuntu 26.04 x86_64 through the PC’s existing WSL installation. These are real Linux executions, not claims of Windows-native or Apple-platform test results. Windows Swift has separate C++/SDK prerequisites in its official setup guide.
Save the complete source as codable-json.swift in a new learning folder. On Linux/macOS compile with swiftc -swift-version 6 -parse-as-library codable-json.swift -o lesson, then run ./lesson. On a configured Windows toolchain use an output such as lesson.exe and run .\lesson.exe. The parse-as-library option supports the explicit @main entry point used by these sources. Compile each ordinary lesson separately.
Bhylu has no Swift browser runner. The JavaScript/Python editors do not run these sources; Apache, PHP and MySQL are not required. Downloads are source text, never executable teaching endpoints.
Download the complete codable-json.swift · Download all source files with hash checks
For the optional collection, save download-examples.mjs in a new folder and run node download-examples.mjs. A local preview uses node download-examples.mjs http://127.0.0.1:8000/examples/swift/. It refuses an existing bhylu-swift-examples folder and installs or executes no Swift automatically. Apple-only files remain separate Xcode learning examples, not a portable Swift package target.
Example
import Foundation
struct RawBooking: Codable { let id: String; let minutes: Int }
enum DecodeFailure: Error { case invalid }
func decodeBooking(_ data: Data) throws -> RawBooking {
guard data.count <= 1024 else { throw DecodeFailure.invalid }
let value = try JSONDecoder().decode(RawBooking.self, from: data)
guard !value.id.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty,
(1...240).contains(value.minutes) else { throw DecodeFailure.invalid }
return value
}
@main
struct Lesson {
static func main() {
for raw in [#"{"id":"P-31","minutes":45}"#, #"{"id":"P-31","minutes":0}"#, #"{"id":"","minutes":30}"#] {
do { let value = try decodeBooking(Data(raw.utf8)); print(value.id, value.minutes) }
catch { print("refused") }
}
}
}
Expected result
P-31 45 refused refused
The first fixture becomes a checked value. Zero minutes and a blank ID are refused after syntax decoding. The standard decoder can ignore unknown keys and does not establish a strict duplicate-key policy; those are separate API requirements.
Primary references: The Swift Programming Language and Apple framework documentation. Use the selected platform’s current requirements. Fictional workshop examples do not establish production safety or deployment approval.