/
ToJSON.swift
174 lines (146 loc) · 4.74 KB
/
ToJSON.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//
// ToJSON.swift
// ObjectMapper
//
// Created by Tristan Himmelman on 2014-10-13.
// Copyright (c) 2014 hearst. All rights reserved.
//
import class Foundation.NSNumber
private func setValue(value: AnyObject, map: Map) {
setValue(value, key: map.currentKey!, checkForNestedKeys: map.keyIsNested, dictionary: &map.JSONDictionary)
}
private func setValue(value: AnyObject, key: String, checkForNestedKeys: Bool, inout dictionary: [String : AnyObject]) {
if checkForNestedKeys {
let keyComponents = ArraySlice(key.characters.split { $0 == "." })
setValue(value, forKeyPathComponents: keyComponents, dictionary: &dictionary)
} else {
dictionary[key] = value
}
}
private func setValue(value: AnyObject, forKeyPathComponents components: ArraySlice<String.CharacterView.SubSequence>, inout dictionary: [String : AnyObject]) {
if components.isEmpty {
return
}
let head = components.first!
if components.count == 1 {
dictionary[String(head)] = value
} else {
var child = dictionary[String(head)] as? [String : AnyObject]
if child == nil {
child = [:]
}
let tail = components.dropFirst()
setValue(value, forKeyPathComponents: tail, dictionary: &child!)
dictionary[String(head)] = child
}
}
internal final class ToJSON {
class func basicType<N>(field: N, map: Map) {
func _setValue(value: AnyObject) {
setValue(value, map: map)
}
if let x = field as? NSNumber { // basic types
_setValue(x)
} else if let x = field as? Bool {
_setValue(x)
} else if let x = field as? Int {
_setValue(x)
} else if let x = field as? Double {
_setValue(x)
} else if let x = field as? Float {
_setValue(x)
} else if let x = field as? String {
_setValue(x)
} else if let x = field as? Array<NSNumber> { // Arrays
_setValue(x)
} else if let x = field as? Array<Bool> {
_setValue(x)
} else if let x = field as? Array<Int> {
_setValue(x)
} else if let x = field as? Array<Double> {
_setValue(x)
} else if let x = field as? Array<Float> {
_setValue(x)
} else if let x = field as? Array<String> {
_setValue(x)
} else if let x = field as? Array<AnyObject> {
_setValue(x)
} else if let x = field as? Dictionary<String, NSNumber> { // Dictionaries
_setValue(x)
} else if let x = field as? Dictionary<String, Bool> {
_setValue(x)
} else if let x = field as? Dictionary<String, Int> {
_setValue(x)
} else if let x = field as? Dictionary<String, Double> {
_setValue(x)
} else if let x = field as? Dictionary<String, Float> {
_setValue(x)
} else if let x = field as? Dictionary<String, String> {
_setValue(x)
} else if let x = field as? Dictionary<String, AnyObject> {
_setValue(x)
}
}
class func optionalBasicType<N>(field: N?, map: Map) {
if let field = field {
basicType(field, map: map)
}
}
class func object<N: Mappable>(field: N, map: Map) {
setValue(Mapper().toJSON(field), map: map)
}
class func optionalObject<N: Mappable>(field: N?, map: Map) {
if let field = field {
object(field, map: map)
}
}
class func objectArray<N: Mappable>(field: Array<N>, map: Map) {
let JSONObjects = Mapper().toJSONArray(field)
setValue(JSONObjects, map: map)
}
class func optionalObjectArray<N: Mappable>(field: Array<N>?, map: Map) {
if let field = field {
objectArray(field, map: map)
}
}
class func twoDimensionalObjectArray<N: Mappable>(field: Array<Array<N>>, map: Map) {
var array = [[[String : AnyObject]]]()
for innerArray in field {
let JSONObjects = Mapper().toJSONArray(innerArray)
array.append(JSONObjects)
}
setValue(array, map: map)
}
class func optionalTwoDimensionalObjectArray<N: Mappable>(field: Array<Array<N>>?, map: Map) {
if let field = field {
twoDimensionalObjectArray(field, map: map)
}
}
class func objectSet<N: Mappable where N: Hashable>(field: Set<N>, map: Map) {
let JSONObjects = Mapper().toJSONSet(field)
setValue(JSONObjects, map: map)
}
class func optionalObjectSet<N: Mappable where N: Hashable>(field: Set<N>?, map: Map) {
if let field = field {
objectSet(field, map: map)
}
}
class func objectDictionary<N: Mappable>(field: Dictionary<String, N>, map: Map) {
let JSONObjects = Mapper().toJSONDictionary(field)
setValue(JSONObjects, map: map)
}
class func optionalObjectDictionary<N: Mappable>(field: Dictionary<String, N>?, map: Map) {
if let field = field {
objectDictionary(field, map: map)
}
}
class func objectDictionaryOfArrays<N: Mappable>(field: Dictionary<String, [N]>, map: Map) {
let JSONObjects = Mapper().toJSONDictionaryOfArrays(field)
setValue(JSONObjects, map: map)
}
class func optionalObjectDictionaryOfArrays<N: Mappable>(field: Dictionary<String, [N]>?, map: Map) {
if let field = field {
objectDictionaryOfArrays(field, map: map)
}
}
}