Skip to content

Commit 26a667a

Browse files
author
Mccc
committed
优化key的自定义映射规则
1 parent 0b6f0a5 commit 26a667a

5 files changed

Lines changed: 92 additions & 100 deletions

File tree

Example/SmartCodable/Test2ViewController.swift

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,49 @@ class Test2ViewController: BaseViewController {
1717
super.viewDidLoad()
1818

1919
let dict: [String: Any] = [
20-
"name": "mccc",
21-
"subModel": "mccc111",
20+
// "name": "mccc",
21+
"age": 30,
22+
"info": [
23+
"name": "mccc111"
24+
],
25+
"sub": [
26+
"subname": "qilin",
27+
"subage": 3,
28+
"info": [
29+
"name": "qilin111"
30+
],
31+
]
2232

2333
]
2434
let model = Model.deserialize(from: dict)
25-
print(model?.subModel?.rawValue)
26-
27-
let dict1 = model?.toDictionary()
28-
print(dict1)
35+
BTPrint.print(model)
2936

30-
}
31-
32-
struct Model: SmartCodable {
33-
var name: String = ""
34-
@SmartPublished
35-
var subModel: TestEnum?
37+
print("\n")
3638

37-
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-
}
39+
let tranDict = model?.toDictionary() ?? [:]
40+
BTPrint.print(tranDict)
4841
}
42+
}
43+
44+
struct Model: SmartCodable {
45+
var name: String = ""
46+
var age: Int = 0
47+
var sub: SubModel = SubModel()
4948

50-
enum TestEnum: String, SmartCaseDefaultable {
51-
case man
49+
static func mappingForKey() -> [SmartKeyTransformer]? {
50+
[
51+
CodingKeys.name <--- "info.name"
52+
]
5253
}
54+
}
55+
56+
struct SubModel: SmartCodable {
57+
var subname: String = ""
58+
var subage: Int = 0
5359

54-
struct SubModel: SmartCodable {
55-
var name: String = ""
56-
60+
static func mappingForKey() -> [SmartKeyTransformer]? {
61+
[
62+
CodingKeys.subname <--- "info.name"
63+
]
5764
}
5865
}

Example/SmartCodable/Test3ViewController.swift

Lines changed: 20 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import CleanJSON
1515
/** 测试内容项
1616
1. 默认值的使用是否正常
1717
2. mappingForValue是否正常。
18-
3.
18+
3.
1919
*/
2020

2121

@@ -25,67 +25,33 @@ class Test3ViewController: BaseViewController {
2525

2626
override func viewDidLoad() {
2727
super.viewDidLoad()
28-
29-
3028

3129
let dict1: [String: Any] = [
32-
"age": 10,
33-
"name": "Mccc",
34-
"location": [
35-
"province": "Jiang zhou",
36-
"city": "Su zhou",
30+
"code": "10000",
31+
"msg": "成功",
32+
"data": [
33+
"guideSvga": "guideSvga",
34+
"guideOnevga": "guideOnevga",
35+
"loadingSvga": "loadingSvga",
36+
"loadingSvgaBackgroundColor": "loadingSvgaBackgroundColor",
3737
]
38-
3938
]
4039

41-
if let jsonData = try? JSONSerialization.data(withJSONObject: dict1, options: []) {
42-
// Successfully converted Dictionary to Data
43-
print("JSON Data:", jsonData)
44-
45-
do {
46-
let obj = try JSONDecoder().decode(Model.self, from: jsonData)
47-
print("obj = ", obj)
48-
49-
} catch {
50-
print("error = ", error)
51-
}
52-
53-
// If you want to convert it back to a String for debugging purposes
54-
if let jsonString = String(data: jsonData, encoding: .utf8) {
55-
print("JSON String:", jsonString)
56-
}
57-
}
58-
59-
if let model = Model.deserialize(from: dict1) {
60-
smartPrint(value: model)
61-
print("\n")
62-
let dict = model.toDictionary() ?? [:]
63-
print(dict)
64-
}
40+
guard let model = ResponseData<HomeListModel>.deserialize(from: dict1) else { return }
41+
print(model)
6542
}
66-
}
67-
68-
69-
70-
71-
extension Test3ViewController {
72-
struct Model: SmartCodable {
73-
var name: String = ""
74-
@IgnoredKey
75-
var ignore: String = ""
76-
@IgnoredKey
77-
var ignore2 = ""
78-
var age: Int = 0
79-
var location: Location?
43+
44+
struct HomeListModel: SmartCodable {
45+
var guideSvga = ""
46+
var guideOnevga = ""
47+
var loadingSvga = ""
48+
var loadingSvgaBackgroundColor = ""
8049
}
8150

82-
struct Location: SmartCodable {
83-
var province: String = ""
84-
85-
// 忽略解析
86-
@IgnoredKey
87-
var city: String = "area123"
51+
struct ResponseData<T>: SmartCodable where T: SmartCodable {
52+
var code = ""
53+
var msg = ""
54+
var data: T?
8855
}
8956
}
9057

91-

Example/SmartCodable/TestViewController.swift

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,37 +35,52 @@ class TestViewController: BaseViewController {
3535
override func viewDidLoad() {
3636
super.viewDidLoad()
3737
let jsonString = """
38+
[
3839
{
39-
40-
"enjoyCount": 462,
41-
"talkCount": null,
40+
"enjoyCount": 1,
41+
"commentCount": 1,
42+
},
43+
{
44+
"enjoyCount": 2,
45+
"commentCount": 2,
46+
},
47+
{
48+
"enjoyCount": 2,
49+
"commentCount": 2,
4250
}
51+
]
4352
4453
"""
45-
46-
guard let model = RecommendModel.deserialize(from: jsonString) else {
54+
guard let models = [RecommendModel].deserialize(from: jsonString) else {
4755
return
4856
}
57+
print(models)
58+
print("\n")
4959

50-
print(model)
60+
let uniqueRecommends = Array(Set(models))
61+
print(uniqueRecommends)
5162
}
5263

53-
54-
struct RecommendModel: SmartCodable {
64+
struct RecommendModel: SmartCodable, Equatable, Hashable {
5565

5666
/// 点赞数
5767
var enjoyCount: Int = 0
5868
/// 评论数
5969
var commentCount: Int = 0
60-
6170

62-
63-
static func mappingForKey() -> [SmartKeyTransformer]? {
64-
[
65-
CodingKeys.commentCount <--- ["commentCount","talkCount","postCommentCount","topicCommentCount","topicTalkCount", "articleCommentCount"],
66-
CodingKeys.enjoyCount <--- ["articleEnjoyCount","enjoyCount","topicEnjoyCount","postEnjoyCount"],
67-
]
71+
@IgnoredKey
72+
var priceText: NSAttributedString? // 价格富文本
73+
74+
75+
static func == (lhs: RecommendModel, rhs: RecommendModel) -> Bool {
76+
return true
77+
}
78+
79+
// 手动实现 Hashable 协议
80+
func hash(into hasher: inout Hasher) {
81+
hasher.combine(enjoyCount)
82+
hasher.combine(commentCount)
6883
}
6984
}
70-
85+
7186
}

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

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

SmartCodable/Classes/JSONDecoder/Decoder/KeysMapper.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,15 @@ struct KeysMapper {
4444
type.mappingForKey()?.forEach { mapping in
4545
for oldKey in mapping.from {
4646
let newKey = mapping.to.stringValue
47+
48+
// 先移除数据中原本的字段
49+
newDict.removeValue(forKey: newKey)
50+
4751
if let value = newDict[oldKey] as? JSONValue, value != .null {
4852
newDict[newKey] = newDict[oldKey]
4953
break
5054
} else { // Handles the case of a custom parsing path.
51-
if newDict[newKey] == nil, let pathValue = newDict.getValue(forKeyPath: oldKey) {
55+
if let pathValue = newDict.getValue(forKeyPath: oldKey) {
5256
newDict.updateValue(pathValue, forKey: newKey)
5357
}
5458
}

0 commit comments

Comments
 (0)