I have a struct conforming to Decodable. It has 50 String properties and only one Bool. That bool is coming from server like a string "false"/"true" or sometimes like as integer 0/1, so cannot be decoded from the box. How can I make it decoding but not write huge amount of manual decoding of all that 50 String properties? Maybe somehow override decodeIfPresent for Bool, but I could not make it working. How I can avoid creating init with decoding everything and all that stuff manually and only handle that one Bool? Without computed property if possible.
struct Response: Decodable {
var s1: String
var s2: String
var s3: String
//...........
var s50: String
var b1: Bool
var b2: Bool
}
Here is json example:
{
"s1":"string"
"s2":"string"
"s3":"string"
//..........
"s50":"string"
"b1":"true"
"b2":"0"
}
Tried this but does not work(((
extension KeyedDecodingContainer { //Doesn't work, no execution
func decodeIfPresent(_ type: Bool.Type, forKey key: K) throws -> Bool {
return try! self.decodeIfPresent(Bool.self, forKey: key)
}
func decodeIfPresent(_ type: Bool.Type, forKey key: K) throws -> Bool? {
return try? self.decodeIfPresent(Bool.self, forKey: key)
}
}
One way to solve this is to write a property wrapper that handles the custom decoding, like this:
Use it like this:
Playground output: