@@ -114,6 +114,14 @@ public class JSONDecoder {
114
114
return i
115
115
}
116
116
117
+ //get the bool and have it throw if it doesn't work
118
+ public func getBool( ) throws -> Bool {
119
+ if let _ = value as? NSNull {
120
+ throw JSONError . WrongType
121
+ }
122
+ return bool
123
+ }
124
+
117
125
//pull the raw values out of an array
118
126
public func getArray< T> ( inout collect: Array < T > ? ) {
119
127
if let array = value as? Array < JSONDecoder > {
@@ -141,8 +149,11 @@ public class JSONDecoder {
141
149
}
142
150
}
143
151
///the init that converts everything to something nice
144
- public init ( _ raw: AnyObject ) {
152
+ public init ( _ raw: AnyObject , isSub : Bool = false ) {
145
153
var rawObject : AnyObject = raw
154
+ if let str = rawObject as? String where !isSub {
155
+ rawObject = str. dataUsingEncoding ( NSUTF8StringEncoding) !
156
+ }
146
157
if let data = rawObject as? NSData {
147
158
var response : AnyObject ?
148
159
do {
@@ -157,13 +168,13 @@ public class JSONDecoder {
157
168
if let array = rawObject as? NSArray {
158
169
var collect = [ JSONDecoder] ( )
159
170
for val : AnyObject in array {
160
- collect. append ( JSONDecoder ( val) )
171
+ collect. append ( JSONDecoder ( val, isSub : true ) )
161
172
}
162
173
value = collect
163
174
} else if let dict = rawObject as? NSDictionary {
164
175
var collect = Dictionary < String , JSONDecoder > ( )
165
176
for (key, val) in dict {
166
- collect [ key as! String ] = JSONDecoder ( val)
177
+ collect [ key as! String ] = JSONDecoder ( val, isSub : true )
167
178
}
168
179
value = collect
169
180
} else {
@@ -214,13 +225,13 @@ public class JSONDecoder {
214
225
str. removeAtIndex ( str. endIndex. advancedBy ( - 1 ) )
215
226
return str + " } "
216
227
}
217
- if value != nil {
218
- if let _ = self . string {
219
- return " \" \( value! ) \" "
228
+ if let v = value {
229
+ if let s = self . string {
230
+ return " \" \( s ) \" "
220
231
} else if let _ = value as? NSNull {
221
232
return " null "
222
233
}
223
- return " \( value! ) "
234
+ return " \( v ) "
224
235
}
225
236
return " "
226
237
}
0 commit comments