@@ -56,7 +56,12 @@ open class JSONSerialization : NSObject {
56
56
*/
57
57
open class func isValidJSONObject( _ obj: Any ) -> Bool {
58
58
// TODO: - revisit this once bridging story gets fully figured out
59
- func isValidJSONObjectInternal( _ obj: Any ) -> Bool {
59
+ func isValidJSONObjectInternal( _ obj: Any ? ) -> Bool {
60
+ // Emulate the SE-0140 behavior bridging behavior for nils
61
+ guard let obj = obj else {
62
+ return true
63
+ }
64
+
60
65
// object is Swift.String, NSNull, Int, Bool, or UInt
61
66
if obj is String || obj is NSNull || obj is Int || obj is Bool || obj is UInt {
62
67
return true
@@ -72,7 +77,7 @@ open class JSONSerialization : NSObject {
72
77
}
73
78
74
79
// object is Swift.Array
75
- if let array = obj as? [ Any ] {
80
+ if let array = obj as? [ Any ? ] {
76
81
for element in array {
77
82
guard isValidJSONObjectInternal ( element) else {
78
83
return false
@@ -82,7 +87,7 @@ open class JSONSerialization : NSObject {
82
87
}
83
88
84
89
// object is Swift.Dictionary
85
- if let dictionary = obj as? [ String : Any ] {
90
+ if let dictionary = obj as? [ String : Any ? ] {
86
91
for (_, value) in dictionary {
87
92
guard isValidJSONObjectInternal ( value) else {
88
93
return false
@@ -103,7 +108,7 @@ open class JSONSerialization : NSObject {
103
108
}
104
109
105
110
// top level object must be an Swift.Array or Swift.Dictionary
106
- guard obj is [ Any ] || obj is [ String : Any ] else {
111
+ guard obj is [ Any ? ] || obj is [ String : Any ? ] else {
107
112
return false
108
113
}
109
114
@@ -308,8 +313,12 @@ private struct JSONWriter {
308
313
self . writer = writer
309
314
}
310
315
311
- mutating func serializeJSON( _ obj: Any ) throws {
316
+ mutating func serializeJSON( _ obj: Any ? ) throws {
312
317
318
+ guard let obj = obj else {
319
+ try serializeNull ( )
320
+ return
321
+ }
313
322
// For better performance, the most expensive conditions to evaluate should be last.
314
323
switch ( obj) {
315
324
case let str as String :
@@ -320,12 +329,12 @@ private struct JSONWriter {
320
329
try serializeInt ( value: num)
321
330
case let num as UInt :
322
331
try serializeUInt ( value: num)
323
- case let array as Array < Any > :
332
+ case let array as Array < Any ? > :
324
333
try serializeArray ( array)
325
- case let dict as Dictionary < AnyHashable , Any > :
334
+ case let dict as Dictionary < AnyHashable , Any ? > :
326
335
try serializeDictionary ( dict)
327
- case let null as NSNull :
328
- try serializeNull ( null )
336
+ case is NSNull :
337
+ try serializeNull ( )
329
338
case _ where _SwiftValue. store ( obj) is NSNumber :
330
339
try serializeNumber ( _SwiftValue. store ( obj) as! NSNumber )
331
340
default :
@@ -471,7 +480,7 @@ private struct JSONWriter {
471
480
}
472
481
}
473
482
474
- mutating func serializeArray( _ array: [ Any ] ) throws {
483
+ mutating func serializeArray( _ array: [ Any ? ] ) throws {
475
484
writer ( " [ " )
476
485
if pretty {
477
486
writer ( " \n " )
@@ -497,7 +506,7 @@ private struct JSONWriter {
497
506
writer ( " ] " )
498
507
}
499
508
500
- mutating func serializeDictionary( _ dict: Dictionary < AnyHashable , Any > ) throws {
509
+ mutating func serializeDictionary( _ dict: Dictionary < AnyHashable , Any ? > ) throws {
501
510
writer ( " { " )
502
511
if pretty {
503
512
writer ( " \n " )
@@ -506,7 +515,7 @@ private struct JSONWriter {
506
515
507
516
var first = true
508
517
509
- func serializeDictionaryElement( key: AnyHashable , value: Any ) throws {
518
+ func serializeDictionaryElement( key: AnyHashable , value: Any ? ) throws {
510
519
if first {
511
520
first = false
512
521
} else if pretty {
@@ -553,7 +562,7 @@ private struct JSONWriter {
553
562
writer ( " } " )
554
563
}
555
564
556
- func serializeNull( _ null : NSNull ) throws {
565
+ func serializeNull( ) throws {
557
566
writer ( " null " )
558
567
}
559
568
0 commit comments