Permalink
Browse files

Drop NS from JSONSerialization

  • Loading branch information...
phausler committed May 23, 2016
1 parent 5f7f6f1 commit b914527d4d560602afc90da29254e1f1571672dd
Showing with 47 additions and 45 deletions.
  1. +21 −19 Foundation/NSJSONSerialization.swift
  2. +26 −26 TestFoundation/TestNSJSONSerialization.swift
@@ -13,20 +13,22 @@
import Glibc
#endif
public struct NSJSONReadingOptions : OptionSet {
public let rawValue : UInt
public init(rawValue: UInt) { self.rawValue = rawValue }
public static let mutableContainers = NSJSONReadingOptions(rawValue: 1 << 0)
public static let mutableLeaves = NSJSONReadingOptions(rawValue: 1 << 1)
public static let allowFragments = NSJSONReadingOptions(rawValue: 1 << 2)
}
extension JSONSerialization {
public struct ReadingOptions : OptionSet {
public let rawValue : UInt
public init(rawValue: UInt) { self.rawValue = rawValue }
public static let mutableContainers = ReadingOptions(rawValue: 1 << 0)
public static let mutableLeaves = ReadingOptions(rawValue: 1 << 1)
public static let allowFragments = ReadingOptions(rawValue: 1 << 2)
}
public struct NSJSONWritingOptions : OptionSet {
public let rawValue : UInt
public init(rawValue: UInt) { self.rawValue = rawValue }
public static let prettyPrinted = NSJSONWritingOptions(rawValue: 1 << 0)
public struct WritingOptions : OptionSet {
public let rawValue : UInt
public init(rawValue: UInt) { self.rawValue = rawValue }
public static let prettyPrinted = WritingOptions(rawValue: 1 << 0)
}
}
@@ -40,7 +42,7 @@ public struct NSJSONWritingOptions : OptionSet {
- `NSNumber`s are not NaN or infinity
*/
public class NSJSONSerialization : NSObject {
public class JSONSerialization : NSObject {
/* Determines whether the given object can be converted to JSON.
Other rules may apply. Calling this method or attempting a conversion are the definitive ways
@@ -96,7 +98,7 @@ public class NSJSONSerialization : NSObject {
/* Generate JSON data from a Foundation object. If the object will not produce valid JSON then an exception will be thrown. Setting the NSJSONWritingPrettyPrinted option will generate JSON with whitespace designed to make the output more readable. If that option is not set, the most compact possible JSON will be generated. If an error occurs, the error parameter will be set and the return value will be nil. The resulting data is a encoded in UTF-8.
*/
public class func data(withJSONObject obj: AnyObject, options opt: NSJSONWritingOptions = []) throws -> NSData {
public class func data(withJSONObject obj: AnyObject, options opt: WritingOptions = []) throws -> NSData {
guard obj is NSArray || obj is NSDictionary else {
throw NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.PropertyListReadCorruptError.rawValue, userInfo: [
"NSDebugDescription" : "Top-level object was not NSArray or NSDictionary"
@@ -123,7 +125,7 @@ public class NSJSONSerialization : NSObject {
The data must be in one of the 5 supported encodings listed in the JSON specification: UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE. The data may or may not have a BOM. The most efficient encoding to use for parsing is UTF-8, so if you have a choice in encoding the data passed to this method, use UTF-8.
*/
/// - Experiment: Note that the return type of this function is different than on Darwin Foundation (Any instead of AnyObject). This is likely to change once we have a more complete story for bridging in place.
public class func jsonObject(with data: NSData, options opt: NSJSONReadingOptions = []) throws -> Any {
public class func jsonObject(with data: NSData, options opt: ReadingOptions = []) throws -> Any {
let bytes = UnsafePointer<UInt8>(data.bytes)
let encoding: NSStringEncoding
@@ -155,20 +157,20 @@ public class NSJSONSerialization : NSObject {
/* 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.
*/
public class func writeJSONObject(_ obj: AnyObject, toStream stream: NSOutputStream, options opt: NSJSONWritingOptions) throws -> Int {
public class func writeJSONObject(_ obj: AnyObject, toStream stream: NSOutputStream, options opt: WritingOptions) throws -> Int {
NSUnimplemented()
}
/* Create a JSON object from JSON data stream. The stream should be opened and configured. All other behavior of this method is the same as the JSONObjectWithData:options:error: method.
*/
public class func jsonObject(with stream: NSInputStream, options opt: NSJSONReadingOptions = []) throws -> AnyObject {
public class func jsonObject(with stream: NSInputStream, options opt: ReadingOptions = []) throws -> AnyObject {
NSUnimplemented()
}
}
//MARK: - Encoding Detection
internal extension NSJSONSerialization {
internal extension JSONSerialization {
/// Detect the encoding format of the NSData contents
class func detectEncoding(_ bytes: UnsafePointer<UInt8>, _ length: Int) -> NSStringEncoding {
@@ -47,7 +47,7 @@ extension TestNSJSONSerialization {
func test_JSONObjectWithData_emptyObject() {
let subject = NSData(bytes: UnsafePointer<Void>([UInt8]([0x7B, 0x7D])), length: 2)
let object = try! NSJSONSerialization.jsonObject(with: subject, options: []) as? [String:Any]
let object = try! JSONSerialization.jsonObject(with: subject, options: []) as? [String:Any]
XCTAssertEqual(object?.count, 0)
}
@@ -75,7 +75,7 @@ extension TestNSJSONSerialization {
]
for (description, encoded) in subjects {
let result = try? NSJSONSerialization.jsonObject(with: NSData(bytes:UnsafePointer<Void>(encoded), length: encoded.count), options: [])
let result = try? JSONSerialization.jsonObject(with: NSData(bytes:UnsafePointer<Void>(encoded), length: encoded.count), options: [])
XCTAssertNotNil(result, description)
}
}
@@ -125,7 +125,7 @@ extension TestNSJSONSerialization {
XCTFail("Unable to convert string to data")
return
}
let t = try NSJSONSerialization.jsonObject(with: data, options: [])
let t = try JSONSerialization.jsonObject(with: data, options: [])
let result = t as? [String: Any]
XCTAssertEqual(result?.count, 0)
} catch {
@@ -141,7 +141,7 @@ extension TestNSJSONSerialization {
XCTFail("Unable to convert string to data")
return
}
let result = try NSJSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
let result = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
XCTAssertEqual(result?["hello"] as? String, "world")
XCTAssertEqual(result?["swift"] as? String, "rocks")
}
@@ -158,7 +158,7 @@ extension TestNSJSONSerialization {
XCTFail("Unable to convert string to data")
return
}
let result = try NSJSONSerialization.jsonObject(with: data, options: []) as? [String : Any]
let result = try JSONSerialization.jsonObject(with: data, options: []) as? [String : Any]
XCTAssertEqual(result?["title"] as? String, " hello world!!")
} catch{
XCTFail("Error thrown: \(error)")
@@ -176,7 +176,7 @@ extension TestNSJSONSerialization {
XCTFail("Unable to convert string to data")
return
}
let result = try NSJSONSerialization.jsonObject(with: data, options: []) as? [Any]
let result = try JSONSerialization.jsonObject(with: data, options: []) as? [Any]
XCTAssertEqual(result?.count, 0)
} catch {
XCTFail("Unexpected error: \(error)")
@@ -192,7 +192,7 @@ extension TestNSJSONSerialization {
XCTFail("Unable to convert string to data")
return
}
let result = try NSJSONSerialization.jsonObject(with: data, options: []) as? [Any]
let result = try JSONSerialization.jsonObject(with: data, options: []) as? [Any]
XCTAssertEqual(result?[0] as? String, "hello")
XCTAssertEqual(result?[1] as? String, "swift⚡️")
}
@@ -211,7 +211,7 @@ extension TestNSJSONSerialization {
XCTFail("Unable to convert string to data")
return
}
let result = try NSJSONSerialization.jsonObject(with: data, options: []) as? [Any]
let result = try JSONSerialization.jsonObject(with: data, options: []) as? [Any]
XCTAssertEqual(result?[0] as? String, "unicode")
XCTAssertEqual(result?[1] as? String, "Ģ")
XCTAssertEqual(result?[2] as? String, "😢")
@@ -231,7 +231,7 @@ extension TestNSJSONSerialization {
XCTFail("Unable to convert string to data")
return
}
let result = try NSJSONSerialization.jsonObject(with: data, options: []) as? [Any]
let result = try JSONSerialization.jsonObject(with: data, options: []) as? [Any]
XCTAssertEqual(result?[0] as? Bool, true)
XCTAssertEqual(result?[1] as? Bool, false)
XCTAssertEqual(result?[2] as? String, "hello")
@@ -254,7 +254,7 @@ extension TestNSJSONSerialization {
XCTFail("Unable to convert string to data")
return
}
let result = try NSJSONSerialization.jsonObject(with: data, options: []) as? [Any]
let result = try JSONSerialization.jsonObject(with: data, options: []) as? [Any]
XCTAssertEqual(result?[0] as? Int, 1)
XCTAssertEqual(result?[1] as? Int, -1)
XCTAssertEqual(result?[2] as? Double, 1.3)
@@ -275,7 +275,7 @@ extension TestNSJSONSerialization {
XCTFail("Unable to convert string to data")
return
}
let res = try NSJSONSerialization.jsonObject(with: data, options: []) as? [Any]
let res = try JSONSerialization.jsonObject(with: data, options: []) as? [Any]
let result = res?.flatMap { $0 as? String }
XCTAssertEqual(result?[0], "\"")
XCTAssertEqual(result?[1], "\\")
@@ -297,7 +297,7 @@ extension TestNSJSONSerialization {
XCTFail("Unable to convert string to data")
return
}
let result = try NSJSONSerialization.jsonObject(with: data, options: []) as? [Any]
let result = try JSONSerialization.jsonObject(with: data, options: []) as? [Any]
XCTAssertEqual(result?[0] as? String, "")
} catch {
XCTFail("Unexpected error: \(error)")
@@ -311,7 +311,7 @@ extension TestNSJSONSerialization {
XCTFail("Unable to convert string to data")
return
}
let result = try NSJSONSerialization.jsonObject(with: data, options: []) as? [Any]
let result = try JSONSerialization.jsonObject(with: data, options: []) as? [Any]
XCTAssertEqual(result?[0] as? String, "\u{1D11E}")
} catch {
XCTFail("Unexpected error: \(error)")
@@ -327,7 +327,7 @@ extension TestNSJSONSerialization {
XCTFail("Unable to convert string to data")
return
}
let result = try NSJSONSerialization.jsonObject(with: data, options: .allowFragments) as? Int
let result = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? Int
XCTAssertEqual(result, 3)
}
} catch {
@@ -344,7 +344,7 @@ extension TestNSJSONSerialization {
XCTFail("Unable to convert string to data")
return
}
let _ = try NSJSONSerialization.jsonObject(with: data, options: [])
let _ = try JSONSerialization.jsonObject(with: data, options: [])
XCTFail("Expected error: UnterminatedString")
} catch {
// Passing case; the object as unterminated
@@ -359,7 +359,7 @@ extension TestNSJSONSerialization {
XCTFail("Unable to convert string to data")
return
}
let _ = try NSJSONSerialization.jsonObject(with: data, options: [])
let _ = try JSONSerialization.jsonObject(with: data, options: [])
XCTFail("Expected error: Missing key for value")
} catch {
// Passing case; the key was missing for a value
@@ -374,7 +374,7 @@ extension TestNSJSONSerialization {
XCTFail("Unable to convert string to data")
return
}
let _ = try NSJSONSerialization.jsonObject(with: data, options: [])
let _ = try JSONSerialization.jsonObject(with: data, options: [])
XCTFail("Expected error: Unexpected end of file")
} catch {
// Success
@@ -389,7 +389,7 @@ extension TestNSJSONSerialization {
XCTFail("Unable to convert string to data")
return
}
let _ = try NSJSONSerialization.jsonObject(with: data, options: [])
let _ = try JSONSerialization.jsonObject(with: data, options: [])
XCTFail("Expected error: Invalid value")
} catch {
// Passing case; the value is invalid
@@ -404,7 +404,7 @@ extension TestNSJSONSerialization {
XCTFail("Unable to convert string to data")
return
}
let _ = try NSJSONSerialization.jsonObject(with: data, options: [])
let _ = try JSONSerialization.jsonObject(with: data, options: [])
XCTFail("Expected error: Invalid value")
} catch {
// passing case the value is invalid
@@ -419,7 +419,7 @@ extension TestNSJSONSerialization {
XCTFail("Unable to convert string to data")
return
}
let _ = try NSJSONSerialization.jsonObject(with: data, options: [])
let _ = try JSONSerialization.jsonObject(with: data, options: [])
XCTFail("Expected error: Invalid value")
} catch {
// Passing case; the element in the array is missing
@@ -434,7 +434,7 @@ extension TestNSJSONSerialization {
XCTFail("Unable to convert string to data")
return
}
let _ = try NSJSONSerialization.jsonObject(with: data, options: [])
let _ = try JSONSerialization.jsonObject(with: data, options: [])
XCTFail("Expected error: Badly formed array")
} catch {
// Passing case; the array is malformed
@@ -449,7 +449,7 @@ extension TestNSJSONSerialization {
XCTFail("Unable to convert string to data")
return
}
let _ = try NSJSONSerialization.jsonObject(with: data, options: [])
let _ = try JSONSerialization.jsonObject(with: data, options: [])
XCTFail("Expected error: Invalid escape sequence")
} catch {
// Passing case; the escape sequence is invalid
@@ -463,7 +463,7 @@ extension TestNSJSONSerialization {
XCTFail("Unable to convert string to data")
return
}
let _ = try NSJSONSerialization.jsonObject(with: data, options: []) as? [String]
let _ = try JSONSerialization.jsonObject(with: data, options: []) as? [String]
XCTFail("Expected error: Missing Trailing Surrogate")
} catch {
// Passing case; the unicode character is malformed
@@ -525,7 +525,7 @@ extension TestNSJSONSerialization {
)
]
for testCase in trueJSON {
XCTAssertTrue(NSJSONSerialization.isValidJSONObject(testCase))
XCTAssertTrue(JSONSerialization.isValidJSONObject(testCase))
}
}
@@ -574,7 +574,7 @@ extension TestNSJSONSerialization {
)
]
for testCase in falseJSON {
XCTAssertFalse(NSJSONSerialization.isValidJSONObject(testCase))
XCTAssertFalse(JSONSerialization.isValidJSONObject(testCase))
}
}
@@ -597,7 +597,7 @@ extension TestNSJSONSerialization {
}
func trySerialize(_ obj: AnyObject) throws -> String {
let data = try NSJSONSerialization.data(withJSONObject: obj, options: [])
let data = try JSONSerialization.data(withJSONObject: obj, options: [])
guard let string = NSString(data: data, encoding: NSUTF8StringEncoding) else {
XCTFail("Unable to create string")
return ""

0 comments on commit b914527

Please sign in to comment.