Skip to content
Open
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
Raised a 'invalidJSON' error if initialization JSON data is corrupt.
  • Loading branch information
Varme authored and Varme committed Mar 15, 2020
commit 21026fc046bab7293ce0ed59d7908161b4397800
2 changes: 2 additions & 0 deletions Source/SwiftyJSON/SwiftyJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public struct JSON {
try self.init(data: object)
} catch {
self.init(jsonObject: NSNull())
self.error = SwiftyJSONError.invalidJSON
}
default:
self.init(jsonObject: object)
Expand All @@ -127,6 +128,7 @@ public struct JSON {
self.init(data)
} else {
self.init(NSNull())
self.error = SwiftyJSONError.invalidJSON
}
}

Expand Down
8 changes: 8 additions & 0 deletions Tests/SwiftJSONTests/BaseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import SwiftyJSON
class BaseTests: XCTestCase {

var testData: Data!
var corruptJSONString: String!

override func setUp() {

Expand All @@ -38,6 +39,8 @@ class BaseTests: XCTestCase {
} else {
XCTFail("Can't find the test JSON file")
}

self.corruptJSONString = "{\n This is a corrupt JSON example."
}

override func tearDown() {
Expand Down Expand Up @@ -274,4 +277,9 @@ class BaseTests: XCTestCase {
// everything is OK
}
}

func testCorruptJSON() {
XCTAssertEqual(JSON(parseJSON: self.corruptJSONString).error, SwiftyJSONError.invalidJSON)
XCTAssertEqual(JSON(Data(self.corruptJSONString.utf8)).error, SwiftyJSONError.invalidJSON)
}
}