Skip to content

Commit cbf2211

Browse files
committed
Removed optional initializer tests
1 parent 000982c commit cbf2211

File tree

4 files changed

+26
-43
lines changed

4 files changed

+26
-43
lines changed

JSONCodableTests/EnumTests.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@ class EnumTests: XCTestCase {
1717
let decodedValue2 = Food(name: "Seaweed Pasta", cuisines: [.Italian, .Japanese])
1818

1919
func testDecodingEnum() {
20-
let a = Fruit(optional: encodedValue)
21-
guard let fruit = Fruit(optional: encodedValue) else {
20+
guard let fruit = try? Fruit(object: encodedValue) else {
2221
XCTFail()
2322
return
2423
}
2524

2625
XCTAssertEqual(fruit, decodedValue)
2726

28-
guard let food = Food(optional: encodedValue2) else {
27+
guard let food = try? Food(object: encodedValue2) else {
2928
XCTFail()
3029
return
3130
}

JSONCodableTests/RegularTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ class RegularTests: XCTestCase {
3434
])
3535

3636
func testDecodingRegular() {
37-
guard let user = User(optional: encodedValue) else {
37+
guard let user = try? User(object: encodedValue) else {
3838
XCTFail()
3939
return
4040
}
4141

4242
XCTAssertEqual(user, decodedValue)
4343
}
44-
44+
4545
func testEncodingRegular() {
4646
guard let json = try? decodedValue.toJSON() else {
4747
XCTFail()

JSONCodableTests/TransformerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class TransformerTests: XCTestCase {
2020
)
2121

2222
func testDecodingTransformer() {
23-
guard let asset = ImageAsset(optional: encodedValue) else {
23+
guard let asset = try? ImageAsset(object: encodedValue) else {
2424
XCTFail()
2525
return
2626
}

README.md

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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:
122122
Simply add conformance to `JSONDecodable` (or to `JSONCodable`):
123123
```swift
124124
extension 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

140135
extension 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]`.
155145
As 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)
159149
print("\(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

225209
func toJSON() throws -> AnyObject {
@@ -244,7 +228,7 @@ This allows for JSONDecoder extensions that allow the type system to better aid
244228
```swift
245229
extension 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

Comments
 (0)