-
-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introducing this typealias helps when trying to conform a non-final class to `JSONDecodable`. If we just use `Self`, we run into an issue where the compiler can't ensure that return type is correct, because the class might be subclassed. By letting classes explicitly define their decoded type, we can make the compiler happy while maintaining the ability to subclass if that's what we want. As an added benefit, we can default `DecodedType` to `Self`, which means that `structs` and `final` classes don't need to worry about changing anything from the way they are doing it now.
- Loading branch information
Showing
7 changed files
with
47 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
public protocol JSONDecodable { | ||
class func decode(JSONValue) -> Self? | ||
typealias DecodedType = Self | ||
class func decode(JSONValue) -> DecodedType? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"url": "http://example.com", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Argo | ||
import Foundation | ||
|
||
extension NSURL: JSONDecodable { | ||
public typealias DecodedType = NSURL | ||
|
||
public class func decode(j: JSONValue) -> DecodedType? { | ||
switch j { | ||
case .JSONString(let url): return NSURL(string: url) | ||
default: return .None | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,12 @@ class ExampleTests: XCTestCase { | |
XCTAssert(user?.email != nil) | ||
XCTAssert(user?.email! == "[email protected]") | ||
} | ||
|
||
func testDecodingNonFinalClass() { | ||
let json: AnyObject? = JSONFileReader.JSON(fromFile: "url") | ||
let url: NSURL? = json >>- JSONValue.parse >>- { $0["url"] >>- NSURL.decode } | ||
|
||
XCTAssert(url != nil) | ||
XCTAssert(url?.absoluteString == "http://example.com") | ||
} | ||
} |