Skip to content

Instantly share code, notes, and snippets.

@bricker
Created April 5, 2021 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bricker/03a0a6cad62d88aafc70494a655db3ca to your computer and use it in GitHub Desktop.
Save bricker/03a0a6cad62d88aafc70494a655db3ca to your computer and use it in GitHub Desktop.
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