Skip to content

Commit d9b10d3

Browse files
committed
NSJSONSerialization.data(...) fatalErrors on bad input to match objc behavior
1 parent ef6509a commit d9b10d3

File tree

2 files changed

+1
-16
lines changed

2 files changed

+1
-16
lines changed

Foundation/NSJSONSerialization.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,7 @@ open class JSONSerialization : NSObject {
134134
} else if let container = value as? Dictionary<AnyHashable, Any> {
135135
try writer.serializeJSON(container)
136136
} else {
137-
if stream {
138-
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [
139-
"NSDebugDescription" : "Top-level object was not NSArray or NSDictionary"
140-
])
141-
} else {
142-
fatalError("Top-level object was not NSArray or NSDictionary") // This is a fatal error in objective-c too (it is an NSInvalidArgumentException)
143-
}
137+
fatalError("Top-level object was not NSArray or NSDictionary") // This is a fatal error in objective-c too (it is an NSInvalidArgumentException)
144138
}
145139

146140
let count = jsonStr.lengthOfBytes(using: .utf8)

TestFoundation/TestNSJSONSerialization.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,6 @@ extension TestNSJSONSerialization {
940940
("test_jsonReadingOffTheEndOfBuffers", test_jsonReadingOffTheEndOfBuffers),
941941
("test_jsonObjectToOutputStreamBuffer", test_jsonObjectToOutputStreamBuffer),
942942
("test_jsonObjectToOutputStreamFile", test_jsonObjectToOutputStreamFile),
943-
("test_invalidJsonObjectToStreamBuffer", test_invalidJsonObjectToStreamBuffer),
944943
("test_jsonObjectToOutputStreamInsufficientBuffer", test_jsonObjectToOutputStreamInsufficientBuffer),
945944
("test_booleanJSONObject", test_booleanJSONObject),
946945
("test_serialize_dictionaryWithDecimal", test_serialize_dictionaryWithDecimal),
@@ -1289,14 +1288,6 @@ extension TestNSJSONSerialization {
12891288
}
12901289
}
12911290

1292-
func test_invalidJsonObjectToStreamBuffer() {
1293-
let str = "Invalid JSON"
1294-
let buffer = Array<UInt8>(repeating: 0, count: 10)
1295-
let outputStream = OutputStream(toBuffer: UnsafeMutablePointer(mutating: buffer), capacity: buffer.count)
1296-
outputStream.open()
1297-
XCTAssertThrowsError(try JSONSerialization.writeJSONObject(str, toStream: outputStream, options: []))
1298-
}
1299-
13001291
func test_booleanJSONObject() {
13011292
do {
13021293
let objectLikeBoolArray = try JSONSerialization.data(withJSONObject: [true, NSNumber(value: false), NSNumber(value: true)] as Array<Any>)

0 commit comments

Comments
 (0)