Skip to content

Commit f01f542

Browse files
author
Mccc
committed
V4.4.0
1 parent 55dfab3 commit f01f542

12 files changed

Lines changed: 583 additions & 560 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.4.0-beta.2)
11+
- SmartCodable (4.4.0)
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: 95cae8388d2ea005a450111963ce7ba366f7b7b3
42+
SmartCodable: 87f2a1d4af818c40786651cdcce14176d0aefc0b
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/Pods/Pods.xcodeproj/project.pbxproj

Lines changed: 510 additions & 497 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: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ class TestViewController: BaseViewController {
6060
// print(model?.color3)
6161

6262
print(model?.date)
63-
print(model?.date1)
64-
print(model?.date2)
65-
print(model?.date3)
66-
print(model?.date4)
63+
// print(model?.date1)
64+
// print(model?.date2)
65+
// print(model?.date3)
66+
// print(model?.date4)
6767

6868
print("\n\n")
6969

@@ -87,16 +87,16 @@ class TestViewController: BaseViewController {
8787
// var color3: UIColor?
8888

8989

90-
@SmartDate
90+
// @SmartDate
9191
var date: Date?
92-
@SmartDate
93-
var date1: Date?
94-
@SmartDate
95-
var date2: Date?
96-
@SmartDate
97-
var date3: Date?
98-
@SmartDate
99-
var date4: Date?
92+
// @SmartDate
93+
// var date1: Date?
94+
// @SmartDate
95+
// var date2: Date?
96+
// @SmartDate
97+
// var date3: Date?
98+
// @SmartDate
99+
// var date4: Date?
100100

101101

102102

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.4.0-beta.2'
15+
s.version = '4.4.0'
1616
s.summary = 'Swift数据解析库'
1717

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

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

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -155,46 +155,53 @@ extension JSONDecoderImpl {
155155
}
156156
}
157157

158+
let container = SingleValueContainer(impl: self, codingPath: codingPath, json: json)
159+
158160

159-
switch self.options.dateDecodingStrategy {
160-
case .deferredToDate:
161-
return try Date(from: self)
162-
163-
case .secondsSince1970:
164-
let container = SingleValueContainer(impl: self, codingPath: self.codingPath, json: self.json)
165-
let double = try container.decode(Double.self)
166-
return Date(timeIntervalSince1970: double)
167-
168-
case .millisecondsSince1970:
169-
let container = SingleValueContainer(impl: self, codingPath: self.codingPath, json: self.json)
170-
let double = try container.decode(Double.self)
171-
return Date(timeIntervalSince1970: double / 1000.0)
172-
173-
case .iso8601:
174-
if #available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
175-
let container = SingleValueContainer(impl: self, codingPath: self.codingPath, json: self.json)
176-
let string = try container.decode(String.self)
177-
guard let date = _iso8601Formatter.date(from: string) else {
178-
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Expected date string to be ISO8601-formatted."))
161+
if let dateDecodingStrategy = self.options.dateDecodingStrategy {
162+
switch dateDecodingStrategy {
163+
case .deferredToDate:
164+
return try Date(from: self)
165+
166+
case .secondsSince1970:
167+
let double = try container.decode(Double.self)
168+
return Date(timeIntervalSince1970: double)
169+
170+
case .millisecondsSince1970:
171+
let double = try container.decode(Double.self)
172+
return Date(timeIntervalSince1970: double / 1000.0)
173+
174+
case .iso8601:
175+
if #available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
176+
let string = try container.decode(String.self)
177+
guard let date = _iso8601Formatter.date(from: string) else {
178+
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Expected date string to be ISO8601-formatted."))
179+
}
180+
181+
return date
182+
} else {
183+
fatalError("ISO8601DateFormatter is unavailable on this platform.")
179184
}
180185

186+
case .formatted(let formatter):
187+
let string = try container.decode(String.self)
188+
guard let date = formatter.date(from: string) else {
189+
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Date string does not match format expected by formatter."))
190+
}
181191
return date
182-
} else {
183-
fatalError("ISO8601DateFormatter is unavailable on this platform.")
184-
}
185-
186-
case .formatted(let formatter):
187-
let container = SingleValueContainer(impl: self, codingPath: self.codingPath, json: self.json)
188-
let string = try container.decode(String.self)
189-
guard let date = formatter.date(from: string) else {
190-
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Date string does not match format expected by formatter."))
192+
193+
case .custom(let closure):
194+
return try closure(self)
195+
@unknown default:
196+
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Encountered Date is not valid , unknown anomaly"))
191197
}
198+
}
199+
200+
// 如果没有设置策略,使用 DateParser 做兜底解析
201+
if let (date, _) = DateParser.parse(json.peel) {
192202
return date
193-
194-
case .custom(let closure):
195-
return try closure(self)
196-
@unknown default:
197-
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Encountered Date is not valid , unknown anomaly"))
203+
} else {
204+
throw DecodingError.dataCorrupted(.init(codingPath: codingPath, debugDescription: "Unsupported date format: \(json)"))
198205
}
199206
}
200207

SmartCodable/Classes/JSONDecoder/Decoder/SmartJSONDecoder.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ open class SmartJSONDecoder: JSONDecoder, @unchecked Sendable {
1414

1515
/// Options set on the top-level encoder to pass down the decoding hierarchy.
1616
struct _Options {
17-
let dateDecodingStrategy: DateDecodingStrategy
17+
let dateDecodingStrategy: DateDecodingStrategy?
1818
let dataDecodingStrategy: SmartDataDecodingStrategy
1919
let nonConformingFloatDecodingStrategy: NonConformingFloatDecodingStrategy
2020
let keyDecodingStrategy: SmartKeyDecodingStrategy
@@ -24,14 +24,16 @@ open class SmartJSONDecoder: JSONDecoder, @unchecked Sendable {
2424
/// The options set on the top-level decoder.
2525
var options: _Options {
2626
return _Options(
27-
dateDecodingStrategy: dateDecodingStrategy,
27+
dateDecodingStrategy: smartDateDecodingStrategy,
2828
dataDecodingStrategy: smartDataDecodingStrategy,
2929
nonConformingFloatDecodingStrategy: nonConformingFloatDecodingStrategy,
3030
keyDecodingStrategy: smartKeyDecodingStrategy,
3131
userInfo: userInfo
3232
)
3333
}
3434

35+
open var smartDateDecodingStrategy: DateDecodingStrategy?
36+
3537
open var smartKeyDecodingStrategy: SmartKeyDecodingStrategy = .useDefaultKeys
3638

3739

SmartCodable/Classes/JSONEncoder/SmartJSONEncoder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ open class SmartJSONEncoder: JSONEncoder, @unchecked Sendable {
2525

2626
/// The options set on the top-level encoder.
2727
fileprivate var options: _Options {
28-
return _Options(dateEncodingStrategy: dateEncodingStrategy,
28+
return _Options(dateEncodingStrategy: .secondsSince1970,
2929
dataEncodingStrategy: smartDataEncodingStrategy,
3030
nonConformingFloatEncodingStrategy: nonConformingFloatEncodingStrategy,
3131
keyEncodingStrategy: smartKeyEncodingStrategy,

SmartCodable/Classes/PropertyWrapper/SmartDate.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ struct DateParser {
8686
"yyyy/MM/dd",
8787
"MM/dd/yyyy",
8888
"yyyy-MM-dd HH:mm",
89+
"yyyy-MM-dd'T'HH:mm:ss",
8990
"yyyy-MM-dd'T'HH:mm:ssZ",
9091
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
9192
]

0 commit comments

Comments
 (0)