@@ -212,7 +212,7 @@ open class JSONSerialization : NSObject {
212
212
throw JSONError . singleFragmentFoundButNotAllowed
213
213
}
214
214
215
- return jsonValue. toObjcRepresentation ( options: opt)
215
+ return try jsonValue. toObjcRepresentation ( options: opt)
216
216
} catch let error as JSONError {
217
217
switch error {
218
218
case . unexpectedEndOfFile:
@@ -240,8 +240,30 @@ open class JSONSerialization : NSObject {
240
240
throw NSError ( domain: NSCocoaErrorDomain, code: CocoaError . propertyListReadCorrupt. rawValue, userInfo: [
241
241
NSDebugDescriptionErrorKey : " JSON text did not start with array or object and option to allow fragments not set. "
242
242
] )
243
- default :
244
- throw error
243
+ case . tooManyNestedArraysOrDictionaries( characterIndex: let characterIndex) :
244
+ throw NSError ( domain: NSCocoaErrorDomain, code: CocoaError . propertyListReadCorrupt. rawValue, userInfo: [
245
+ NSDebugDescriptionErrorKey : " Too many nested arrays or dictionaries around character \( characterIndex + 1 ) . "
246
+ ] )
247
+ case . invalidHexDigitSequence( let string, index: let index) :
248
+ throw NSError ( domain: NSCocoaErrorDomain, code: CocoaError . propertyListReadCorrupt. rawValue, userInfo: [
249
+ NSDebugDescriptionErrorKey : #"Invalid hex encoded sequence in " \#( string) " at \#( index) ."#
250
+ ] )
251
+ case . unescapedControlCharacterInString( ascii: let ascii, in: _, index: let index) where ascii == UInt8 ( ascii: " \\ " ) :
252
+ throw NSError ( domain: NSCocoaErrorDomain, code: CocoaError . propertyListReadCorrupt. rawValue, userInfo: [
253
+ NSDebugDescriptionErrorKey : #"Invalid escape sequence around character \#( index) ."#
254
+ ] )
255
+ case . unescapedControlCharacterInString( ascii: _, in: _, index: let index) :
256
+ throw NSError ( domain: NSCocoaErrorDomain, code: CocoaError . propertyListReadCorrupt. rawValue, userInfo: [
257
+ NSDebugDescriptionErrorKey : #"Unescaped control character around character \#( index) ."#
258
+ ] )
259
+ case . numberWithLeadingZero( index: let index) :
260
+ throw NSError ( domain: NSCocoaErrorDomain, code: CocoaError . propertyListReadCorrupt. rawValue, userInfo: [
261
+ NSDebugDescriptionErrorKey : #"Number with leading zero around character \#( index) ."#
262
+ ] )
263
+ case . numberIsNotRepresentableInSwift( parsed: let parsed) :
264
+ throw NSError ( domain: NSCocoaErrorDomain, code: CocoaError . propertyListReadCorrupt. rawValue, userInfo: [
265
+ NSDebugDescriptionErrorKey : #"Number \#( parsed) is not representable in Swift."#
266
+ ] )
245
267
}
246
268
} catch {
247
269
preconditionFailure ( " Only `JSONError` expected " )
@@ -1309,6 +1331,7 @@ enum JSONError: Swift.Error, Equatable {
1309
1331
case expectedLowSurrogateUTF8SequenceAfterHighSurrogate( in: String , index: Int )
1310
1332
case couldNotCreateUnicodeScalarFromUInt32( in: String , index: Int , unicodeScalarValue: UInt32 )
1311
1333
case numberWithLeadingZero( index: Int )
1334
+ case numberIsNotRepresentableInSwift( parsed: String )
1312
1335
case singleFragmentFoundButNotAllowed
1313
1336
}
1314
1337
@@ -1420,16 +1443,16 @@ extension JSONValue: Equatable {
1420
1443
}
1421
1444
1422
1445
extension JSONValue {
1423
- func toObjcRepresentation( options: JSONSerialization . ReadingOptions ) -> Any {
1446
+ func toObjcRepresentation( options: JSONSerialization . ReadingOptions ) throws -> Any {
1424
1447
switch self {
1425
1448
case . array( let values) :
1426
- let array = values. map { $0. toObjcRepresentation ( options: options) }
1449
+ let array = try values. map { try $0. toObjcRepresentation ( options: options) }
1427
1450
if !options. contains ( . mutableContainers) {
1428
1451
return array
1429
1452
}
1430
1453
return NSMutableArray ( array: array, copyItems: false )
1431
1454
case . object( let object) :
1432
- let dictionary = object. mapValues { $0. toObjcRepresentation ( options: options) }
1455
+ let dictionary = try object. mapValues { try $0. toObjcRepresentation ( options: options) }
1433
1456
if !options. contains ( . mutableContainers) {
1434
1457
return dictionary
1435
1458
}
@@ -1491,8 +1514,7 @@ extension JSONValue {
1491
1514
return NSNumber ( value: doubleValue)
1492
1515
}
1493
1516
1494
- #warning("@fabian this must throw an error")
1495
- preconditionFailure ( " " )
1517
+ throw JSONError . numberIsNotRepresentableInSwift ( parsed: string)
1496
1518
case . null:
1497
1519
return NSNull ( )
1498
1520
case . string( let string) :
0 commit comments