Skip to content

Commit c09e94d

Browse files
committed
SmartCompact
1 parent 2bdf3a1 commit c09e94d

11 files changed

Lines changed: 207 additions & 870 deletions

File tree

Example/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ PODS:
88
- FBSnapshotTestCase/SwiftSupport (2.1.4):
99
- FBSnapshotTestCase/Core
1010
- HandyJSON (5.0.0-beta.1)
11-
- SmartCodable/Core (6.0.0)
12-
- SmartCodable/Inherit (6.0.0):
11+
- SmartCodable/Core (6.0.1)
12+
- SmartCodable/Inherit (6.0.1):
1313
- SmartCodable/Core
1414
- SnapKit (5.6.0)
1515

@@ -41,7 +41,7 @@ SPEC CHECKSUMS:
4141
CleanJSON: 910a36465ce4829e264a902ccf6d1455fdd9f980
4242
FBSnapshotTestCase: 094f9f314decbabe373b87cc339bea235a63e07a
4343
HandyJSON: 582477127ab3ab65bd2e471815f1a7b846856978
44-
SmartCodable: 53c8abfa6b448c8925ad677b3b9c8b3a230064d8
44+
SmartCodable: 54aaad8dc9d8e0e5b5a9c8aecb87322815129a25
4545
SnapKit: e01d52ebb8ddbc333eefe2132acf85c8227d9c25
4646

4747
PODFILE CHECKSUM: fbd909bd1bf3747400cd705163fad38954f6a18f

Example/SmartCodable/Test2ViewController.swift

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,42 @@ import SmartCodable
1010

1111

1212

13-
1413
class Test2ViewController: BaseViewController {
1514

1615
override func viewDidLoad() {
1716
super.viewDidLoad()
1817

19-
2018
}
2119

2220

2321
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
2422
let dict: [String: Any] = [
25-
"my_name": "Tom",
26-
"student": [
27-
"my_age": "18",
28-
"my_name": "Tom",
29-
]
23+
"frinds": [
24+
"a": [],
25+
"b": "b",
26+
"c": NSNull(),
27+
"d": 1,
28+
"f": true
29+
],
30+
"ages": ["a": 1, "b": 2, "c": "3","d": 4],
31+
"names": [ "name1": "Tim", "name2": 2, "name3": []]
3032
]
3133

32-
let student = StudentModel.deserialize(from: dict)
33-
print("1111 \(String(describing: student))")
34+
guard let student = Model.deserialize(from: dict) else { return }
35+
print("⭐️解析结果: frinds = \(student.frinds)")
36+
print("⭐️解析结果: ages = \(student.ages)")
37+
print("⭐️解析结果: names = \(student.names)")
38+
3439
}
3540

36-
37-
struct FlatModel: SmartCodableX {
38-
var _cover: String = "123"
39-
}
40-
struct StudentModel: SmartCodableX {
41-
var student: FlatModel?
41+
struct Model: SmartDecodable {
42+
@SmartCompact.Dictionary
43+
var frinds: [String: Any] = [:]
44+
45+
@SmartCompact.Dictionary
46+
var ages: [String: Int] = [:]
47+
48+
@SmartCompact.Dictionary
49+
var names: [String: String] = [:]
4250
}
4351
}

Example/SmartCodable/TestViewController.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ class TestViewController: BaseViewController {
4545
print("⭐️解析结果: others = \(student.others)")
4646
}
4747
struct Model: SmartCodableX {
48-
@SmartCompact
48+
@SmartCompact.Array
4949
var ages: [Int] = []
50-
@SmartCompact
50+
@SmartCompact.Array
5151
var frinds: [Frind] = []
5252

53-
@SmartCompact
53+
@SmartCompact.Array
5454
var others: [Any] = []
5555
}
5656

Explore&Contribute/Contributing.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
# 💖 SmartCodable 捐赠名单
1+
# 💖 SmartCodable 捐赠
2+
3+
4+
5+
## 💖 捐赠
6+
7+
![Support](https://github.com/iAmMccc/SmartCodable/blob/main/Explore%26Contribute/Images/support.png)
8+
9+
10+
11+
212

313
## 捐赠记录
414

@@ -11,10 +21,6 @@
1121
| 5 | **原来如此 | QQ红包 | 协助支持后续打赏 | 8.88 | 2.5 | 70% 支持人收益,30% 计入奖金池 |
1222
| 6 | bn | 支付宝 | 打赏支持 | 10.24 | 10 | |
1323

14-
15-
16-
17-
1824
### 资金说明
1925

2026
- 捐赠和打赏资金**主要用于开发团队激励**(奶茶奖励)。
@@ -23,12 +29,6 @@
2329

2430

2531

26-
### 💖 捐赠
27-
28-
![Support](https://github.com/iAmMccc/SmartCodable/blob/main/Explore%26Contribute/Images/support.png)
29-
30-
31-
3232

3333

3434

README.md

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
<h1 align="center">SmartCodable - Resilient & Flexible Codable for Swift </h1>
55

66
<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>
107
<a href="https://github.com/iAmMccc/SmartCodable/releases">
118
<img src="https://img.shields.io/github/v/release/iAmMccc/SmartCodable?color=blue&label=version" alt="Latest Release">
129
</a>
@@ -27,6 +24,7 @@
2724
</a>
2825
</p>
2926

27+
3028
**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.
3129

3230
## Features
@@ -441,6 +439,46 @@ print(model)
441439

442440

443441

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+
444482
### 4. Inheritance Support
445483

446484
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

Comments
 (0)