Skip to content

Commit fd5dcef

Browse files
committed
made object property private, updated DataError enum, RawRepresentable extension now returns nil in init if not valid JSON object
1 parent abb430b commit fd5dcef

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

Sources/JSON.swift

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import Foundation
2424

2525
public struct JSON {
2626

27-
private(set) var object: AnyObject?
27+
private var object: AnyObject?
2828

2929
public init() {
3030
self.object = nil
@@ -299,16 +299,14 @@ extension Dictionary {
299299

300300
extension JSON: RawRepresentable {
301301

302-
enum JSONDataError: ErrorType {
303-
case ObjectMissing
304-
case InvalidJSONObject
302+
enum DataError: ErrorType {
303+
case MissingObject
304+
case InvalidObject
305305
}
306306

307307
public init?(rawValue: AnyObject) {
308308
guard NSJSONSerialization.isValidJSONObject(rawValue) else {
309-
self.init()
310-
311-
return
309+
return nil
312310
}
313311

314312
self.init(rawValue)
@@ -318,13 +316,13 @@ extension JSON: RawRepresentable {
318316
return self.object ?? NSNull()
319317
}
320318

321-
public func rawData(options: NSJSONWritingOptions = NSJSONWritingOptions(rawValue: 0)) throws -> NSData {
319+
public func rawData(options: NSJSONWritingOptions = []) throws -> NSData {
322320
guard let object = object else {
323-
throw JSONDataError.ObjectMissing
321+
throw DataError.MissingObject
324322
}
325323

326324
guard NSJSONSerialization.isValidJSONObject(object) else {
327-
throw JSONDataError.InvalidJSONObject
325+
throw DataError.InvalidObject
328326
}
329327

330328
return try NSJSONSerialization.dataWithJSONObject(object, options: options)

0 commit comments

Comments
 (0)