{"action":"created","issue":{"url":"https://api.github.com/repos/matthewcheok/JSONCodable/issues/66","repository_url":"https://api.github.com/repos/matthewcheok/JSONCodable","labels_url":"https://api.github.com/repos/matthewcheok/JSONCodable/issues/66/labels{/name}","comments_url":"https://api.github.com/repos/matthewcheok/JSONCodable/issues/66/comments","events_url":"https://api.github.com/repos/matthewcheok/JSONCodable/issues/66/events","html_url":"https://github.com/matthewcheok/JSONCodable/issues/66","id":196279478,"number":66,"title":"`JSONDecodable` with static `parse` method?","user":{"login":"bejar37","id":752997,"avatar_url":"https://avatars.githubusercontent.com/u/752997?v=3","gravatar_id":"","url":"https://api.github.com/users/bejar37","html_url":"https://github.com/bejar37","followers_url":"https://api.github.com/users/bejar37/followers","following_url":"https://api.github.com/users/bejar37/following{/other_user}","gists_url":"https://api.github.com/users/bejar37/gists{/gist_id}","starred_url":"https://api.github.com/users/bejar37/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bejar37/subscriptions","organizations_url":"https://api.github.com/users/bejar37/orgs","repos_url":"https://api.github.com/users/bejar37/repos","events_url":"https://api.github.com/users/bejar37/events{/privacy}","received_events_url":"https://api.github.com/users/bejar37/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-12-18T14:48:09Z","updated_at":"2016-12-18T21:11:48Z","closed_at":null,"body":"Hi,\r\nRight now the definition for `JSONDecodable` requires an implementation of an initializer. This has a couple of drawbacks IMO:\r\n* Enums with associated values can't conform to `JSONDecodable` (see #29)\r\n* For classes, `JSONDecodable` has to be conformed to in the main class declaration, not in an extension. This means that classes defined in ObjC can't conform to `JSONDecodable` in a Swift extension, for example. \r\n\r\nMy question is: what are opinions on a breaking change to moving to a definition of `JSONDecodable` that looks like this:\r\n\r\n```swift\r\nprotocol JSONDecodable {\r\n    static func parse(decoder: JSONDecoder) throws -> Self\r\n}\r\n```\r\n\r\nI know that this would be a breaking change, but are there other reasons why this is a bad idea?"},"comment":{"url":"https://api.github.com/repos/matthewcheok/JSONCodable/issues/comments/267846940","html_url":"https://github.com/matthewcheok/JSONCodable/issues/66#issuecomment-267846940","issue_url":"https://api.github.com/repos/matthewcheok/JSONCodable/issues/66","id":267846940,"user":{"login":"Nadohs","id":1163363,"avatar_url":"https://avatars.githubusercontent.com/u/1163363?v=3","gravatar_id":"","url":"https://api.github.com/users/Nadohs","html_url":"https://github.com/Nadohs","followers_url":"https://api.github.com/users/Nadohs/followers","following_url":"https://api.github.com/users/Nadohs/following{/other_user}","gists_url":"https://api.github.com/users/Nadohs/gists{/gist_id}","starred_url":"https://api.github.com/users/Nadohs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Nadohs/subscriptions","organizations_url":"https://api.github.com/users/Nadohs/orgs","repos_url":"https://api.github.com/users/Nadohs/repos","events_url":"https://api.github.com/users/Nadohs/events{/privacy}","received_events_url":"https://api.github.com/users/Nadohs/received_events","type":"User","site_admin":false},"created_at":"2016-12-18T21:11:48Z","updated_at":"2016-12-18T21:11:48Z","body":"We thought about the impact of doing this awhile ago and the biggest reason we didn't like it was because this would require your class object properties to have all default values or another initializer defined in the class definition anyways to handle all the properties from the object.\r\n\r\nBeyond that if you settle for one of those two conditions I believe you wouldn't necessarily require breaking changes. I played around with an implementation targeting objects using a new protocol `JSONObjDecodable`, which is conformant to the original `JSONDecodable`.  Here is an implementation I threw together.\r\n\r\nThis is what it looks like in the extension for a class:\r\n```\r\nclass NameCount {\r\n    let name: String\r\n    let count: Int\r\n    init(name: String, count: Int) {\r\n        self.name = name\r\n        self.count = count\r\n    }\r\n}\r\n\r\nextension NameCount: JSONObjDecodable {\r\n\r\n    static func jsonConstruct(object: JSONObject) throws -> Self {\r\n        let decoder = JSONDecoder(object: object)\r\n\r\n        let result = NameCount(name: try decoder.decode(\"name\"),\r\n                               count: try decoder.decode(\"count\"))\r\n\r\n        return DecodableCastAs(object: result)\r\n    }\r\n}\r\n```\r\n\r\nThis is what the implementation to make this work looks like:\r\n```\r\npublic protocol JSONObjDecodable: JSONDecodable {\r\n    static func jsonConstruct(object: JSONObject) throws -> Self\r\n}\r\n\r\nextension JSONObjDecodable where Self: AnyObject {\r\n    public init(object: JSONObject) throws {\r\n        self = try Self.jsonConstruct(object: object)\r\n    }\r\n}\r\n\r\n/// this is to avoid the class requiring to be marked `final` \r\npublic func DecodableCastAs<T>(object: Any) -> T {\r\n    return object as! T\r\n}\r\n```\r\n\r\nHere is a branch with this implementation along with a test for it: \r\nhttps://github.com/matthewcheok/JSONCodable/tree/object-constructor"}}
{"actor":{"display_login":"Nadohs"}}