Skip to content

Commit 1eae39a

Browse files
committed
修复json解析bug
1 parent d6a45d1 commit 1eae39a

4 files changed

Lines changed: 147 additions & 11 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 (5.1.1)
12-
- SmartCodable/Inherit (5.1.1):
11+
- SmartCodable/Core (5.1.2)
12+
- SmartCodable/Inherit (5.1.2):
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: 4592eeb1e7921e96c91372df59e7260f963a8dbc
44+
SmartCodable: 275755f95fd7019ce6b626f75517af5f2cf55bc7
4545
SnapKit: e01d52ebb8ddbc333eefe2132acf85c8227d9c25
4646

4747
PODFILE CHECKSUM: fbd909bd1bf3747400cd705163fad38954f6a18f

Example/SmartCodable/Test3ViewController.swift

Lines changed: 135 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,148 @@ class Test3ViewController: BaseViewController {
1010

1111
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
1212

13-
let dict: [String: Any] = [
14-
"name": "Mccc",
15-
"age": 200
16-
]
17-
18-
guard let model = [Model].deserialize(from: [dict, dict]) else { return }
19-
print(model)
13+
let arr = ARTEmojiEntity.parseEmojis()
14+
print(arr)
2015
}
2116

2217
struct Model: SmartCodable {
2318

2419
var name: String?
2520
}
21+
22+
23+
}
24+
25+
26+
27+
struct ARTEmojiEntity: SmartCodable, Equatable {
28+
29+
var emojis: [EmojiItem] = [] /// 表情列表
30+
31+
// MARK: - 表情信息结构
32+
struct EmojiItem: SmartCodable, Equatable {
33+
var identifier: String = "" /// 表情的唯一标识符,比如 ":smile:"
34+
var image: String = "" /// 表情图片
35+
}
36+
37+
38+
static func parseEmojis() -> [String: UIImage] {
39+
guard let entity = ARTEmojiEntity.deserialize(from: requestJsonString()) else {
40+
print("Error deserializing JSON")
41+
return [:]
42+
}
43+
44+
45+
print(entity)
46+
47+
// 将表情信息转换为 [String: UIImage] 字典
48+
var emojiDict: [String: UIImage] = [:]
49+
for emoji in entity.emojis {
50+
if let image = UIImage(named: emoji.image) {
51+
emojiDict[emoji.identifier] = image
52+
} else {
53+
print("Error loading image for identifier: \(emoji.identifier)")
54+
}
55+
}
56+
return emojiDict
57+
}
2658
}
2759

2860

61+
// MARK: - 表情数据
62+
extension ARTEmojiEntity {
63+
64+
static func requestJsonString() -> String {
65+
let jsonString = """
66+
{
67+
"emojis": [
68+
{"image": "zeco_emoji_001", "identifier": ":circle:"},
69+
{"image": "zeco_emoji_002", "identifier": ":smile:"},
70+
{"image": "zeco_emoji_003", "identifier": ":awkward:"},
71+
{"image": "zeco_emoji_004", "identifier": ":heart:"},
72+
{"image": "zeco_emoji_005", "identifier": ":quiet:"},
73+
{"image": "zeco_emoji_006", "identifier": ":surprised:"},
74+
{"image": "zeco_emoji_007", "identifier": ":proud:"},
75+
{"image": "zeco_emoji_008", "identifier": ":grin:"},
76+
{"image": "zeco_emoji_009", "identifier": ":naughty:"},
77+
{"image": "zeco_emoji_010", "identifier": ":dazed:"},
78+
{"image": "zeco_emoji_011", "identifier": ":arrogant:"},
79+
{"image": "zeco_emoji_012", "identifier": ":shock:"},
80+
{"image": "zeco_emoji_013", "identifier": ":blush:"},
81+
{"image": "zeco_emoji_014", "identifier": ":happy:"},
82+
{"image": "zeco_emoji_015", "identifier": ":facepalm:"},
83+
{"image": "zeco_emoji_016", "identifier": ":sleepy:"},
84+
{"image": "zeco_emoji_017", "identifier": ":sleep:"},
85+
{"image": "zeco_emoji_018", "identifier": ":tear:"},
86+
{"image": "zeco_emoji_019", "identifier": ":sad:"},
87+
{"image": "zeco_emoji_020", "identifier": ":cow:"},
88+
{"image": "zeco_emoji_021", "identifier": ":angry:"},
89+
{"image": "zeco_emoji_022", "identifier": ":petrified:"},
90+
{"image": "zeco_emoji_023", "identifier": ":snicker:"},
91+
{"image": "zeco_emoji_024", "identifier": ":pout:"},
92+
{"image": "zeco_emoji_025", "identifier": ":eyeroll:"},
93+
{"image": "zeco_emoji_026", "identifier": ":thank_you:"},
94+
{"image": "zeco_emoji_027", "identifier": ":cry:"},
95+
{"image": "zeco_emoji_028", "identifier": ":crazy:"},
96+
{"image": "zeco_emoji_029", "identifier": ":thumbs_up:"},
97+
{"image": "zeco_emoji_030", "identifier": ":goodbye:"},
98+
{"image": "zeco_emoji_031", "identifier": ":question:"},
99+
{"image": "zeco_emoji_032", "identifier": ":dog_head:"},
100+
{"image": "zeco_emoji_033", "identifier": ":yeah:"},
101+
{"image": "zeco_emoji_034", "identifier": ":thumbs_up_hand:"},
102+
{"image": "zeco_emoji_035", "identifier": ":point:"},
103+
{"image": "zeco_emoji_036", "identifier": ":clasped_hands:"},
104+
{"image": "zeco_emoji_037", "identifier": ":ok:"},
105+
{"image": "zeco_emoji_038", "identifier": ":handshake:"},
106+
{"image": "zeco_emoji_039", "identifier": ":clap:"},
107+
{"image": "zeco_emoji_040", "identifier": ":fist_bump:"},
108+
{"image": "zeco_emoji_041", "identifier": ":muscle:"},
109+
{"image": "zeco_emoji_042", "identifier": ":location:"},
110+
{"image": "zeco_emoji_043", "identifier": ":airplane:"},
111+
{"image": "zeco_emoji_044", "identifier": ":gift:"},
112+
{"image": "zeco_emoji_045", "identifier": ":graduation_cap:"},
113+
{"image": "zeco_emoji_046", "identifier": ":notebook:"},
114+
{"image": "zeco_emoji_047", "identifier": ":fire:"},
115+
{"image": "zeco_emoji_048", "identifier": ":money_bag:"},
116+
{"image": "zeco_emoji_049", "identifier": ":rich:"},
117+
{"image": "zeco_emoji_050", "identifier": ":cheer:"},
118+
{"image": "zeco_emoji_051", "identifier": ":cake:"},
119+
{"image": "zeco_emoji_052", "identifier": ":fireworks:"},
120+
{"image": "zeco_emoji_053", "identifier": ":look:"},
121+
{"image": "zeco_emoji_054", "identifier": ":wallet:"},
122+
{"image": "zeco_emoji_055", "identifier": ":star:"},
123+
{"image": "zeco_emoji_056", "identifier": ":rose:"},
124+
{"image": "zeco_emoji_057", "identifier": ":relaxed:"},
125+
{"image": "zeco_emoji_058", "identifier": ":parenting:"},
126+
{"image": "zeco_emoji_059", "identifier": ":useful:"},
127+
{"image": "zeco_emoji_060", "identifier": ":saved:"},
128+
{"image": "zeco_emoji_061", "identifier": ":seedling:"},
129+
{"image": "zeco_emoji_062", "identifier": ":have:"},
130+
{"image": "zeco_emoji_063", "identifier": ":yes:"},
131+
{"image": "zeco_emoji_064", "identifier": ":one:"},
132+
{"image": "zeco_emoji_065", "identifier": ":two:"},
133+
{"image": "zeco_emoji_066", "identifier": ":three:"},
134+
{"image": "zeco_emoji_067", "identifier": ":four:"},
135+
{"image": "zeco_emoji_068", "identifier": ":five:"},
136+
{"image": "zeco_emoji_069", "identifier": ":six:"},
137+
{"image": "zeco_emoji_070", "identifier": ":seven:"},
138+
{"image": "zeco_emoji_071", "identifier": ":eight:"},
139+
{"image": "zeco_emoji_072", "identifier": ":nine:"},
140+
{"image": "zeco_emoji_073", "identifier": ":full:"},
141+
{"image": "zeco_emoji_074", "identifier": ":plus_one:"},
142+
{"image": "zeco_emoji_075", "identifier": ":prohibited:"},
143+
{"image": "zeco_emoji_076", "identifier": ":yellow_heart:"},
144+
{"image": "zeco_emoji_077", "identifier": ":orange_heart:"},
145+
{"image": "zeco_emoji_078", "identifier": ":green_heart:"},
146+
{"image": "zeco_emoji_079", "identifier": ":dark_green_heart:"},
147+
{"image": "zeco_emoji_080", "identifier": ":blue_heart:"}
148+
]
149+
}
150+
"""
151+
return jsonString
152+
}
153+
}
154+
155+
/**
29156

157+
*/

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

1818
s.homepage = 'https://github.com/iAmMccc/SmartCodable'

Sources/SmartCodable/Core/JSONDecoder/Decoder/SmartJSONDecoder.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ open class SmartJSONDecoder: JSONDecoder, @unchecked Sendable {
6969

7070
case let arr as [Any]:
7171
jsonObject = arr
72+
73+
case let json as String:
74+
guard let object = json.toJSONObject() else {
75+
let error = DecodingError.dataCorrupted(.init(codingPath: [], debugDescription: "不支持的 JSON 值类型"))
76+
SmartSentinel.monitorAndPrint(debugDescription: "The given data was not valid JSON.", error: error, in: type)
77+
throw error
78+
}
79+
jsonObject = object
7280
default:
7381
let error = DecodingError.dataCorrupted(.init(codingPath: [], debugDescription: "不支持的 JSON 值类型"))
7482
SmartSentinel.monitorAndPrint(debugDescription: "The given data was not valid JSON.", error: error, in: type)

0 commit comments

Comments
 (0)