Skip to content

JSON serializer and tests. #291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 178 additions & 1 deletion Foundation/NSJSONSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,26 @@ 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 dataWithJSONObject(obj: AnyObject, options opt: NSJSONWritingOptions) throws -> NSData {
NSUnimplemented()
guard obj is NSArray || obj is NSDictionary else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this going to fail for e.g. [String] or [String:Any]? Because those are not automatically bridged to NSArray and NSDictionary on Linux, so unless things have changed, I don't think this will work on Linux.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, this is a consequence of the current bridging situation. There is also the fact that the documentation of NSJSONSerialization says that the top-level objects must be NSArray or NSDictionary - so perhaps the documentation needs updating as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the signature is for AnyObject not Any as well...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, but the parsing code uses native Swift types like [Any] and [String: Any], which I think is the right way to do it. I think here you should detect native Swift containers as well, so that it works properly both on OS X and Linux.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'NSDictionary' or 'NSArray' language is just a reflection of the JSON spec, which says that the top level must be an 'Object' or 'Array'. We can use a Swift dictionary or array here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@parkera That's how I read it too. Now, if @ianpartridge does want to detect the native Swift containers as well, what would you recommend is the right approach? I wrote my own JSON parser Jay, where I had to opt in for ugly (almost) exhaustive checking of all Array/Dictionary types, because of the lack of automatic bridging on Linux. Is there a better way to do it that I missed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a recursive descent of the input and checking all types along the way is the most reasonable way to do this for now. There's a lot of libraries out there that are exploring different avenues for parsing JSON in a type-safe way, but I haven't seen a clear winner among the approaches yet.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, that makes sense to tell a String from a Int, but iterating through all possible [String: String], [String: Int], [String: Double`, ... is less than ideal I think. I'm fine with using this for detecting different JSON-compatible types, but is it really the best way to detect different Array and Dictionary types?

throw NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.PropertyListReadCorruptError.rawValue, userInfo: [
"NSDebugDescription" : "Top-level object was not NSArray or NSDictionary"
])
}

let result = NSMutableData()

var writer = JSONWriter(
pretty: opt.contains(.PrettyPrinted),
writer: { (str: String?) in
if let str = str {
result.appendBytes(str.bridge().cStringUsingEncoding(NSUTF8StringEncoding), length: str.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))
}
}
)

try writer.serializeJSON(obj)

return result
}

/* Create a Foundation object from JSON data. Set the NSJSONReadingAllowFragments option if the parser should allow top-level objects that are not an NSArray or NSDictionary. Setting the NSJSONReadingMutableContainers option will make the parser generate mutable NSArrays and NSDictionaries. Setting the NSJSONReadingMutableLeaves option will make the parser generate mutable NSString objects. If an error occurs during the parse, then the error parameter will be set and the result will be nil.
Expand Down Expand Up @@ -207,6 +226,164 @@ internal extension NSJSONSerialization {
}
}

//MARK: - JSONSerializer
private struct JSONWriter {

var indent = 0
let pretty: Bool
let writer: (String?) -> Void

init(pretty: Bool = false, writer: (String?) -> Void) {
self.pretty = pretty
self.writer = writer
}

mutating func serializeJSON(obj: AnyObject) throws {
if let str = obj as? NSString {
try serializeString(str)
}
else if let num = obj as? NSNumber {
try serializeNumber(num)
}
else if let array = obj as? NSArray {
try serializeArray(array)
}
else if let dict = obj as? NSDictionary {
try serializeDictionary(dict)
}
else if let null = obj as? NSNull {
try serializeNull(null)
}
else {
throw NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.PropertyListReadCorruptError.rawValue, userInfo: ["NSDebugDescription" : "Invalid object cannot be serialized"])
}
}

func serializeString(str: NSString) throws {
let str = str.bridge()

writer("\"")
for scalar in str.unicodeScalars {
switch scalar {
case "\"":
writer("\\\"") // U+0022 quotation mark
case "\\":
writer("\\\\") // U+005C reverse solidus
// U+002F solidus not escaped
case "\u{8}":
writer("\\b") // U+0008 backspace
case "\u{c}":
writer("\\f") // U+000C form feed
case "\n":
writer("\\n") // U+000A line feed
case "\r":
writer("\\r") // U+000D carriage return
case "\t":
writer("\\t") // U+0009 tab
case "\u{0}"..."\u{f}":
writer("\\u000\(String(scalar.value, radix: 16))") // U+0000 to U+000F
case "\u{10}"..."\u{1f}":
writer("\\u00\(String(scalar.value, radix: 16))") // U+0010 to U+001F
default:
writer(String(scalar))
}
}
writer("\"")
}

func serializeNumber(num: NSNumber) throws {
if num.doubleValue.isInfinite || num.doubleValue.isNaN {
throw NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.PropertyListReadCorruptError.rawValue, userInfo: ["NSDebugDescription" : "Number cannot be infinity or NaN"])
}

// Cannot detect type information (e.g. bool) as there is no objCType property on NSNumber in Swift
// So, just print the number

writer("\(num)")
}

mutating func serializeArray(array: NSArray) throws {
writer("[")
if pretty {
writer("\n")
incAndWriteIndent()
}

var first = true
for elem in array.bridge() {
if first {
first = false
} else if pretty {
writer(",\n")
writeIndent()
} else {
writer(",")
}
try serializeJSON(elem)
}
if pretty {
writer("\n")
decAndWriteIndent()
}
writer("]")
}

mutating func serializeDictionary(dict: NSDictionary) throws {
writer("{")
if pretty {
writer("\n")
incAndWriteIndent()
}

var first = true
for (key, value) in dict.bridge() {
if first {
first = false
} else if pretty {
writer(",\n")
writeIndent()
} else {
writer(",")
}

if key is NSString {
try serializeString(key as! NSString)
} else {
throw NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.PropertyListReadCorruptError.rawValue, userInfo: ["NSDebugDescription" : "NSDictionary key must be NSString"])
}
pretty ? writer(": ") : writer(":")
try serializeJSON(value)
}
if pretty {
writer("\n")
decAndWriteIndent()
}
writer("}")
}

func serializeNull(null: NSNull) throws {
writer("null")
}

let indentAmount = 2

mutating func incAndWriteIndent() {
indent += indentAmount
writeIndent()
}

mutating func decAndWriteIndent() {
indent -= indentAmount
writeIndent()
}

func writeIndent() {
for _ in 0..<indent {
writer(" ")
}
}
}

//MARK: - JSONDeserializer
private struct JSONReader {

Expand Down
173 changes: 173 additions & 0 deletions TestFoundation/TestNSJSONSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class TestNSJSONSerialization : XCTestCase {
return JSONObjectWithDataTests
+ deserializationTests
+ isValidJSONObjectTests
+ serializationTests
}

}
Expand Down Expand Up @@ -560,3 +561,175 @@ extension TestNSJSONSerialization {
}

}

// MARK: - serializationTests
extension TestNSJSONSerialization {

class var serializationTests: [(String, TestNSJSONSerialization -> () throws -> Void)] {
return [
("test_serialize_emptyObject", test_serialize_emptyObject),
("test_serialize_null", test_serialize_null),
("test_serialize_complexObject", test_serialize_complexObject),
("test_nested_array", test_nested_array),
("test_nested_dictionary", test_nested_dictionary),
("test_serialize_number", test_serialize_number),
("test_serialize_stringEscaping", test_serialize_stringEscaping),
("test_serialize_invalid_json", test_serialize_invalid_json),
]
}

func trySerialize(obj: AnyObject) throws -> String {
let data = try NSJSONSerialization.dataWithJSONObject(obj, options: [])
guard let string = NSString(data: data, encoding: NSUTF8StringEncoding) else {
XCTFail("Unable to create string")
return ""
}
return string.bridge()
}

func test_serialize_emptyObject() {
let dict1 = [String: Any]().bridge()
XCTAssertEqual(try trySerialize(dict1), "{}")

let dict2 = [String: NSNumber]().bridge()
XCTAssertEqual(try trySerialize(dict2), "{}")

let dict3 = [String: String]().bridge()
XCTAssertEqual(try trySerialize(dict3), "{}")

let array1 = [String]().bridge()
XCTAssertEqual(try trySerialize(array1), "[]")

let array2 = [NSNumber]().bridge()
XCTAssertEqual(try trySerialize(array2), "[]")
}

func test_serialize_null() {
let arr = [NSNull()].bridge()
XCTAssertEqual(try trySerialize(arr), "[null]")

let dict = ["a":NSNull()].bridge()
XCTAssertEqual(try trySerialize(dict), "{\"a\":null}")

let arr2 = [NSNull(), NSNull(), NSNull()].bridge()
XCTAssertEqual(try trySerialize(arr2), "[null,null,null]")

let dict2 = [["a":NSNull()], ["b":NSNull()], ["c":NSNull()]].bridge()
XCTAssertEqual(try trySerialize(dict2), "[{\"a\":null},{\"b\":null},{\"c\":null}]")
}

func test_serialize_complexObject() {
let jsonDict = ["a": 4].bridge()
XCTAssertEqual(try trySerialize(jsonDict), "{\"a\":4}")

let jsonArr = [1, 2, 3, 4].bridge()
XCTAssertEqual(try trySerialize(jsonArr), "[1,2,3,4]")

let jsonDict2 = ["a": [1,2]].bridge()
XCTAssertEqual(try trySerialize(jsonDict2), "{\"a\":[1,2]}")

let jsonArr2 = ["a", "b", "c"].bridge()
XCTAssertEqual(try trySerialize(jsonArr2), "[\"a\",\"b\",\"c\"]")

let jsonArr3 = [["a":1],["b":2]].bridge()
XCTAssertEqual(try trySerialize(jsonArr3), "[{\"a\":1},{\"b\":2}]")

let jsonArr4 = [["a":NSNull()],["b":NSNull()]].bridge()
XCTAssertEqual(try trySerialize(jsonArr4), "[{\"a\":null},{\"b\":null}]")
}

func test_nested_array() {
var arr = ["a"].bridge()
XCTAssertEqual(try trySerialize(arr), "[\"a\"]")

arr = [["b"]].bridge()
XCTAssertEqual(try trySerialize(arr), "[[\"b\"]]")

arr = [[["c"]]].bridge()
XCTAssertEqual(try trySerialize(arr), "[[[\"c\"]]]")

arr = [[[["d"]]]].bridge()
XCTAssertEqual(try trySerialize(arr), "[[[[\"d\"]]]]")
}

func test_nested_dictionary() {
var dict = ["a":1].bridge()
XCTAssertEqual(try trySerialize(dict), "{\"a\":1}")

dict = ["a":["b":1]].bridge()
XCTAssertEqual(try trySerialize(dict), "{\"a\":{\"b\":1}}")

dict = ["a":["b":["c":1]]].bridge()
XCTAssertEqual(try trySerialize(dict), "{\"a\":{\"b\":{\"c\":1}}}")

dict = ["a":["b":["c":["d":1]]]].bridge()
XCTAssertEqual(try trySerialize(dict), "{\"a\":{\"b\":{\"c\":{\"d\":1}}}}")
}

func test_serialize_number() {
var json = [1, 1.1, 0, -2].bridge()
XCTAssertEqual(try trySerialize(json), "[1,1.1,0,-2]")

// Cannot generate "true"/"false" currently
json = [NSNumber(bool:false),NSNumber(bool:true)].bridge()
XCTAssertEqual(try trySerialize(json), "[0,1]")
}

func test_serialize_stringEscaping() {
var json = ["foo"].bridge()
XCTAssertEqual(try trySerialize(json), "[\"foo\"]")

json = ["a\0"].bridge()
XCTAssertEqual(try trySerialize(json), "[\"a\\u0000\"]")

json = ["b\\"].bridge()
XCTAssertEqual(try trySerialize(json), "[\"b\\\\\"]")

json = ["c\t"].bridge()
XCTAssertEqual(try trySerialize(json), "[\"c\\t\"]")

json = ["d\n"].bridge()
XCTAssertEqual(try trySerialize(json), "[\"d\\n\"]")

json = ["e\r"].bridge()
XCTAssertEqual(try trySerialize(json), "[\"e\\r\"]")

json = ["f\""].bridge()
XCTAssertEqual(try trySerialize(json), "[\"f\\\"\"]")

json = ["g\'"].bridge()
XCTAssertEqual(try trySerialize(json), "[\"g\'\"]")

json = ["h\u{7}"].bridge()
XCTAssertEqual(try trySerialize(json), "[\"h\\u0007\"]")

json = ["i\u{1f}"].bridge()
XCTAssertEqual(try trySerialize(json), "[\"i\\u001f\"]")
}

func test_serialize_invalid_json() {
let str = "Invalid JSON".bridge()
do {
let _ = try trySerialize(str)
XCTFail("Top-level JSON object cannot be string")
} catch {
// should get here
}

let double = NSNumber(double: 1.2)
do {
let _ = try trySerialize(double)
XCTFail("Top-level JSON object cannot be double")
} catch {
// should get here
}

let dict = [NSNumber(double: 1.2):"a"].bridge()
do {
let _ = try trySerialize(dict)
XCTFail("Dictionary keys must be strings")
} catch {
// should get here
}
}
}