|
4 | 4 | <h1 align="center">SmartCodable - Resilient & Flexible Codable for Swift </h1> |
5 | 5 |
|
6 | 6 | <p align="center"> |
7 | | -<a href="https://github.com/iAmMccc/SmartCodable/blob/main/README_CN.md"> |
8 | | - <img src="https://img.shields.io/badge/中文-可用-brightgreen.svg" alt="中文文档"> |
9 | | -</a> |
10 | 7 | <a href="https://github.com/iAmMccc/SmartCodable/releases"> |
11 | 8 | <img src="https://img.shields.io/github/v/release/iAmMccc/SmartCodable?color=blue&label=version" alt="Latest Release"> |
12 | 9 | </a> |
|
27 | 24 | </a> |
28 | 25 | </p> |
29 | 26 |
|
| 27 | + |
30 | 28 | **SmartCodable** redefines Swift data parsing by enhancing Apple's native Codable with production-ready resilience and flexibility. It provides seamless support for default values, nested flattening, and ignored properties, reducing boilerplate while increasing reliability. |
31 | 29 |
|
32 | 30 | ## Features |
@@ -441,6 +439,46 @@ print(model) |
441 | 439 |
|
442 | 440 |
|
443 | 441 |
|
| 442 | +#### 3.6 @SmartCompact |
| 443 | + |
| 444 | +Adds Codable support for arrays and dictionaries with tolerant decoding. |
| 445 | + |
| 446 | +- **@SmartCompact.Array** |
| 447 | + When decoding an array, any element that cannot be decoded to the target element type will be skipped instead of failing the whole decode. |
| 448 | +- **@SmartCompact.Dictionary** |
| 449 | + When decoding a dictionary, any key-value pair that cannot be decoded will be skipped instead of failing the whole decode. |
| 450 | + |
| 451 | +```Swift |
| 452 | +struct Model: Decodable { |
| 453 | + // Array may contain invalid values, those will be ignored |
| 454 | + @SmartCompact.Array |
| 455 | + var ages: [Int] |
| 456 | + |
| 457 | + // Dictionary may contain invalid entries, those will be ignored |
| 458 | + @SmartCompact.Dictionary |
| 459 | + var info: [String: String] |
| 460 | +} |
| 461 | + |
| 462 | +let dict: [String: Any] = [ |
| 463 | + "ages": ["Tom", 1, [:], 2, 3, "4"], |
| 464 | + "info": [ |
| 465 | + "name": "Tom", |
| 466 | + "age": 18, |
| 467 | + "extra": [:] |
| 468 | + ] |
| 469 | +] |
| 470 | + |
| 471 | +let model = try! JSONDecoder().decode(Model.self, from: JSONSerialization.data(withJSONObject: dict)) |
| 472 | +print(model) |
| 473 | +// print: Model(ages: [1, 2, 3, 4], info: ["name": "Tom", "age": "18"]) |
| 474 | +``` |
| 475 | + |
| 476 | + |
| 477 | + |
| 478 | + |
| 479 | + |
| 480 | + |
| 481 | + |
444 | 482 | ### 4. Inheritance Support |
445 | 483 |
|
446 | 484 | This feature relies on **Swift Macros**, which requires **Swift 5.9+** and is compatible with **iOS 13+**. Therefore, it is only supported in SmartCodable version 5.0 and above. |
|
0 commit comments