Created
May 4, 2019 14:08
-
-
Save elenipapanik/9b105480e7c53ae3d4d8415a5260b61c to your computer and use it in GitHub Desktop.
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
extension Argo.JSON: Swift.Decodable { | |
public init(from decoder: Decoder) throws { | |
let container = try decoder.singleValueContainer() | |
if let value = try? container.decode(String.self) { | |
self = .string(value) | |
} else if let value = try? container.decode(Int.self) { | |
self = JSON.number(value as NSNumber) | |
} else if let value = try? container.decode(Double.self) { | |
self = JSON.number(value as NSNumber) | |
} else if let value = try? container.decode(Bool.self) { | |
self = .bool(value) | |
} else if let value = try? container.decode([String: Argo.JSON].self) { | |
self = .object(value) | |
} else if let value = try? container.decode([Argo.JSON].self) { | |
self = .array(value) | |
} else if container.decodeNil() { | |
self = .null | |
} else { | |
throw DecodingError.typeMismatch(JSON.self, DecodingError.Context(codingPath: container.codingPath, debugDescription: "Not a JSON")) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment