Created
April 5, 2021 05:48
-
-
Save bricker/03a0a6cad62d88aafc70494a655db3ca to your computer and use it in GitHub Desktop.
This file contains hidden or 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 JSONValue: Decodable { | |
public init(from decoder: Decoder) throws { | |
let container = try decoder.singleValueContainer() | |
if let value = try? container.decode(Bool.self) { | |
self = .boolean(value) | |
} else if let value = try? container.decode(Double.self) { | |
// Double covers Int as well | |
self = .number(value) | |
} else if let value = try? container.decode(String.self) { | |
self = .string(value) | |
} else if let value = try? container.decode([JSONValue].self) { | |
self = .array(value) | |
} else if let value = try? container.decode([String: JSONValue].self) { | |
self = .object(value) | |
} else { | |
self = .null | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment