Skip to content

Commit df58c58

Browse files
committed
Make JSONDecodable constructor throws.
1 parent 5693233 commit df58c58

File tree

2 files changed

+21
-36
lines changed

2 files changed

+21
-36
lines changed

JSONCodable/JSONDecodable.swift

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ public enum JSONDecodableError: ErrorType, CustomStringConvertible {
4848
// Dictionary -> Struct
4949

5050
public protocol JSONDecodable {
51-
init?(JSONDictionary: JSONObject)
51+
init(JSONDictionary: JSONObject) throws
5252
}
5353

5454
public extension Array where Element: JSONDecodable {
55-
init(JSONArray: [AnyObject]) {
56-
self.init(JSONArray.flatMap {
55+
init(JSONArray: [AnyObject]) throws {
56+
self.init(try JSONArray.flatMap {
5757
guard let json = $0 as? [String : AnyObject] else {
58-
return nil
58+
throw JSONDecodableError.DictionaryTypeExpectedError(key: "[inarray]", elementType: $0.dynamicType)
5959
}
60-
return Element(JSONDictionary: json)
60+
return try Element(JSONDictionary: json)
6161
})
6262
}
6363
}
@@ -110,10 +110,7 @@ public class JSONDecoder {
110110
guard let object = value as? JSONObject else {
111111
throw JSONDecodableError.DictionaryTypeExpectedError(key: key, elementType: value.dynamicType)
112112
}
113-
guard let decodable = Decodable(JSONDictionary: object) else {
114-
throw JSONDecodableError.IncompatibleTypeError(key: key, elementType: value.dynamicType, expectedType: Decodable.self)
115-
}
116-
return decodable
113+
return try Decodable(JSONDictionary: object)
117114
}
118115

119116
// JSONDecodable?
@@ -124,7 +121,7 @@ public class JSONDecoder {
124121
guard let object = value as? JSONObject else {
125122
throw JSONDecodableError.DictionaryTypeExpectedError(key: key, elementType: value.dynamicType)
126123
}
127-
return Decodable(JSONDictionary: object)
124+
return try Decodable(JSONDictionary: object)
128125
}
129126

130127
// Enum
@@ -185,7 +182,7 @@ public class JSONDecoder {
185182
guard let array = value as? [JSONObject] else {
186183
throw JSONDecodableError.ArrayTypeExpectedError(key: key, elementType: value.dynamicType)
187184
}
188-
return array.flatMap {Element(JSONDictionary: $0)}
185+
return try array.flatMap { try Element(JSONDictionary: $0)}
189186
}
190187

191188
// [JSONDecodable]?
@@ -196,7 +193,7 @@ public class JSONDecoder {
196193
guard let array = value as? [JSONObject] else {
197194
throw JSONDecodableError.ArrayTypeExpectedError(key: key, elementType: value.dynamicType)
198195
}
199-
return array.flatMap {Element(JSONDictionary: $0)}
196+
return try array.flatMap { try Element(JSONDictionary: $0)}
200197
}
201198

202199
// [Enum]

JSONCodable/JSONString.swift

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -56,45 +56,33 @@ public extension Optional where Wrapped: JSONEncodable {
5656
}
5757

5858
public extension JSONDecodable {
59-
init?(JSONString: String) {
59+
init(JSONString: String) throws {
6060
guard let data = JSONString.dataUsingEncoding(NSUTF8StringEncoding) else {
61-
return nil
62-
}
63-
64-
let result: AnyObject
65-
do {
66-
result = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(rawValue: 0))
67-
}
68-
catch {
69-
return nil
61+
throw JSONDecodableError.IncompatibleTypeError(key: "n/a", elementType: JSONString.dynamicType, expectedType: String.self)
7062
}
7163

64+
let result: AnyObject = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(rawValue: 0))
65+
7266
guard let converted = result as? [String: AnyObject] else {
73-
return nil
67+
throw JSONDecodableError.DictionaryTypeExpectedError(key: "n/a", elementType: result.dynamicType)
7468
}
7569

76-
self.init(JSONDictionary: converted)
70+
try self.init(JSONDictionary: converted)
7771
}
7872
}
7973

8074
public extension Array where Element: JSONDecodable {
81-
init?(JSONString: String) {
75+
init(JSONString: String) throws {
8276
guard let data = JSONString.dataUsingEncoding(NSUTF8StringEncoding) else {
83-
return nil
84-
}
85-
86-
let result: AnyObject
87-
do {
88-
result = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(rawValue: 0))
89-
}
90-
catch {
91-
return nil
77+
throw JSONDecodableError.IncompatibleTypeError(key: "n/a", elementType: JSONString.dynamicType, expectedType: String.self)
9278
}
9379

80+
let result: AnyObject = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(rawValue: 0))
81+
9482
guard let converted = result as? [AnyObject] else {
95-
return nil
83+
throw JSONDecodableError.ArrayTypeExpectedError(key: "n/a", elementType: result.dynamicType)
9684
}
9785

98-
self.init(JSONArray: converted)
86+
try self.init(JSONArray: converted)
9987
}
10088
}

0 commit comments

Comments
 (0)