8
8
//////////////////////////////////////////////////////////////////////////////////////////////////
9
9
10
10
import Foundation
11
+ fileprivate func < < T : Comparable > ( lhs: T ? , rhs: T ? ) -> Bool {
12
+ switch ( lhs, rhs) {
13
+ case let ( l? , r? ) :
14
+ return l < r
15
+ case ( nil , _? ) :
16
+ return true
17
+ default :
18
+ return false
19
+ }
20
+ }
21
+
22
+ fileprivate func > < T : Comparable > ( lhs: T ? , rhs: T ? ) -> Bool {
23
+ switch ( lhs, rhs) {
24
+ case let ( l? , r? ) :
25
+ return l > r
26
+ default :
27
+ return rhs < lhs
28
+ }
29
+ }
30
+
11
31
12
- public enum JSONError : ErrorType {
13
- case WrongType
32
+ public enum JSONError : Error {
33
+ case wrongType
14
34
}
15
35
16
- public class JSONDecoder {
17
- var value : AnyObject ?
36
+ open class JSONDecoder {
37
+ var value : Any ?
18
38
19
39
///return the value raw
20
- public var rawValue : AnyObject ? {
40
+ open var rawValue : Any ? {
21
41
return value
22
42
}
23
43
///print the description of the JSONDecoder
24
- public var description : String {
44
+ open var description : String {
25
45
return self . print ( )
26
46
}
27
47
///convert the value to a String
28
- public var string : String ? {
48
+ open var string : String ? {
29
49
return value as? String
30
50
}
31
51
32
52
///convert the value to an Int
33
- public var integer : Int ? {
53
+ open var integer : Int ? {
34
54
return value as? Int
35
55
}
36
56
///convert the value to an UInt
37
- public var unsigned : UInt ? {
57
+ open var unsigned : UInt ? {
38
58
return value as? UInt
39
59
}
40
60
///convert the value to a Double
41
- public var double : Double ? {
61
+ open var double : Double ? {
42
62
return value as? Double
43
63
}
44
64
///convert the value to a float
45
- public var float : Float ? {
65
+ open var float : Float ? {
46
66
return value as? Float
47
67
}
48
68
///convert the value to an NSNumber
49
- public var number : NSNumber ? {
69
+ open var number : NSNumber ? {
50
70
return value as? NSNumber
51
71
}
52
72
///treat the value as a bool
53
- public var bool : Bool {
73
+ open var bool : Bool {
54
74
if let str = self . string {
55
- let lower = str. lowercaseString
75
+ let lower = str. lowercased ( )
56
76
if lower == " true " || Int ( lower) > 0 {
57
77
return true
58
78
}
@@ -66,64 +86,64 @@ public class JSONDecoder {
66
86
return false
67
87
}
68
88
//get the value if it is an error
69
- public var error : NSError ? {
89
+ open var error : NSError ? {
70
90
return value as? NSError
71
91
}
72
92
//get the value if it is a dictionary
73
- public var dictionary : Dictionary < String , JSONDecoder > ? {
93
+ open var dictionary : Dictionary < String , JSONDecoder > ? {
74
94
return value as? Dictionary < String , JSONDecoder >
75
95
}
76
96
//get the value if it is an array
77
- public var array : Array < JSONDecoder > ? {
97
+ open var array : Array < JSONDecoder > ? {
78
98
return value as? Array < JSONDecoder >
79
99
}
80
100
81
101
//get the string and have it throw if it doesn't work
82
- public func getString( ) throws -> String {
83
- guard let str = string else { throw JSONError . WrongType }
102
+ open func getString( ) throws -> String {
103
+ guard let str = string else { throw JSONError . wrongType }
84
104
return str
85
105
}
86
106
87
107
//get the int and have it throw if it doesn't work
88
- public func getInt( ) throws -> Int {
89
- guard let i = integer else { throw JSONError . WrongType }
108
+ open func getInt( ) throws -> Int {
109
+ guard let i = integer else { throw JSONError . wrongType }
90
110
return i
91
111
}
92
112
93
113
//get the unsigned and have it throw if it doesn't work
94
- public func getUnsigned( ) throws -> UInt {
95
- guard let i = unsigned else { throw JSONError . WrongType }
114
+ open func getUnsigned( ) throws -> UInt {
115
+ guard let i = unsigned else { throw JSONError . wrongType }
96
116
return i
97
117
}
98
118
99
119
//get the double and have it throw if it doesn't work
100
- public func getDouble( ) throws -> Double {
101
- guard let i = double else { throw JSONError . WrongType }
120
+ open func getDouble( ) throws -> Double {
121
+ guard let i = double else { throw JSONError . wrongType }
102
122
return i
103
123
}
104
124
105
125
//get the Float and have it throw if it doesn't work
106
- public func getFloat( ) throws -> Float {
107
- guard let i = float else { throw JSONError . WrongType }
126
+ open func getFloat( ) throws -> Float {
127
+ guard let i = float else { throw JSONError . wrongType }
108
128
return i
109
129
}
110
130
111
131
//get the number and have it throw if it doesn't work
112
- public func getFloat( ) throws -> NSNumber {
113
- guard let i = number else { throw JSONError . WrongType }
132
+ open func getFloat( ) throws -> NSNumber {
133
+ guard let i = number else { throw JSONError . wrongType }
114
134
return i
115
135
}
116
136
117
137
//get the bool and have it throw if it doesn't work
118
- public func getBool( ) throws -> Bool {
138
+ open func getBool( ) throws -> Bool {
119
139
if let _ = value as? NSNull {
120
- throw JSONError . WrongType
140
+ throw JSONError . wrongType
121
141
}
122
142
return bool
123
143
}
124
144
125
145
//pull the raw values out of an array
126
- public func getArray< T> ( inout collect: Array < T > ? ) {
146
+ open func getArray< T> ( _ collect: inout Array < T > ? ) {
127
147
if let array = value as? Array < JSONDecoder > {
128
148
if collect == nil {
129
149
collect = Array < T > ( )
@@ -136,7 +156,7 @@ public class JSONDecoder {
136
156
}
137
157
}
138
158
///pull the raw values out of a dictionary.
139
- public func getDictionary< T> ( inout collect: Dictionary < String , T > ? ) {
159
+ open func getDictionary< T> ( _ collect: inout Dictionary < String , T > ? ) {
140
160
if let dictionary = value as? Dictionary < String , JSONDecoder > {
141
161
if collect == nil {
142
162
collect = Dictionary < String , T > ( )
@@ -149,15 +169,15 @@ public class JSONDecoder {
149
169
}
150
170
}
151
171
///the init that converts everything to something nice
152
- public init ( _ raw: AnyObject , isSub: Bool = false ) {
153
- var rawObject : AnyObject = raw
154
- if let str = rawObject as? String where !isSub {
155
- rawObject = str. dataUsingEncoding ( NSUTF8StringEncoding ) !
172
+ public init ( _ raw: Any , isSub: Bool = false ) {
173
+ var rawObject : Any = raw
174
+ if let str = rawObject as? String , !isSub {
175
+ rawObject = str. data ( using : String . Encoding . utf8 ) ! as Any
156
176
}
157
- if let data = rawObject as? NSData {
158
- var response : AnyObject ?
177
+ if let data = rawObject as? Data {
178
+ var response : Any ?
159
179
do {
160
- try response = NSJSONSerialization . JSONObjectWithData ( data, options: NSJSONReadingOptions ( ) )
180
+ try response = JSONSerialization . jsonObject ( with : data, options: JSONSerialization . ReadingOptions ( ) )
161
181
rawObject = response!
162
182
}
163
183
catch let error as NSError {
@@ -167,22 +187,22 @@ public class JSONDecoder {
167
187
}
168
188
if let array = rawObject as? NSArray {
169
189
var collect = [ JSONDecoder] ( )
170
- for val : AnyObject in array {
190
+ for val : Any in array {
171
191
collect. append ( JSONDecoder ( val, isSub: true ) )
172
192
}
173
- value = collect
193
+ value = collect as Any ?
174
194
} else if let dict = rawObject as? NSDictionary {
175
195
var collect = Dictionary < String , JSONDecoder > ( )
176
196
for (key, val) in dict {
177
- collect [ key as! String ] = JSONDecoder ( val, isSub: true )
197
+ collect [ key as! String ] = JSONDecoder ( val as AnyObject , isSub: true )
178
198
}
179
- value = collect
199
+ value = collect as Any ?
180
200
} else {
181
201
value = rawObject
182
202
}
183
203
}
184
204
///Array access support
185
- public subscript( index: Int ) -> JSONDecoder {
205
+ open subscript( index: Int ) -> JSONDecoder {
186
206
get {
187
207
if let array = self . value as? NSArray {
188
208
if array. count > index {
@@ -193,36 +213,36 @@ public class JSONDecoder {
193
213
}
194
214
}
195
215
///Dictionary access support
196
- public subscript( key: String ) -> JSONDecoder {
216
+ open subscript( key: String ) -> JSONDecoder {
197
217
get {
198
218
if let dict = self . value as? NSDictionary {
199
- if let value: AnyObject = dict [ key] {
219
+ if let value: Any = dict [ key] {
200
220
return value as! JSONDecoder
201
221
}
202
222
}
203
223
return JSONDecoder ( createError ( " key: \( key) does not exist or this is not a Dictionary type " ) )
204
224
}
205
225
}
206
226
///private method to create an error
207
- func createError( text: String ) -> NSError {
227
+ func createError( _ text: String ) -> NSError {
208
228
return NSError ( domain: " JSONJoy " , code: 1002 , userInfo: [ NSLocalizedDescriptionKey: text] ) ;
209
229
}
210
230
211
231
///print the decoder in a JSON format. Helpful for debugging.
212
- public func print( ) -> String {
232
+ open func print( ) -> String {
213
233
if let arr = self . array {
214
234
var str = " [ "
215
235
for decoder in arr {
216
236
str += decoder. print ( ) + " , "
217
237
}
218
- str. removeAtIndex ( str. endIndex . advancedBy ( - 1 ) )
238
+ str. remove ( at : str. characters . index ( str . endIndex , offsetBy : - 1 ) )
219
239
return str + " ] "
220
240
} else if let dict = self . dictionary {
221
241
var str = " { "
222
242
for (key, decoder) in dict {
223
243
str += " \" \( key) \" : \( decoder. print ( ) ) , "
224
244
}
225
- str. removeAtIndex ( str. endIndex . advancedBy ( - 1 ) )
245
+ str. remove ( at : str. characters . index ( str . endIndex , offsetBy : - 1 ) )
226
246
return str + " } "
227
247
}
228
248
if let v = value {
0 commit comments