import Foundation
let json = """
{
"property": null
}
""".data(using: .utf8)!
struct Program<T: Decodable>: Decodable {
let property: T
static func decode() {
do {
try JSONDecoder().decode(Self.self, from: json)
} catch {
print("Error decoding \(T.self): \(error)\n")
}
}
}
Program<String>.decode()
Program<Int>.decode()
Program<[Double]>.decode()
Program<[String: Int]>.decode()
Program<Bool>.decode()
For every case, except for Bool
, we get valueNotFound
("Cannot get unkeyed decoding container -- found null value instead") error. Which is correct, according to the documentation.
Only for Bool
, for some reason, we get typeMismatch
("Expected to decode Bool but found null instead.") error.