kvngwxxk/lazycodablekit
π [View this page in Korean](./README_KR.md)
π Features
- Decode
Int,Double,String, andBoolfrom mixed or malformed formats - Use fallback values when decoding fails
- Optional variants decode to
nilon 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 LazyCodableKitCocoaPods
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