@@ -24,12 +24,12 @@ Hassle-free JSON encoding and decoding in Swift
2424
2525- Simply add the following to your [ ` Cartfile ` ] ( https://github.com/Carthage/Carthage ) and run ` carthage update ` :
2626```
27- github "matthewcheok/JSONCodable"
27+ github "matthewcheok/JSONCodable" ~> 2.1
2828```
2929
3030- or add the following to your [ ` Podfile ` ] ( http://cocoapods.org/ ) and run ` pod install ` :
3131```
32- pod 'JSONCodable', '~> 2.0 '
32+ pod 'JSONCodable', '~> 2.1 '
3333```
3434
3535- or clone as a git submodule,
@@ -122,40 +122,30 @@ Result:
122122Simply add conformance to ` JSONDecodable ` (or to ` JSONCodable ` ):
123123``` swift
124124extension User : JSONDecodable {
125- init? (JSONDictionary : JSONObject) {
126- let decoder = JSONDecoder (object : JSONDictionary)
127- do {
128- id = try decoder.decode (" id" )
129- name = try decoder.decode (" full_name" )
130- email = try decoder.decode (" email" )
131- company = try decoder.decode (" company" )
132- friends = try decoder.decode (" friends" )
133- }
134- catch {
135- return nil
136- }
137- }
125+ init (object : JSONObject) throws {
126+ let decoder = JSONDecoder (object : object)
127+ id = try decoder.decode (" id" )
128+ name = try decoder.decode (" full_name" )
129+ email = try decoder.decode (" email" )
130+ company = try decoder.decode (" company" )
131+ friends = try decoder.decode (" friends" )
132+ }
138133}
139134
140135extension Company : JSONDecodable {
141- init? (JSONDictionary : JSONObject) {
142- let decoder = JSONDecoder (object : JSONDictionary)
143- do {
144- name = try decoder.decode (" name" )
145- address = try decoder.decode (" address" )
146- }
147- catch {
148- return nil
149- }
150- }
136+ init (object : JSONObject) throws {
137+ let decoder = JSONDecoder (object : object)
138+ name = try decoder.decode (" name" )
139+ address = try decoder.decode (" address" )
140+ }
151141}
152142```
153143
154- Simply provide the implementations for ` init?(JSONDictionary : JSONObject) ` where ` JSONObject ` is a typealias for ` [String:AnyObject] ` .
144+ Simply provide the implementations for ` init(object : JSONObject) throws ` where ` JSONObject ` is a typealias for ` [String:AnyObject] ` .
155145As before, you can use this to configure the mapping between keys in the Dictionary to properties in your structs and classes.
156146
157147``` swift
158- let user = User (JSONDictionary : JSON)
148+ let user = try ! User (object : JSON)
159149print (" \( user ) " )
160150```
161151
@@ -211,15 +201,9 @@ struct User {
211201 var website: NSURL?
212202}
213203
214- init? (JSONDictionary : JSONObject) {
215- let decoder = JSONDecoder (object : JSONDictionary)
216- do {
217- ...
218- website = try JSONDictionary.decode (" website" , transformer : JSONTransformerStringToNSURL)
219- }
220- catch {
221- return nil
222- }
204+ init (object : JSONObject) throws {
205+ ...
206+ website = try JSONDictionary.decode (" website" , transformer : JSONTransformerStringToNSURL)
223207}
224208
225209func toJSON () throws -> AnyObject {
@@ -244,7 +228,7 @@ This allows for JSONDecoder extensions that allow the type system to better aid
244228``` swift
245229extension JSONDecoder {
246230 public func decode (key : String ) throws -> NSURL {
247- return decode (key, JSONTransformers.StringToNSURL )
231+ return try decode (key, transformer : JSONTransformers.StringToNSURL )
248232 }
249233}
250234```
0 commit comments