Skip to content

Commit b046c76

Browse files
committed
Added a bunch of tests
1 parent fb15c4a commit b046c76

File tree

2 files changed

+218
-74
lines changed

2 files changed

+218
-74
lines changed

Sources/Foundation/JSONSerialization.swift

+30-8
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ open class JSONSerialization : NSObject {
212212
throw JSONError.singleFragmentFoundButNotAllowed
213213
}
214214

215-
return jsonValue.toObjcRepresentation(options: opt)
215+
return try jsonValue.toObjcRepresentation(options: opt)
216216
} catch let error as JSONError {
217217
switch error {
218218
case .unexpectedEndOfFile:
@@ -240,8 +240,30 @@ open class JSONSerialization : NSObject {
240240
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [
241241
NSDebugDescriptionErrorKey : "JSON text did not start with array or object and option to allow fragments not set."
242242
])
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+
])
245267
}
246268
} catch {
247269
preconditionFailure("Only `JSONError` expected")
@@ -1309,6 +1331,7 @@ enum JSONError: Swift.Error, Equatable {
13091331
case expectedLowSurrogateUTF8SequenceAfterHighSurrogate(in: String, index: Int)
13101332
case couldNotCreateUnicodeScalarFromUInt32(in: String, index: Int, unicodeScalarValue: UInt32)
13111333
case numberWithLeadingZero(index: Int)
1334+
case numberIsNotRepresentableInSwift(parsed: String)
13121335
case singleFragmentFoundButNotAllowed
13131336
}
13141337

@@ -1420,16 +1443,16 @@ extension JSONValue: Equatable {
14201443
}
14211444

14221445
extension JSONValue {
1423-
func toObjcRepresentation(options: JSONSerialization.ReadingOptions) -> Any {
1446+
func toObjcRepresentation(options: JSONSerialization.ReadingOptions) throws -> Any {
14241447
switch self {
14251448
case .array(let values):
1426-
let array = values.map { $0.toObjcRepresentation(options: options) }
1449+
let array = try values.map { try $0.toObjcRepresentation(options: options) }
14271450
if !options.contains(.mutableContainers) {
14281451
return array
14291452
}
14301453
return NSMutableArray(array: array, copyItems: false)
14311454
case .object(let object):
1432-
let dictionary = object.mapValues { $0.toObjcRepresentation(options: options) }
1455+
let dictionary = try object.mapValues { try $0.toObjcRepresentation(options: options) }
14331456
if !options.contains(.mutableContainers) {
14341457
return dictionary
14351458
}
@@ -1491,8 +1514,7 @@ extension JSONValue {
14911514
return NSNumber(value: doubleValue)
14921515
}
14931516

1494-
#warning("@fabian this must throw an error")
1495-
preconditionFailure("")
1517+
throw JSONError.numberIsNotRepresentableInSwift(parsed: string)
14961518
case .null:
14971519
return NSNull()
14981520
case .string(let string):

0 commit comments

Comments
 (0)