Codable - leacode/SwiftWings GitHub Wiki
Source: Sources/Extensions/Foundation/Codable/Codable+JSON.swift
Tests: Tests/Extensions/Codable
- Encodes
selfusingJSONEncoderand converts the data into a UTF-8 string. - Handy for debug logging and snapshotting lightweight payloads.
- Encodes
selfand immediately deserializes back into[String: Any]usingJSONSerialization. - Throws on encoding or JSON errors so you can bubble failures up to the caller.
struct User: Codable { let id: Int; let name: String }
let user = User(id: 1, name: "Ava")
print(user.jsonString!) // {"id":1,"name":"Ava"}
print(try user.dictionary() ?? [:]) // ["id": 1, "name": "Ava"]