Skip to content

Commit

Permalink
Merge pull request #1151 from glenna/fix/fatalerror_on_jsonserializat…
Browse files Browse the repository at this point in the history
…ion_bad_input
  • Loading branch information
swift-ci committed Aug 11, 2017
2 parents cab40e7 + 57062ee commit 8e0a890
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 16 deletions.
8 changes: 1 addition & 7 deletions Foundation/JSONSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,7 @@ open class JSONSerialization : NSObject {
} else if let container = value as? Dictionary<AnyHashable, Any> {
try writer.serializeJSON(container)
} else {
if stream {
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [
"NSDebugDescription" : "Top-level object was not NSArray or NSDictionary"
])
} else {
fatalError("Top-level object was not NSArray or NSDictionary") // This is a fatal error in objective-c too (it is an NSInvalidArgumentException)
}
fatalError("Top-level object was not NSArray or NSDictionary") // This is a fatal error in objective-c too (it is an NSInvalidArgumentException)
}

let count = jsonStr.lengthOfBytes(using: .utf8)
Expand Down
9 changes: 0 additions & 9 deletions TestFoundation/TestJSONSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,6 @@ extension TestJSONSerialization {
("test_jsonReadingOffTheEndOfBuffers", test_jsonReadingOffTheEndOfBuffers),
("test_jsonObjectToOutputStreamBuffer", test_jsonObjectToOutputStreamBuffer),
("test_jsonObjectToOutputStreamFile", test_jsonObjectToOutputStreamFile),
("test_invalidJsonObjectToStreamBuffer", test_invalidJsonObjectToStreamBuffer),
("test_jsonObjectToOutputStreamInsufficientBuffer", test_jsonObjectToOutputStreamInsufficientBuffer),
("test_booleanJSONObject", test_booleanJSONObject),
("test_serialize_dictionaryWithDecimal", test_serialize_dictionaryWithDecimal),
Expand Down Expand Up @@ -1315,14 +1314,6 @@ extension TestJSONSerialization {
}
}

func test_invalidJsonObjectToStreamBuffer() {
let str = "Invalid JSON"
let buffer = Array<UInt8>(repeating: 0, count: 10)
let outputStream = OutputStream(toBuffer: UnsafeMutablePointer(mutating: buffer), capacity: buffer.count)
outputStream.open()
XCTAssertThrowsError(try JSONSerialization.writeJSONObject(str, toStream: outputStream, options: []))
}

func test_booleanJSONObject() {
do {
let objectLikeBoolArray = try JSONSerialization.data(withJSONObject: [true, NSNumber(value: false), NSNumber(value: true)] as Array<Any>)
Expand Down

0 comments on commit 8e0a890

Please sign in to comment.