@@ -150,22 +150,22 @@ public enum JSON:
150150 public var rawValue : Any {
151151 switch self {
152152 case let . array( array) :
153- return array. map ( \. rawValue)
153+ array. map ( \. rawValue)
154154
155155 case let . boolean( bool) :
156- return bool
156+ bool
157157
158158 case let . dictionary( dictionary) :
159- return dictionary. mapValues ( \. rawValue)
159+ dictionary. mapValues ( \. rawValue)
160160
161161 case . null:
162- return NSNull ( )
162+ NSNull ( )
163163
164164 case let . number( number) :
165- return number
165+ number
166166
167167 case let . string( string) :
168- return string
168+ string
169169 }
170170 }
171171
@@ -203,9 +203,9 @@ public enum JSON:
203203 /// underlying type is `.number`, otherwise `nil`.
204204 public var integerValue : Int ? {
205205 if let double = rawValue as? Double {
206- return Int ( double)
206+ Int ( double)
207207 } else {
208- return nil
208+ nil
209209 }
210210 }
211211
@@ -216,6 +216,19 @@ public enum JSON:
216216 return string
217217 }
218218
219+ /// Returns `true` if the receiver is an array, otherwise `false`.
220+ public var isArray : Bool {
221+ guard case . array = self else { return false }
222+ return true
223+ }
224+
225+ /// Returns `true` if the receiver is a JSON object (a "dictionary"),
226+ /// otherwise `false`.
227+ public var isObject : Bool {
228+ guard case . dictionary = self else { return false }
229+ return true
230+ }
231+
219232 public var description : String {
220233 String ( describing: rawValue)
221234 }
@@ -280,25 +293,25 @@ extension JSON: Equatable {
280293 public static func == ( _ arg1: JSON , _ arg2: JSON ) -> Bool {
281294 switch ( arg1, arg2) {
282295 case let ( . array( one) , . array( two) ) :
283- return one == two
296+ one == two
284297
285298 case let ( . boolean( one) , . boolean( two) ) :
286- return one == two
299+ one == two
287300
288301 case let ( . dictionary( one) , . dictionary( two) ) :
289- return one == two
302+ one == two
290303
291304 case let ( . number( one) , . number( two) ) :
292- return one == two
305+ one == two
293306
294307 case ( . null, . null) :
295- return true
308+ true
296309
297310 case let ( . string( one) , . string( two) ) :
298- return one == two
311+ one == two
299312
300313 default :
301- return false
314+ false
302315 }
303316 }
304317}
@@ -508,9 +521,9 @@ public extension JSON? {
508521 /// underlying type is `.number`, otherwise `nil`.
509522 var integerValue : Int ? {
510523 if let double = self ? . rawValue as? Double {
511- return Int ( double)
524+ Int ( double)
512525 } else {
513- return nil
526+ nil
514527 }
515528 }
516529
@@ -521,6 +534,19 @@ public extension JSON? {
521534 return string
522535 }
523536
537+ /// Returns `true` if the receiver is an array, otherwise `false`.
538+ var isArray : Bool {
539+ guard let self, self . isArray else { return false }
540+ return true
541+ }
542+
543+ /// Returns `true` if the receiver is a JSON object (a "dictionary"),
544+ /// otherwise `false`.
545+ var isObject : Bool {
546+ guard let self, self . isObject else { return false }
547+ return true
548+ }
549+
524550 static func == ( _ arg1: JSON , _ arg2: JSON ? ) -> Bool {
525551 guard let arg2 else { return false }
526552 return arg1 == arg2
@@ -620,7 +646,6 @@ private extension Any? {
620646 case let e as NSNumber where e. isBool: return . boolean( e. boolValue)
621647 case let e as NSNumber : return . number( e. doubleValue)
622648 case let e as String : return . string( e)
623-
624649 // The above cases should catch everything, but, in case they
625650 // don't, we try remaining types here.
626651 case let e as Bool : return . boolean( e)
@@ -637,9 +662,7 @@ private extension Any? {
637662 case let e as UInt16 : return . number( Double ( e) )
638663 case let e as UInt32 : return . number( Double ( e) )
639664 case let e as UInt64 : return . number( Double ( e) )
640-
641665 case let e as JSON : return e
642-
643666 default : return nil
644667 }
645668 }
0 commit comments