Skip to content

Commit 0b6f0a5

Browse files
author
Mccc
committed
bugfix
1 parent b17d751 commit 0b6f0a5

File tree

10 files changed

+123
-22
lines changed

10 files changed

+123
-22
lines changed

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.2.2)
11+
- SmartCodable (4.2.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: 4f52d801dbd15856fabb69110d24f31f49145734
42+
SmartCodable: f01c43e62a8867828fb9f51f4b354b657dafc37d
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/Target Support Files/SmartCodable/SmartCodable-Info.plist

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/SmartCodable/Test2ViewController.swift

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,43 @@ class Test2ViewController: BaseViewController {
1616
override func viewDidLoad() {
1717
super.viewDidLoad()
1818

19-
let dict = [
20-
"name": 1.22222
19+
let dict: [String: Any] = [
20+
"name": "mccc",
21+
"subModel": "mccc111",
22+
2123
]
2224
let model = Model.deserialize(from: dict)
23-
print(model)
25+
print(model?.subModel?.rawValue)
26+
27+
let dict1 = model?.toDictionary()
28+
print(dict1)
2429

2530
}
2631

2732
struct Model: SmartCodable {
2833
var name: String = ""
34+
@SmartPublished
35+
var subModel: TestEnum?
2936

3037

38+
static func mappingForValue() -> [SmartValueTransformer]? {
39+
[
40+
CodingKeys.name <--- FastTransformer<String, String>(fromJSON: { json in
41+
"abc"
42+
}),
43+
CodingKeys.subModel <--- FastTransformer<TestEnum, String>(fromJSON: { json in
44+
TestEnum.man
45+
}),
46+
]
47+
}
48+
}
49+
50+
enum TestEnum: String, SmartCaseDefaultable {
51+
case man
52+
}
53+
54+
struct SubModel: SmartCodable {
55+
var name: String = ""
56+
3157
}
3258
}

Example/SmartCodable/TestViewController.swift

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import CleanJSON
1414
import BTPrint
1515

1616

17-
1817
/** 字典的值情况
1918
1. @Published 修饰的属性的解析。
2019
2. 继承关系!!!!
@@ -35,6 +34,38 @@ class TestViewController: BaseViewController {
3534

3635
override func viewDidLoad() {
3736
super.viewDidLoad()
37+
let jsonString = """
38+
{
39+
40+
"enjoyCount": 462,
41+
"talkCount": null,
42+
}
43+
44+
"""
45+
46+
guard let model = RecommendModel.deserialize(from: jsonString) else {
47+
return
48+
}
49+
50+
print(model)
51+
}
52+
53+
54+
struct RecommendModel: SmartCodable {
55+
56+
/// 点赞数
57+
var enjoyCount: Int = 0
58+
/// 评论数
59+
var commentCount: Int = 0
60+
3861

62+
63+
static func mappingForKey() -> [SmartKeyTransformer]? {
64+
[
65+
CodingKeys.commentCount <--- ["commentCount","talkCount","postCommentCount","topicCommentCount","topicTalkCount", "articleCommentCount"],
66+
CodingKeys.enjoyCount <--- ["articleEnjoyCount","enjoyCount","topicEnjoyCount","postEnjoyCount"],
67+
]
68+
}
3969
}
70+
4071
}

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.2.2'
15+
s.version = '4.2.3'
1616
s.summary = '数据解析库'
1717

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

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -290,15 +290,16 @@ extension JSONDecoderImpl.KeyedContainer {
290290

291291
// 如果值可以被成功获取
292292
if let value = try? getValue(forKey: key) {
293-
if let decoded = impl.cache.tranform(value: value, for: key) {
294-
295-
// 检查 SmartPublished 包装器类型
296-
if let publishedType = T.self as? any SmartPublishedProtocol.Type,
297-
let publishedValue = publishedType.createInstance(with: decoded) as? T {
298-
return publishedValue
299-
}
300-
}
301-
}
293+
if let decoded = impl.cache.tranform(value: value, for: key) {
294+
if let tTypeValue = decoded as? T {
295+
return tTypeValue
296+
} else if let publishedType = T.self as? any SmartPublishedProtocol.Type,
297+
let publishedValue = publishedType.createInstance(with: decoded) as? T {
298+
// // 检查 SmartPublished 包装器类型
299+
return publishedValue
300+
}
301+
}
302+
}
302303

303304

304305

SmartCodable/Classes/JSONDecoder/Decoder/KeysMapper.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ struct KeysMapper {
4444
type.mappingForKey()?.forEach { mapping in
4545
for oldKey in mapping.from {
4646
let newKey = mapping.to.stringValue
47-
if let value = newDict[oldKey], !(value is NSNull) {
47+
if let value = newDict[oldKey] as? JSONValue, value != .null {
4848
newDict[newKey] = newDict[oldKey]
4949
break
5050
} else { // Handles the case of a custom parsing path.

SmartCodable/Classes/Transformer/Transformer.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,46 @@ public protocol ValueTransformable {
5353
public func <---(location: CodingKey, tranformer: any ValueTransformable) -> SmartValueTransformer {
5454
SmartValueTransformer.init(location: location, tranformer: tranformer)
5555
}
56+
57+
58+
59+
/** 便捷的Transformer
60+
static func mappingForValue() -> [SmartValueTransformer]? {
61+
[
62+
CodingKeys.name <--- FastTransformer<String, String>(fromJSON: { json in
63+
"abc"
64+
}, toJSON: { object in
65+
"123"
66+
}),
67+
CodingKeys.subModel <--- FastTransformer<TestEnum, String>(fromJSON: { json in
68+
TestEnum.man
69+
}, toJSON: { object in
70+
object?.rawValue
71+
}),
72+
]
73+
}
74+
*/
75+
76+
public struct FastTransformer<Object, JSON>: ValueTransformable {
77+
78+
private let fromJSON: (JSON?) -> Object?
79+
private let toJSON: ((Object?) -> JSON?)?
80+
81+
82+
/// 便捷的转换器
83+
/// - Parameters:
84+
/// - fromJSON: json 转 object
85+
/// - toJSON: object 转 json, 如果需要转json,可以不实现。
86+
public init(fromJSON: @escaping (JSON?) -> Object?, toJSON: ((Object?) -> JSON?)? = nil) {
87+
self.fromJSON = fromJSON
88+
self.toJSON = toJSON
89+
}
90+
91+
public func transformFromJSON(_ value: Any) -> Object? {
92+
return fromJSON(value as? JSON)
93+
}
94+
95+
public func transformToJSON(_ value: Object) -> JSON? {
96+
return toJSON?(value)
97+
}
98+
}

0 commit comments

Comments
 (0)