Skip to content

Instantly share code, notes, and snippets.

@elenipapanik
Created May 4, 2019 14:08
Show Gist options
  • Save elenipapanik/9b105480e7c53ae3d4d8415a5260b61c to your computer and use it in GitHub Desktop.
Save elenipapanik/9b105480e7c53ae3d4d8415a5260b61c to your computer and use it in GitHub Desktop.
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