Skip to content

Commit da1e19e

Browse files
committed
better build data list model
1 parent a8a5c54 commit da1e19e

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

JSONConverter/Classes/Builder/JSONBuilder.swift

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,27 @@ class JSONBuilder {
4444
let content = file.contentWithKeyName(keyName)
4545

4646
dic.forEach { (item) in
47-
let itemKey = item.key
48-
var propertyModel: Property?
47+
let keyName = item.key
48+
var property: Property?
4949

5050
switch item.value {
5151
case _ as String:
52-
propertyModel = file.propertyWithKeyName(itemKey, type: .String)
52+
property = file.propertyWithKeyName(keyName, type: .String)
5353
case let num as NSNumber:
54-
propertyModel = file.propertyWithKeyName(itemKey, type: num.valueType())
54+
property = file.propertyWithKeyName(keyName, type: num.valueType())
5555
case let dic as [String: Any]:
56-
propertyModel = file.propertyWithKeyName(itemKey, type: .Dictionary)
57-
let content = addDictionaryWithKeyName(itemKey, dic: dic)
56+
property = file.propertyWithKeyName(keyName, type: .Dictionary)
57+
let content = addDictionaryWithKeyName(keyName, dic: dic)
5858
file.contents.insert(content, at: 0)
5959
case let arr as [Any]:
60-
propertyModel = addArraryWithKeyName(itemKey, valueArrary: arr)
60+
property = addArraryWithKeyName(keyName, valueArrary: arr)
6161
case _ as NSNull:
62-
propertyModel = file.propertyWithKeyName(itemKey, type: .nil)
62+
property = file.propertyWithKeyName(keyName, type: .nil)
6363
default:
64-
assertionFailure("parse object type error")
64+
assertionFailure("build JSON object type error")
6565
}
6666

67-
if let propertyModel = propertyModel {
67+
if let propertyModel = property {
6868
content.properties.append(propertyModel)
6969
}
7070
}
@@ -73,13 +73,16 @@ class JSONBuilder {
7373
}
7474

7575
private func addArraryWithKeyName(_ keyName: String, valueArrary: [Any]) -> Property? {
76-
if valueArrary.count == 0 {
77-
return nil
76+
var item = valueArrary.first
77+
if valueArrary.first is Dictionary<String, Any> {
78+
var temp = [String: Any]()
79+
valueArrary.forEach { temp.merge($0 as! [String: Any]) { $1 } }
80+
item = temp
7881
}
7982

80-
if let first = valueArrary.first {
83+
if let item = item {
8184
var propertyModel: Property?
82-
switch first {
85+
switch item {
8386
case _ as String:
8487
propertyModel = file.propertyWithKeyName(keyName, type: .ArrayString)
8588
case let num as NSNumber:
@@ -90,14 +93,14 @@ class JSONBuilder {
9093
let content = addDictionaryWithKeyName(keyName, dic: dic)
9194
file.contents.insert(content, at: 0)
9295
default:
93-
assertionFailure("parse object type error")
96+
assertionFailure("build JSON object type error")
9497
break
9598
}
9699

97100
return propertyModel
101+
}else {
102+
return nil
98103
}
99-
100-
return nil
101104
}
102105
}
103106

0 commit comments

Comments
 (0)