@@ -24,19 +24,6 @@ public struct NSJSONWritingOptions : OptionSetType {
24
24
public static let PrettyPrinted = NSJSONWritingOptions ( rawValue: 1 << 0 )
25
25
}
26
26
27
- enum NSJSONSerializationError : ErrorType {
28
- typealias Position = String . UnicodeScalarIndex . Distance
29
-
30
- case InvalidStringEncoding
31
- case NotAnArrayOrObject
32
- case UnterminatedString( Position )
33
- case MissingObjectKey( Position )
34
- case InvalidValue( Position )
35
- case MissingTrailingSurrogate( Position )
36
- case InvalidEscapeSequence( Position )
37
- case BadlyFormedArray( Position )
38
- case UnexpectedEndOfFile
39
- }
40
27
41
28
/* A class for converting JSON to Foundation/Swift objects and converting Foundation/Swift objects to JSON.
42
29
@@ -69,7 +56,9 @@ public class NSJSONSerialization : NSObject {
69
56
public class func JSONObjectWithData( data: NSData , options opt: NSJSONReadingOptions ) throws -> AnyObject {
70
57
71
58
guard let string = NSString ( data: data, encoding: detectEncoding ( data) ) else {
72
- throw NSJSONSerializationError . InvalidStringEncoding
59
+ throw NSError ( domain: NSCocoaErrorDomain, code: NSCocoaError . PropertyListReadCorruptError. rawValue, userInfo: [
60
+ " NSDebugDescription " : " Unable to convert data to a string using the detected encoding. The data may be corrupt. "
61
+ ] )
73
62
}
74
63
let result = _NSObjectRepresentableBridge ( try JSONObjectWithString ( string. _swiftObject) )
75
64
return result
@@ -99,7 +88,9 @@ internal extension NSJSONSerialization {
99
88
else if let ( array, _) = try JSONDeserializer . parseArray ( parser) {
100
89
return array
101
90
}
102
- throw NSJSONSerializationError . NotAnArrayOrObject
91
+ throw NSError ( domain: NSCocoaErrorDomain, code: NSCocoaError . PropertyListReadCorruptError. rawValue, userInfo: [
92
+ " NSDebugDescription " : " JSON text did not start with array or object and option to allow fragments not set. "
93
+ ] )
103
94
}
104
95
}
105
96
@@ -227,7 +218,9 @@ private struct JSONDeserializer {
227
218
static func consumeScalar( scalar: UnicodeScalar , input: UnicodeParser ) throws -> UnicodeParser ? {
228
219
switch takeScalar ( input) {
229
220
case nil :
230
- throw NSJSONSerializationError . UnexpectedEndOfFile
221
+ throw NSError ( domain: NSCocoaErrorDomain, code: NSCocoaError . PropertyListReadCorruptError. rawValue, userInfo: [
222
+ " NSDebugDescription " : " Unexpected end of file during JSON parse. "
223
+ ] )
231
224
case let ( taken, parser) ? where taken == scalar:
232
225
return parser
233
226
default :
@@ -296,18 +289,24 @@ private struct JSONDeserializer {
296
289
index = newParser. index
297
290
}
298
291
else {
299
- throw NSJSONSerializationError . InvalidEscapeSequence ( parser. distanceFromStart - 1 )
292
+ throw NSError ( domain: NSCocoaErrorDomain, code: NSCocoaError . PropertyListReadCorruptError. rawValue, userInfo: [
293
+ " NSDebugDescription " : " Invalid unicode escape sequence at position \( parser. distanceFromStart - 1 ) "
294
+ ] )
300
295
}
301
296
default :
302
297
value. append ( scalar)
303
298
}
304
299
}
305
- throw NSJSONSerializationError . UnterminatedString ( input. distanceFromStart)
300
+ throw NSError ( domain: NSCocoaErrorDomain, code: NSCocoaError . PropertyListReadCorruptError. rawValue, userInfo: [
301
+ " NSDebugDescription " : " Unexpected end of file during string parse. "
302
+ ] )
306
303
}
307
304
308
305
static func parseEscapeSequence( input: UnicodeParser ) throws -> ( UnicodeScalar , UnicodeParser ) ? {
309
306
guard let ( scalar, parser) = takeScalar ( input) else {
310
- throw NSJSONSerializationError . UnexpectedEndOfFile
307
+ throw NSError ( domain: NSCocoaErrorDomain, code: NSCocoaError . PropertyListReadCorruptError. rawValue, userInfo: [
308
+ " NSDebugDescription " : " Early end of unicode escape sequence around character "
309
+ ] )
311
310
}
312
311
switch scalar {
313
312
case UnicodeScalar ( 0x22 ) : // " quotation mark U+0022
@@ -344,7 +343,9 @@ private struct JSONDeserializer {
344
343
}
345
344
346
345
guard let ( trailCodeUnit, finalParser) = try consumeSequence ( " \\ u " , input: parser) . flatMap ( parseCodeUnit) where UTF16 . isTrailSurrogate ( trailCodeUnit) else {
347
- throw NSJSONSerializationError . MissingTrailingSurrogate ( parser. distanceFromStart)
346
+ throw NSError ( domain: NSCocoaErrorDomain, code: NSCocoaError . PropertyListReadCorruptError. rawValue, userInfo: [
347
+ " NSDebugDescription " : " Unable to convert hex escape sequence (no high character) to UTF8-encoded character at position \( parser. distanceFromStart) "
348
+ ] )
348
349
}
349
350
350
351
var utf = UTF16 ( )
@@ -441,13 +442,19 @@ private struct JSONDeserializer {
441
442
442
443
static func parseObjectMember( input: UnicodeParser ) throws -> ( String , Any , UnicodeParser ) ? {
443
444
guard let ( name, parser) = try parseString ( input) else {
444
- throw NSJSONSerializationError . MissingObjectKey ( input. distanceFromStart)
445
+ throw NSError ( domain: NSCocoaErrorDomain, code: NSCocoaError . PropertyListReadCorruptError. rawValue, userInfo: [
446
+ " NSDebugDescription " : " Missing object key at location \( input. distanceFromStart) "
447
+ ] )
445
448
}
446
449
guard let separatorParser = try consumeStructure ( StructureScalar . NameSeparator, input: parser) else {
447
- throw NSJSONSerializationError . InvalidValue ( parser. distanceFromStart)
450
+ throw NSError ( domain: NSCocoaErrorDomain, code: NSCocoaError . PropertyListReadCorruptError. rawValue, userInfo: [
451
+ " NSDebugDescription " : " Invalid value at location \( input. distanceFromStart) "
452
+ ] )
448
453
}
449
454
guard let ( value, finalParser) = try parseValue ( separatorParser) else {
450
- throw NSJSONSerializationError . InvalidValue ( separatorParser. distanceFromStart)
455
+ throw NSError ( domain: NSCocoaErrorDomain, code: NSCocoaError . PropertyListReadCorruptError. rawValue, userInfo: [
456
+ " NSDebugDescription " : " Invalid value at location \( input. distanceFromStart) "
457
+ ] )
451
458
}
452
459
453
460
return ( name, value, finalParser)
@@ -476,10 +483,14 @@ private struct JSONDeserializer {
476
483
continue
477
484
}
478
485
else {
479
- throw NSJSONSerializationError . BadlyFormedArray ( newParser. distanceFromStart)
486
+ throw NSError ( domain: NSCocoaErrorDomain, code: NSCocoaError . PropertyListReadCorruptError. rawValue, userInfo: [
487
+ " NSDebugDescription " : " Unexpected end of file while parsing array at location \( input. distanceFromStart) "
488
+ ] )
480
489
}
481
490
}
482
- throw NSJSONSerializationError . InvalidValue ( parser. distanceFromStart)
491
+ throw NSError ( domain: NSCocoaErrorDomain, code: NSCocoaError . PropertyListReadCorruptError. rawValue, userInfo: [
492
+ " NSDebugDescription " : " Unexpected end of file while parsing array at location \( input. distanceFromStart) "
493
+ ] )
483
494
}
484
495
}
485
496
}
0 commit comments