Skip to content

Commit e819ec4

Browse files
committed
Make the types consistent for new Runtime
1 parent fda6111 commit e819ec4

File tree

2 files changed

+37
-36
lines changed

2 files changed

+37
-36
lines changed

Foundation/NSJSONSerialization.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ public class NSJSONSerialization : NSObject {
7171
guard let string = NSString(data: data, encoding: detectEncoding(data)) else {
7272
throw NSJSONSerializationError.InvalidStringEncoding
7373
}
74-
return try JSONObjectWithString(string as String)
74+
let result = _NSObjectRepresentableBridge(try JSONObjectWithString(string._swiftObject))
75+
return result
7576
}
7677

7778
/* Write JSON data into a stream. The stream should be opened and configured. The return value is the number of bytes written to the stream, or 0 on error. All other behavior of this method is the same as the dataWithJSONObject:options:error: method.
@@ -90,7 +91,7 @@ public class NSJSONSerialization : NSObject {
9091
//MARK: - Deserialization
9192
internal extension NSJSONSerialization {
9293

93-
static func JSONObjectWithString(string: String) throws -> AnyObject {
94+
static func JSONObjectWithString(string: String) throws -> Any {
9495
let parser = JSONDeserializer.UnicodeParser(viewSkippingBOM: string.unicodeScalars)
9596
if let (object, _) = try JSONDeserializer.parseObject(parser) {
9697
return object
@@ -367,7 +368,7 @@ private struct JSONDeserializer {
367368

368369
//MARK: - Number parsing
369370
static let numberScalars = ".+-0123456789eE".unicodeScalars
370-
static func parseNumber(input: UnicodeParser) throws -> (AnyObject, UnicodeParser)? {
371+
static func parseNumber(input: UnicodeParser) throws -> (Double, UnicodeParser)? {
371372
let view = input.view
372373
let endIndex = view.endIndex
373374
var index = input.index
@@ -383,7 +384,7 @@ private struct JSONDeserializer {
383384
}
384385

385386
//MARK: - Value parsing
386-
static func parseValue(input: UnicodeParser) throws -> (AnyObject, UnicodeParser)? {
387+
static func parseValue(input: UnicodeParser) throws -> (Any, UnicodeParser)? {
387388
if let (value, parser) = try parseString(input) {
388389
return (value, parser)
389390
}
@@ -409,12 +410,12 @@ private struct JSONDeserializer {
409410
}
410411

411412
//MARK: - Object parsing
412-
static func parseObject(input: UnicodeParser) throws -> ([String: AnyObject], UnicodeParser)? {
413+
static func parseObject(input: UnicodeParser) throws -> ([String: Any], UnicodeParser)? {
413414
guard let beginParser = try consumeStructure(StructureScalar.BeginObject, input: input) else {
414415
return nil
415416
}
416417
var parser = beginParser
417-
var output: [String: AnyObject] = [:]
418+
var output: [String: Any] = [:]
418419
while true {
419420
if let finalParser = try consumeStructure(StructureScalar.EndObject, input: parser) {
420421
return (output, finalParser)
@@ -438,7 +439,7 @@ private struct JSONDeserializer {
438439
}
439440
}
440441

441-
static func parseObjectMember(input: UnicodeParser) throws -> (String, AnyObject, UnicodeParser)? {
442+
static func parseObjectMember(input: UnicodeParser) throws -> (String, Any, UnicodeParser)? {
442443
guard let (name, parser) = try parseString(input) else {
443444
throw NSJSONSerializationError.MissingObjectKey(input.distanceFromStart)
444445
}
@@ -453,12 +454,12 @@ private struct JSONDeserializer {
453454
}
454455

455456
//MARK: - Array parsing
456-
static func parseArray(input: UnicodeParser) throws -> ([AnyObject], UnicodeParser)? {
457+
static func parseArray(input: UnicodeParser) throws -> ([Any], UnicodeParser)? {
457458
guard let beginParser = try consumeStructure(StructureScalar.BeginArray, input: input) else {
458459
return nil
459460
}
460461
var parser = beginParser
461-
var output: [AnyObject] = []
462+
var output: [Any] = []
462463
while true {
463464
if let finalParser = try consumeStructure(StructureScalar.EndArray, input: parser) {
464465
return (output, finalParser)

TestFoundation/TestNSJSONSerialization.swift

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ extension TestNSJSONSerialization {
3838
func test_JSONObjectWithData_emptyObject() {
3939
let subject = NSData(bytes: UnsafePointer<Void>([UInt8]([0x7B, 0x7D])), length: 2)
4040

41-
let object = try! NSJSONSerialization.JSONObjectWithData(subject, options: []) as? [NSObject: AnyObject]
42-
XCTAssertEqual(object?.keys.count, 0)
41+
let object = try! NSJSONSerialization.JSONObjectWithData(subject, options: []) as? NSDictionary
42+
XCTAssertEqual(object?.count, 0)
4343
}
4444

4545
//MARK: - Encoding Detection
@@ -66,7 +66,7 @@ extension TestNSJSONSerialization {
6666
]
6767

6868
for (description, encoded) in subjects {
69-
let result = try? NSJSONSerialization.JSONObjectWithData(NSData(bytes:UnsafePointer<Void>(encoded), length: encoded.count), options: []) as? [String:String]
69+
let result = try? NSJSONSerialization.JSONObjectWithData(NSData(bytes:UnsafePointer<Void>(encoded), length: encoded.count), options: [])
7070
XCTAssertNotNil(result, description)
7171
}
7272
}
@@ -107,8 +107,8 @@ extension TestNSJSONSerialization {
107107
let subject = "{}"
108108

109109
do {
110-
let result = try NSJSONSerialization.JSONObjectWithString(subject) as? [NSObject: AnyObject]
111-
XCTAssertEqual(result?.keys.count, 0)
110+
let result = try NSJSONSerialization.JSONObjectWithString(subject) as? [String: Any]
111+
XCTAssertEqual(result?.count, 0)
112112
} catch {
113113
XCTFail("Error thrown: \(error)")
114114
}
@@ -118,9 +118,9 @@ extension TestNSJSONSerialization {
118118
let subject = "{ \"hello\": \"world\", \"swift\": \"rocks\" }"
119119

120120
do {
121-
let result = try NSJSONSerialization.JSONObjectWithString(subject) as? [String: String]
122-
XCTAssertEqual(result?["hello"], "world")
123-
XCTAssertEqual(result?["swift"], "rocks")
121+
let result = try NSJSONSerialization.JSONObjectWithString(subject) as? [String: Any]
122+
XCTAssertEqual(result?["hello"] as? String, "world")
123+
XCTAssertEqual(result?["swift"] as? String, "rocks")
124124
} catch {
125125
XCTFail("Error thrown: \(error)")
126126
}
@@ -131,7 +131,7 @@ extension TestNSJSONSerialization {
131131
let subject = "[]"
132132

133133
do {
134-
let result = try NSJSONSerialization.JSONObjectWithString(subject) as? [String]
134+
let result = try NSJSONSerialization.JSONObjectWithString(subject) as? [Any]
135135
XCTAssertEqual(result?.count, 0)
136136
} catch {
137137
XCTFail("Unexpected error: \(error)")
@@ -142,9 +142,9 @@ extension TestNSJSONSerialization {
142142
let subject = "[\"hello\", \"swift⚡️\"]"
143143

144144
do {
145-
let result = try NSJSONSerialization.JSONObjectWithString(subject) as? [String]
146-
XCTAssertEqual(result?[0], "hello")
147-
XCTAssertEqual(result?[1], "swift⚡️")
145+
let result = try NSJSONSerialization.JSONObjectWithString(subject) as? [Any]
146+
XCTAssertEqual(result?[0] as? String, "hello")
147+
XCTAssertEqual(result?[1] as? String, "swift⚡️")
148148
} catch {
149149
XCTFail("Unexpected error: \(error)")
150150
}
@@ -155,13 +155,13 @@ extension TestNSJSONSerialization {
155155
let subject = "[true, false, \"hello\", null, {}, []]"
156156

157157
do {
158-
let result = try NSJSONSerialization.JSONObjectWithString(subject) as? [AnyObject]
158+
let result = try NSJSONSerialization.JSONObjectWithString(subject) as? [Any]
159159
XCTAssertEqual(result?[0] as? Bool, true)
160160
XCTAssertEqual(result?[1] as? Bool, false)
161161
XCTAssertEqual(result?[2] as? String, "hello")
162162
XCTAssertNotNil(result?[3] as? NSNull)
163-
XCTAssertNotNil(result?[4] as? [String:String])
164-
XCTAssertNotNil(result?[5] as? [String])
163+
XCTAssertNotNil(result?[4] as? [String:Any])
164+
XCTAssertNotNil(result?[5] as? [Any])
165165
} catch {
166166
XCTFail("Unexpected error: \(error)")
167167
}
@@ -172,13 +172,13 @@ extension TestNSJSONSerialization {
172172
let subject = "[1, -1, 1.3, -1.3, 1e3, 1E-3]"
173173

174174
do {
175-
let result = try NSJSONSerialization.JSONObjectWithString(subject) as? [Double]
176-
XCTAssertEqual(result?[0], 1)
177-
XCTAssertEqual(result?[1], -1)
178-
XCTAssertEqual(result?[2], 1.3)
179-
XCTAssertEqual(result?[3], -1.3)
180-
XCTAssertEqual(result?[4], 1000)
181-
XCTAssertEqual(result?[5], 0.001)
175+
let result = try NSJSONSerialization.JSONObjectWithString(subject) as? [Any]
176+
XCTAssertEqual(result?[0] as? Double, 1)
177+
XCTAssertEqual(result?[1] as? Double, -1)
178+
XCTAssertEqual(result?[2] as? Double, 1.3)
179+
XCTAssertEqual(result?[3] as? Double, -1.3)
180+
XCTAssertEqual(result?[4] as? Double, 1000)
181+
XCTAssertEqual(result?[5] as? Double, 0.001)
182182
} catch {
183183
XCTFail("Unexpected error: \(error)")
184184
}
@@ -188,7 +188,7 @@ extension TestNSJSONSerialization {
188188
func test_deserialize_simpleEscapeSequences() {
189189
let subject = "[\"\\\"\", \"\\\\\", \"\\/\", \"\\b\", \"\\f\", \"\\n\", \"\\r\", \"\\t\"]"
190190
do {
191-
let result = try NSJSONSerialization.JSONObjectWithString(subject) as? [String]
191+
let result = (try NSJSONSerialization.JSONObjectWithString(subject) as? [Any])?.flatMap { $0 as? String }
192192
XCTAssertEqual(result?[0], "\"")
193193
XCTAssertEqual(result?[1], "\\")
194194
XCTAssertEqual(result?[2], "/")
@@ -205,8 +205,8 @@ extension TestNSJSONSerialization {
205205
func test_deserialize_unicodeEscapeSequence() {
206206
let subject = "[\"\\u2728\"]"
207207
do {
208-
let result = try NSJSONSerialization.JSONObjectWithString(subject) as? [String]
209-
XCTAssertEqual(result?[0], "")
208+
let result = try NSJSONSerialization.JSONObjectWithString(subject) as? [Any]
209+
XCTAssertEqual(result?[0] as? String, "")
210210
} catch {
211211
XCTFail("Unexpected error: \(error)")
212212
}
@@ -215,8 +215,8 @@ extension TestNSJSONSerialization {
215215
func test_deserialize_unicodeSurrogatePairEscapeSequence() {
216216
let subject = "[\"\\uD834\\udd1E\"]"
217217
do {
218-
let result = try NSJSONSerialization.JSONObjectWithString(subject) as? [String]
219-
XCTAssertEqual(result?[0], "\u{1D11E}")
218+
let result = try NSJSONSerialization.JSONObjectWithString(subject) as? [Any]
219+
XCTAssertEqual(result?[0] as? String, "\u{1D11E}")
220220
} catch {
221221
XCTFail("Unexpected error: \(error)")
222222
}

0 commit comments

Comments
 (0)