Skip to content

Commit 56cccc3

Browse files
author
Mccc
committed
encode 的options实现
1 parent 1d21d70 commit 56cccc3

14 files changed

Lines changed: 45 additions & 282 deletions

File tree

Example/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ PODS:
88
- FBSnapshotTestCase/SwiftSupport (2.1.4):
99
- FBSnapshotTestCase/Core
1010
- HandyJSON (5.0.0-beta.1)
11-
- SmartCodable (4.1.11-beta.2)
11+
- SmartCodable (4.1.11-beta.3)
1212
- SnapKit (5.6.0)
1313

1414
DEPENDENCIES:
@@ -39,7 +39,7 @@ SPEC CHECKSUMS:
3939
CleanJSON: 910a36465ce4829e264a902ccf6d1455fdd9f980
4040
FBSnapshotTestCase: 094f9f314decbabe373b87cc339bea235a63e07a
4141
HandyJSON: 582477127ab3ab65bd2e471815f1a7b846856978
42-
SmartCodable: d31da2fab38c9dcdfa24a40c7a5880c95dbcf03d
42+
SmartCodable: 5429462702dd8ac32fb664d98c367120cd331d37
4343
SnapKit: e01d52ebb8ddbc333eefe2132acf85c8227d9c25
4444

4545
PODFILE CHECKSUM: 7f3af03f81934df0c035518074a7abbec8fa9d3f

Example/Pods/Local Podspecs/SmartCodable.podspec.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/Pods/Manifest.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/SmartCodable/TestViewController.swift

Lines changed: 0 additions & 221 deletions
Original file line numberDiff line numberDiff line change
@@ -30,228 +30,7 @@ class TestViewController: BaseViewController {
3030
override func viewDidLoad() {
3131
super.viewDidLoad()
3232

33-
SmartConfig.debugMode = .none
34-
35-
36-
37-
let json = """
38-
39-
{
40-
"baseAppDrainage": "",
41-
"adChannel": "0",
42-
"versionInfo": {
43-
"version": "1.0.0",
44-
"build": 1,
45-
"createTime": "2024-07-02 18:59:28",
46-
"id": 1257772650835476480,
47-
"packageName": "com.elevrin.app"
48-
},
49-
"configDefault": {
50-
"IPFiltration": [
51-
"CHINA",
52-
"CHINESE",
53-
"CINEMA",
54-
"CN",
55-
"COMIC",
56-
"CONSTANTIN",
57-
"DISNEY",
58-
"DREAMWORK",
59-
"ENTERTAINMENT",
60-
"EUROPACORP",
61-
"FILM",
62-
"FOCUS",
63-
"GAUMONT",
64-
"GOOGLE",
65-
"HEIDI",
66-
"HOLLYWOOD",
67-
"HULU",
68-
"IMAGE",
69-
"IQIYI",
70-
"LARICK",
71-
"LEGENDARY",
72-
"LIONSGATE",
73-
"MARVEL",
74-
"MEDIA",
75-
"MIRAMAX",
76-
"NETFLIX",
77-
"NEW LINE",
78-
"PARAMOUNT",
79-
"PEACOCK",
80-
"PHILO",
81-
"PICTURE",
82-
"PIXAR",
83-
"SONY",
84-
"STARLIGHT",
85-
"STARZ",
86-
"STUDIO",
87-
"STX",
88-
"TENCENT",
89-
"TOUCHSTONE",
90-
"TUBI",
91-
"TV",
92-
"UNIVERSAL",
93-
"CHINANET",
94-
"VIKI",
95-
"ZEUS",
96-
"VUDU",
97-
"WARNER",
98-
"WEINSTEIN"
99-
]
100-
},
101-
"eventConfig": {
102-
"returnDetail": [],
103-
"eventType": "",
104-
"returnDetailAd": "",
105-
"adType": []
106-
},
107-
"messagePush": "",
108-
"com.elevrin.app": [],
109-
"Home_Data": [
110-
{
111-
"data": [
112-
{
113-
"vodeo": 28407,
114-
"type": "movie"
115-
},
116-
{
117-
"vodeo": 28493,
118-
"type": "movie"
119-
},
120-
{
121-
"vodeo": 24179,
122-
"type": "movie"
123-
},
124-
{
125-
"vodeo": 28367,
126-
"type": "movie"
127-
},
128-
{
129-
"vodeo": 20791,
130-
"type": "movie"
131-
}
132-
],
133-
"level": 0,
134-
"mode": 0,
135-
"name": "Home Carousel"
136-
},
137-
{
138-
"data": [
139-
{
140-
"vodeo": 20791,
141-
"type": "movie"
142-
},
143-
{
144-
"vodeo": 28303,
145-
"type": "movie"
146-
},
147-
{
148-
"vodeo": 28211,
149-
"type": "movie"
150-
},
151-
{
152-
"vodeo": 28236,
153-
"type": "movie"
154-
},
155-
{
156-
"vodeo": 28220,
157-
"type": "movie"
158-
}
159-
],
160-
"level": 1,
161-
"mode": 0,
162-
"name": "Trending",
163-
"id": 1
164-
},
165-
{
166-
"data": [
167-
{
168-
"vodeo": 14064,
169-
"type": "tv"
170-
},
171-
{
172-
"vodeo": 14053,
173-
"type": "tv"
174-
},
175-
{
176-
"vodeo": 14056,
177-
"type": "tv"
178-
},
179-
{
180-
"vodeo": 14043,
181-
"type": "tv"
182-
},
183-
{
184-
"vodeo": 14052,
185-
"type": "tv"
186-
}
187-
],
188-
"level": 2,
189-
"mode": 0,
190-
"name": "Popular TV Shows",
191-
"id": 2
192-
},
193-
{
194-
"data": [
195-
{
196-
"vodeo": 27915,
197-
"type": "movie"
198-
},
199-
{
200-
"vodeo": 25650,
201-
"type": "movie"
202-
},
203-
{
204-
"vodeo": 28380,
205-
"type": "movie"
206-
},
207-
{
208-
"vodeo": 28383,
209-
"type": "movie"
210-
},
211-
{
212-
"vodeo": 28381,
213-
"type": "movie"
214-
}
215-
],
216-
"level": 3,
217-
"mode": 0,
218-
"name": "Popular Movies",
219-
"id": 3
220-
}
221-
],
222-
"playSubTypeRatio": [],
223-
"privacyLink": "https://catfight.top"
224-
}
225-
226-
"""
227-
228-
229-
if let model = NNInfos.deserialize(from: json) {
230-
smartPrint(value: model)
231-
}
23233

233-
}
234-
struct NNInfos: SmartCodable {
235-
var version: String = ""
236-
var build: Int = -1
237-
var ipfilters: [String] = []
238-
var privacyLink: String = ""
23934

240-
static func mappingForKey() -> [SmartKeyTransformer]? {
241-
let versionKey = "versionInfo".fixValue + "." + "version".fixValue
242-
let buildKey = "versionInfo".fixValue + "." + "build".fixValue
243-
let ipfilesKey = "configDefault".fixValue + "." + "IPFiltration".fixValue
244-
return [
245-
CodingKeys.version <--- versionKey,
246-
CodingKeys.build <--- buildKey,
247-
CodingKeys.ipfilters <--- ipfilesKey,
248-
CodingKeys.privacyLink <--- "privacyLink".fixValue
249-
]
250-
}
251-
}
252-
}
253-
extension String {
254-
var fixValue: String {
255-
return self
25635
}
25736
}

Example/SmartCodable/测试用例/解码/数据测试/SpecialData/SpecialData_dataViewController.swift

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,8 @@ class SpecialData_dataViewController: BaseCompatibilityViewController {
1717
test()
1818
}
1919

20-
func getStrategy() -> JSONDecoder.DataDecodingStrategy {
21-
// let strategy: JSONDecoder.DataDecodingStrategy = .base64
22-
23-
let strategy: JSONDecoder.DataDecodingStrategy = .custom({ decoder -> Data in
24-
let container = try decoder.singleValueContainer()
25-
let string = try container.decode(String.self)
26-
guard let data = string.data(using: .utf8) else {
27-
throw DecodingError.dataCorruptedError(in: container, debugDescription: "无法转换为Data类型")
28-
}
29-
return data
30-
})
31-
return strategy
20+
func getStrategy() -> JSONDecoder.SmartDataDecodingStrategy {
21+
return .base64
3222
}
3323

3424
func test() {
@@ -38,7 +28,11 @@ class SpecialData_dataViewController: BaseCompatibilityViewController {
3828
if let model = DataModel.deserialize(from: dict, options: [.data(strategy)]) {
3929
print(model)
4030
print(model.aData.toString() ?? "")
31+
32+
print(model.toDictionary())
4133
}
34+
35+
4236
}
4337
}
4438

@@ -58,7 +52,7 @@ extension SpecialData_dataViewController {
5852

5953
func getDictData(
6054
mode: Mode,
61-
strategy: JSONDecoder.DataDecodingStrategy) -> [String: Any] {
55+
strategy: JSONDecoder.SmartDataDecodingStrategy) -> [String: Any] {
6256
switch mode {
6357
case .keyless:
6458
return [:]
@@ -80,22 +74,13 @@ extension SpecialData_dataViewController {
8074

8175

8276
switch strategy {
83-
// 由于 JSON 标准本身不支持直接的二进制数据表示,我们通常不会在实践中看到 deferredToData 的直接应用。
84-
case .deferredToData:
85-
return [
86-
:
87-
]
77+
8878

8979
case .base64:
9080
return [
9181
"aData": "aHR0cHM6Ly93d3cucWl4aW4uY29t",
9282
"bData": "aHR0cHM6Ly93d3cucWl4aW4uY29t",
9383
]
94-
case .custom(_):
95-
return [
96-
"aData": "Hello, world!",
97-
"bData": "Hello, world!",
98-
]
9984
}
10085

10186
}

SmartCodable.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
Pod::Spec.new do |s|
1414
s.name = 'SmartCodable'
15-
s.version = '4.1.11-beta.2'
15+
s.version = '4.1.11-beta.4'
1616
s.summary = '数据解析库'
1717

1818
s.homepage = 'https://github.com/intsig171'

SmartCodable/Classes/JSONDecoder/Decoder/Impl/JSONDecoderImpl+Unwrap.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,6 @@ extension JSONDecoderImpl {
196196
}
197197

198198
switch self.options.dataDecodingStrategy {
199-
case .deferredToData:
200-
return try Data(from: self)
201-
202199
case .base64:
203200
let container = SingleValueContainer(impl: self, codingPath: self.codingPath, json: self.json)
204201
let string = try container.decode(String.self)
@@ -208,11 +205,6 @@ extension JSONDecoderImpl {
208205
}
209206

210207
return data
211-
212-
case .custom(let closure):
213-
return try closure(self)
214-
@unknown default:
215-
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Encountered Data is not valid , unknown anomaly"))
216208
}
217209
}
218210

0 commit comments

Comments
 (0)