Here is my problem, when I receive some JSON, it happens that some values do not match the required type. I don't really mind, I'm only interested by the value when its type is correct.
For instance, the following structure:
struct Foo : Decodable {
var bar : Int?
}
I'd like it to match these JSON:
{ "bar" : 42 } => foo.bar == 42
{ "bar" : null } => foo.bar == nil
{ "bar" : "baz" } => foo.bar == nil
Indeed I'm looking for an optional Int
, so whenever it's an integer I want it, but when it's null
or something else I want nil
.
Unfortunately, our good old JSONDecoder
raises a type mismatch error on the last case.
I know a manual way to do it:
struct Foo : Decodable {
var bar : Int?
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.bar = try? container.decode(Int.self, forKey: .bar)
}
enum CodingKeys : CodingKey {
case bar
}
}
But I have many structures, and many fields to check.
So I'd like to know if there is a general way to do it something like:
decoder.typeMismatchStrategy = .nilInsteadOfError // <= Don't try it at home, I know it does not exist...
Or maybe override JSONDecoder
, anyway something to write once and not on every struct.
Thanks in advance.