Skip to content

Commit 9f3477f

Browse files
Merge pull request #27 from AppcentMobile/feature/inner-params
🎷 [UPDATE] Inner params added.
2 parents 1d2f8fe + 8b8368e commit 9f3477f

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

Sources/ACMNetworking/Library/Extensions/Codable+Extensions.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ import Foundation
77
extension Encodable {
88
var dictionary: [String: Any]? {
99
guard let data = try? JSONEncoder().encode(self) else { return nil }
10-
return (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)).flatMap { $0 as? [String: Any] }
10+
guard let list = try? JSONSerialization.jsonObject(with: data, options: .fragmentsAllowed) as? [String: Any] else { return nil }
11+
var formattedList = [String: Any]()
12+
list.forEach({ (key: String, value: Any) in
13+
if let strValue = value as? String {
14+
formattedList[key] = strValue
15+
} else if let strValue = value as? [String: Any] {
16+
var innerList = [String: Any]()
17+
strValue.forEach { (key: String, value: Any) in
18+
if let item = value as? String {
19+
if let data = item.data(using: .utf8, allowLossyConversion: true),
20+
let json = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? [String: Any] {
21+
innerList[key] = json
22+
} else {
23+
innerList[key] = item
24+
}
25+
}
26+
}
27+
formattedList[key] = innerList
28+
}
29+
})
30+
return formattedList
1131
}
1232
}

Sources/ACMNetworking/Library/Utils/Body/ACMBodyEncoder.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,21 @@ enum ACMBodyEncoder {
2323
if let dict = item.dictionary {
2424
$0[$1.key] = dict
2525
} else if let arrayOfCodable = $1.value as? [Codable] {
26-
$0[$1.key] = arrayOfCodable.map { $0.dictionary }
26+
$0[$1.key] = arrayOfCodable.map { item in
27+
if let dict = item.dictionary {
28+
return dict
29+
} else if let str = item as? String {
30+
if let data = str.data(using: .utf8, allowLossyConversion: true),
31+
let list = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? [String: Any]
32+
{
33+
return list
34+
} else {
35+
return [:]
36+
}
37+
} else {
38+
return [:]
39+
}
40+
}
2741
} else {
2842
$0[$1.key] = ""
2943
}

0 commit comments

Comments
 (0)