forked from isair/JSONHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONHelper.swift
More file actions
324 lines (285 loc) · 9.05 KB
/
JSONHelper.swift
File metadata and controls
324 lines (285 loc) · 9.05 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//
// JSONHelper.swift
//
// Created by Baris Sencan on 28/08/2014.
// Copyright 2014 Baris Sencan
//
// Distributed under the permissive zlib license
// Get the latest version from here:
//
// https://github.com/isair/JSONHelper
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
import Foundation
/// A type of dictionary that only uses strings for keys and can contain any
/// type of object as a value.
public typealias JSONDictionary = [String: AnyObject]
/// Operator for use in deserialization operations.
infix operator <-- { associativity right precedence 150 }
/// MARK: Primitive Type Deserialization
// For optionals.
public func <-- <T>(inout property: T?, value: AnyObject?) -> T? {
var newValue: T?
if let unwrappedValue: AnyObject = value {
// We unwrapped the given value successfully, try to convert.
if let convertedValue = unwrappedValue as? T {
// Convert by just type-casting.
newValue = convertedValue
} else {
// Convert by processing the value first.
switch property {
case is Int?:
if unwrappedValue is String {
if let intValue = "\(unwrappedValue)".toInt() {
newValue = intValue as? T
}
}
case is NSURL?:
newValue = NSURL(string: "\(unwrappedValue)") as? T
case is NSDate?:
if let timestamp = value as? Int {
newValue = NSDate(timeIntervalSince1970: Double(timestamp)) as? T
} else if let timestamp = value as? Double {
newValue = NSDate(timeIntervalSince1970: timestamp) as? T
} else if let timestamp = value as? NSNumber {
newValue = NSDate(timeIntervalSince1970: timestamp.doubleValue) as? T
}
default:
break
}
}
}
property = newValue
return property
}
// For non-optionals.
public func <-- <T>(inout property: T, value: AnyObject?) -> T {
var newValue: T?
newValue <-- value
if let newValue = newValue { property = newValue }
return property
}
// Special handling for value and format pair to NSDate conversion.
public func <-- (inout property: NSDate?, valueAndFormat: (value: AnyObject?, format: AnyObject?)) -> NSDate? {
var newValue: NSDate?
if let dateString = valueAndFormat.value as? String {
if let formatString = valueAndFormat.format as? String {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = formatString
if let newDate = dateFormatter.dateFromString(dateString) {
newValue = newDate
}
}
}
property = newValue
return property
}
public func <-- (inout property: NSDate, valueAndFormat: (value: AnyObject?, format: AnyObject?)) -> NSDate {
var date: NSDate?
date <-- valueAndFormat
if let date = date { property = date }
return property
}
// MARK: Primitive Array Deserialization
public func <-- (inout array: [String]?, value: AnyObject?) -> [String]? {
if let stringArray = value as? [String] {
array = stringArray
} else {
array = nil
}
return array
}
public func <-- (inout array: [String], value: AnyObject?) -> [String] {
var newValue: [String]?
newValue <-- value
if let newValue = newValue { array = newValue }
return array
}
public func <-- (inout array: [Int]?, value: AnyObject?) -> [Int]? {
if let intArray = value as? [Int] {
array = intArray
} else {
array = nil
}
return array
}
public func <-- (inout array: [Int], value: AnyObject?) -> [Int] {
var newValue: [Int]?
newValue <-- value
if let newValue = newValue { array = newValue }
return array
}
public func <-- (inout array: [Float]?, value: AnyObject?) -> [Float]? {
if let floatArray = value as? [Float] {
array = floatArray
} else {
array = nil
}
return array
}
public func <-- (inout array: [Float], value: AnyObject?) -> [Float] {
var newValue: [Float]?
newValue <-- value
if let newValue = newValue { array = newValue }
return array
}
public func <-- (inout array: [Double]?, value: AnyObject?) -> [Double]? {
if let doubleArrayDoubleExcitement = value as? [Double] {
array = doubleArrayDoubleExcitement
} else {
array = nil
}
return array
}
public func <-- (inout array: [Double], value: AnyObject?) -> [Double] {
var newValue: [Double]?
newValue <-- value
if let newValue = newValue { array = newValue }
return array
}
public func <-- (inout array: [Bool]?, value: AnyObject?) -> [Bool]? {
if let boolArray = value as? [Bool] {
array = boolArray
} else {
array = nil
}
return array
}
public func <-- (inout array: [Bool], value: AnyObject?) -> [Bool] {
var newValue: [Bool]?
newValue <-- value
if let newValue = newValue { array = newValue }
return array
}
public func <-- (inout array: [NSURL]?, value: AnyObject?) -> [NSURL]? {
if let stringURLArray = value as? [String] {
array = [NSURL]()
for stringURL in stringURLArray {
if let url = NSURL(string: stringURL) {
array!.append(url)
}
}
} else {
array = nil
}
return array
}
public func <-- (inout array: [NSURL], value: AnyObject?) -> [NSURL] {
var newValue: [NSURL]?
newValue <-- value
if let newValue = newValue { array = newValue }
return array
}
public func <-- (inout array: [NSDate]?, valueAndFormat: (AnyObject?, AnyObject?)) -> [NSDate]? {
var newValue: [NSDate]?
if let dateStringArray = valueAndFormat.0 as? [String] {
if let formatString = valueAndFormat.1 as? String {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = formatString
newValue = [NSDate]()
for dateString in dateStringArray {
if let date = dateFormatter.dateFromString(dateString) {
newValue!.append(date)
}
}
}
}
array = newValue
return array
}
public func <-- (inout array: [NSDate], valueAndFormat: (AnyObject?, AnyObject?)) -> [NSDate] {
var newValue: [NSDate]?
newValue <-- valueAndFormat
if let newValue = newValue { array = newValue }
return array
}
public func <-- (inout array: [NSDate]?, value: AnyObject?) -> [NSDate]? {
if let timestamps = value as? [AnyObject] {
array = [NSDate]()
for timestamp in timestamps {
var date: NSDate?
date <-- timestamp
if date != nil { array!.append(date!) }
}
} else {
array = nil
}
return array
}
public func <-- (inout array: [NSDate], value: AnyObject?) -> [NSDate] {
var newValue: [NSDate]?
newValue <-- value
if let newValue = newValue { array = newValue }
return array
}
// MARK: Custom Object Deserialization
public protocol Deserializable {
init(data: JSONDictionary)
}
public func <-- <T: Deserializable>(inout instance: T?, dataObject: AnyObject?) -> T? {
if let data = dataObject as? JSONDictionary {
instance = T(data: data)
} else {
instance = nil
}
return instance
}
public func <-- <T: Deserializable>(inout instance: T, dataObject: AnyObject?) -> T {
var newInstance: T?
newInstance <-- dataObject
if let newInstance = newInstance { instance = newInstance }
return instance
}
// MARK: Custom Object Array Deserialization
public func <-- <T: Deserializable>(inout array: [T]?, dataObject: AnyObject?) -> [T]? {
if let dataArray = dataObject as? [JSONDictionary] {
array = [T]()
for data in dataArray {
array!.append(T(data: data))
}
} else {
array = nil
}
return array
}
public func <-- <T: Deserializable>(inout array: [T], dataObject: AnyObject?) -> [T] {
var newArray: [T]?
newArray <-- dataObject
if let newArray = newArray { array = newArray }
return array
}
// MARK: JSON String Deserialization
private func dataStringToObject(dataString: String) -> AnyObject? {
var data: NSData = dataString.dataUsingEncoding(NSUTF8StringEncoding)!
var error: NSError?
return NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(0), error: &error)
}
public func <-- <T: Deserializable>(inout instance: T?, dataString: String) -> T? {
return instance <-- dataStringToObject(dataString)
}
public func <-- <T: Deserializable>(inout instance: T, dataString: String) -> T {
return instance <-- dataStringToObject(dataString)
}
public func <-- <T: Deserializable>(inout array: [T]?, dataString: String) -> [T]? {
return array <-- dataStringToObject(dataString)
}
public func <-- <T: Deserializable>(inout array: [T], dataString: String) -> [T] {
return array <-- dataStringToObject(dataString)
}