Skip to content

Commit 8e0a890

Browse files
authored
Merge pull request #1151 from glenna/fix/fatalerror_on_jsonserialization_bad_input
2 parents cab40e7 + 57062ee commit 8e0a890

File tree

2 files changed

+1
-16
lines changed

2 files changed

+1
-16
lines changed

Foundation/JSONSerialization.swift

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

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

TestFoundation/TestJSONSerialization.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,6 @@ extension TestJSONSerialization {
945945
("test_jsonReadingOffTheEndOfBuffers", test_jsonReadingOffTheEndOfBuffers),
946946
("test_jsonObjectToOutputStreamBuffer", test_jsonObjectToOutputStreamBuffer),
947947
("test_jsonObjectToOutputStreamFile", test_jsonObjectToOutputStreamFile),
948-
("test_invalidJsonObjectToStreamBuffer", test_invalidJsonObjectToStreamBuffer),
949948
("test_jsonObjectToOutputStreamInsufficientBuffer", test_jsonObjectToOutputStreamInsufficientBuffer),
950949
("test_booleanJSONObject", test_booleanJSONObject),
951950
("test_serialize_dictionaryWithDecimal", test_serialize_dictionaryWithDecimal),
@@ -1315,14 +1314,6 @@ extension TestJSONSerialization {
13151314
}
13161315
}
13171316

1318-
func test_invalidJsonObjectToStreamBuffer() {
1319-
let str = "Invalid JSON"
1320-
let buffer = Array<UInt8>(repeating: 0, count: 10)
1321-
let outputStream = OutputStream(toBuffer: UnsafeMutablePointer(mutating: buffer), capacity: buffer.count)
1322-
outputStream.open()
1323-
XCTAssertThrowsError(try JSONSerialization.writeJSONObject(str, toStream: outputStream, options: []))
1324-
}
1325-
13261317
func test_booleanJSONObject() {
13271318
do {
13281319
let objectLikeBoolArray = try JSONSerialization.data(withJSONObject: [true, NSNumber(value: false), NSNumber(value: true)] as Array<Any>)

0 commit comments

Comments
 (0)