Skip to content

Commit de8d19a

Browse files
committed
Merge pull request #20 from srdanrasic/master
Moving commits to develop branch
2 parents e5c1993 + 1cf873d commit de8d19a

File tree

3 files changed

+33
-38
lines changed

3 files changed

+33
-38
lines changed

JSONCodable.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
9EDB390B1B59D00B00C63019 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
6161
9EDB39231B59D01D00C63019 /* JSONCodable.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = JSONCodable.framework; sourceTree = BUILT_PRODUCTS_DIR; };
6262
9EDB393C1B59D0AF00C63019 /* JSONCodable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONCodable.swift; sourceTree = "<group>"; };
63-
9EDB393D1B59D0AF00C63019 /* JSONDecodable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONDecodable.swift; sourceTree = "<group>"; };
63+
9EDB393D1B59D0AF00C63019 /* JSONDecodable.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = JSONDecodable.swift; sourceTree = "<group>"; tabWidth = 4; };
6464
9EDB393E1B59D0AF00C63019 /* JSONEncodable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONEncodable.swift; sourceTree = "<group>"; };
6565
9EDB393F1B59D0AF00C63019 /* JSONHelpers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONHelpers.swift; sourceTree = "<group>"; };
6666
9EDB39411B59D0AF00C63019 /* JSONString.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONString.swift; sourceTree = "<group>"; };

JSONCodable/JSONDecodable.swift

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

5050
public protocol JSONDecodable {
51-
init?(JSONDictionary: JSONObject)
51+
init(object: JSONObject) throws
52+
}
53+
54+
public extension JSONDecodable {
55+
init?(JSONDictionary: JSONObject) {
56+
do {
57+
try self.init(object: JSONDictionary)
58+
} catch {
59+
return nil
60+
}
61+
}
5262
}
5363

5464
public extension Array where Element: JSONDecodable {
55-
init(JSONArray: [AnyObject]) {
56-
self.init(JSONArray.flatMap {
65+
init(JSONArray: [AnyObject]) throws {
66+
self.init(try JSONArray.flatMap {
5767
guard let json = $0 as? [String : AnyObject] else {
58-
return nil
68+
throw JSONDecodableError.DictionaryTypeExpectedError(key: "n/a", elementType: $0.dynamicType)
5969
}
60-
return Element(JSONDictionary: json)
70+
return try Element(object: json)
6171
})
6272
}
6373
}
@@ -110,10 +120,7 @@ public class JSONDecoder {
110120
guard let object = value as? JSONObject else {
111121
throw JSONDecodableError.DictionaryTypeExpectedError(key: key, elementType: value.dynamicType)
112122
}
113-
guard let decodable = Decodable(JSONDictionary: object) else {
114-
throw JSONDecodableError.IncompatibleTypeError(key: key, elementType: value.dynamicType, expectedType: Decodable.self)
115-
}
116-
return decodable
123+
return try Decodable(object: object)
117124
}
118125

119126
// JSONDecodable?
@@ -124,7 +131,7 @@ public class JSONDecoder {
124131
guard let object = value as? JSONObject else {
125132
throw JSONDecodableError.DictionaryTypeExpectedError(key: key, elementType: value.dynamicType)
126133
}
127-
return Decodable(JSONDictionary: object)
134+
return try Decodable(object: object)
128135
}
129136

130137
// Enum
@@ -185,7 +192,7 @@ public class JSONDecoder {
185192
guard let array = value as? [JSONObject] else {
186193
throw JSONDecodableError.ArrayTypeExpectedError(key: key, elementType: value.dynamicType)
187194
}
188-
return array.flatMap {Element(JSONDictionary: $0)}
195+
return try array.flatMap { try Element(object: $0)}
189196
}
190197

191198
// [JSONDecodable]?
@@ -196,7 +203,7 @@ public class JSONDecoder {
196203
guard let array = value as? [JSONObject] else {
197204
throw JSONDecodableError.ArrayTypeExpectedError(key: key, elementType: value.dynamicType)
198205
}
199-
return array.flatMap {Element(JSONDictionary: $0)}
206+
return try array.flatMap { try Element(object: $0)}
200207
}
201208

202209
// [Enum]
@@ -270,4 +277,4 @@ public class JSONDecoder {
270277
}
271278
return result
272279
}
273-
}
280+
}

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(object: 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)