Anviking / Decodable

[Probably deprecated] Swift 2/3 JSON unmarshalling done (more) right
MIT License
1.04k stars 73 forks source link

Extends RawRepresentables which are also Decodable with decode implem… #63

Closed danielgarbien closed 8 years ago

danielgarbien commented 8 years ago

…entation.

JaviSoto commented 8 years ago

This is great!

Anviking commented 8 years ago

Nice! Could you just add a short comment describing the .UnexpectedValue so it's not confused in any way with .TypeMismatch?

(Was bit uncomfortable by having the .UnexpectedValue inside DecodingError, but then realized that it has to for the DecodingError.Info-things to work, so sure. I should perhaps rethink the error handling tough)

And a minor detail, but how about this instead?

public extension RawRepresentable where RawValue: Decodable, Self: Decodable {
    static func decode(json: AnyObject) throws -> Self {
        let rawValue = try RawValue.decode(json)
        guard let rawRepresentable = Self(rawValue: rawValue) else {
            throw DecodingError.UnexpectedValue(value: rawValue as! AnyObject, info: DecodingError.Info(object: json))
        }
        return rawRepresentable
    }
}
danielgarbien commented 8 years ago

Sure, it now fits better the implementation of extensions on arrays and dictionaries.

Anviking commented 8 years ago

Thank you very much!