Codable - leacode/SwiftWings GitHub Wiki

Codable Extensions

Source: Sources/Extensions/Foundation/Codable/Codable+JSON.swift

Tests: Tests/Extensions/Codable

Encodable.jsonString

  • Encodes self using JSONEncoder and converts the data into a UTF-8 string.
  • Handy for debug logging and snapshotting lightweight payloads.

Encodable.dictionary()

  • Encodes self and immediately deserializes back into [String: Any] using JSONSerialization.
  • Throws on encoding or JSON errors so you can bubble failures up to the caller.

Example

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"]
⚠️ **GitHub.com Fallback** ⚠️