Contents

kvngwxxk/lazycodablekit

πŸ“˜ [View this page in Korean](./README_KR.md)

πŸš€ Features

  • Decode Int, Double, String, and Bool from mixed or malformed formats
  • Use fallback values when decoding fails
  • Optional variants decode to nil on failure
  • Lightweight and dependency-free
  • Fully compatible with Swift Concurrency (async/await)

πŸ“’ Logging

By default, all logs are disabled. Enable detailed decoding logs like this:

import LazyCodableKit

// Turn on logging for all wrappers
LazyCodableLogger.isEnabled = true

// (Optional) Also log successful conversions (βœ…)
// LazyCodableLogger.logOnSuccess = true

// (Optional) Customize log output destination
LazyCodableLogger.handler = { message in
    // e.g. os_log, Crashlytics, write to file
    print(message)
}

When enabled, you’ll see entries like:

[LazyCodableKit] πŸ“user.age: πŸ”„ String("25") β†’ Int(25)
[LazyCodableKit] πŸ“user.score: ⚠️ Unknown value β†’ fallback to -1
[LazyCodableKit] πŸ“user.nickname: 🚫 JSON null β†’ nil
  • πŸ”„ converted: type coercion events
  • ⚠️ fallback: mismatches falling back to default
  • 🚫 null: JSON null β†’ nil
  • βœ… success: (only if logOnSuccess = true)

πŸ“¦ Installation

Swift Package Manager

Add to your Package.swift:

.package(url: "https://github.com/kvngwxxk/LazyCodableKit.git", from: "1.0.0")

Then import:

import LazyCodableKit

CocoaPods

In your Podfile:

pod 'LazyCodableKit', '~> 1.0.0'

Then:

pod install

πŸ› οΈ Usage

Promised (non-optional) decoding

struct User: Codable {
    @PromisedInt var age: Int
    @PromisedBool var isActive: Bool
    @PromisedString var nickname: String
    @PromisedDouble var rating: Double
    @PromisedDate var createdAt: Date    
}

Optional decoding (fails silently)

struct User: Codable {
    @PromisedOptionalInt var age: Int?
    @PromisedOptionalBool var isActive: Bool?
    @PromisedOptionalString var nickname: String?
    @PromisedOptionalDouble var rating: Double?
    @PromisedOptionalDate var createdAt: Date?
}

πŸ“‹ Supported Formats

| Wrapper | Accepts | Fallback (default) | Notes | |----------------------|---------------------------------------------------|--------------------|-------------------------| | @PromisedInt | Int, "123", 123.4, true | -1 | | | @PromisedBool | true, "yes", 1, "false" | false | | | @PromisedString | "str", 123, true | "" | | | @PromisedDouble | 123.45, "123", true | -1.0 | | | @PromisedDate | ISO8601, "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss" | Date.distantPast | Available since 1.1.0 | | @PromisedOptional* | Same as above, but returns nil on failure | nil | |

πŸ” Quick Example

Here's a full example that uses all Promised property wrappers with nested types and arrays:

struct Badge: Codable {
    @PromisedString var title: String
    @PromisedOptionalInt var level: Int?
}

struct Profile: Codable {
    @PromisedString var bio: String
    @PromisedOptionalBool var isVerified: Bool?
    @PromisedOptionalDate var birthday: Date?
}

struct User: Codable {
    @PromisedInt var id: Int
    @PromisedOptionalString var nickname: String?
    @PromisedBool var isActive: Bool
    @PromisedDouble var rating: Double
    @PromisedOptionalDouble var optionalScore: Double?
    @PromisedDate var createdAt: Date

    var profile: Profile
    var badges: [Badge]
}

let json = """
{
  "id": "1001",
  "nickname": 123,
  "isActive": "yes",
  "rating": "4.7",
  "optionalScore": null,
  "createdAt": "2023-11-20 12:34:56",
  "profile": {
    "bio": true,
    "isVerified": "no",
    "birthday": "2000-01-01"
  },
  "badges": [
    { "title": 456, "level": "3" },
    { "title": null, "level": {} }
  ]
}
""".data(using: .utf8)!

LazyCodableLogger.isEnabled = true
let user = try JSONDecoder().decode(User.self, from: json)

Console

[LazyCodableKit] πŸ“id: πŸ”„ String("1001") β†’ Int(1001)
[LazyCodableKit] πŸ“nickname: πŸ”„ Int(123) β†’ String("123")
[LazyCodableKit] πŸ“isActive: πŸ”„ String("yes") β†’ Bool(true)
[LazyCodableKit] πŸ“rating: πŸ”„ String("4.7") β†’ Double(4.7)
[LazyCodableKit] πŸ“optionalScore: 🚫 JSON null β†’ nil
[LazyCodableKit] πŸ“createdAt: πŸ”„ String("2023-11-20 12:34:56") β†’ Date(2023-11-20 12:34:56 +0000)
[LazyCodableKit] πŸ“profile.bio: πŸ”„ Bool(true) β†’ String("true")
[LazyCodableKit] πŸ“profile.isVerified: πŸ”„ String("no") β†’ Bool(false)
[LazyCodableKit] πŸ“profile.birthday: πŸ”„ String("2000-01-01") β†’ Date(2000-01-01 00:00:00 +0000)
[LazyCodableKit] πŸ“badges.Index 0.title: πŸ”„ Int(456) β†’ String("456")
[LazyCodableKit] πŸ“badges.Index 0.level: πŸ”„ String("3") β†’ Int(3)
[LazyCodableKit] πŸ“badges.Index 1.title: ⚠️ Unknown value β†’ fallback to ""
[LazyCodableKit] πŸ“badges.Index 1.level: 🚫 Unknown value β†’ nil

βœ… Minimum Requirements

  • iOS 13+
  • macOS 11+

πŸ“„ License

LazyCodableKit is released under the MIT License.

πŸ”— Contribution

Contributions, issues, and feature requests are welcome! Feel free to file an issue or submit a pull request.

Package Metadata

Repository: kvngwxxk/lazycodablekit

Default branch: master

README: README.md