I need to parse the following date in a JSON payload and it's almost in ISO8601 format.
"2020-06-05 14:52:54 UTC"
To conform to ISO8601 it needs to be altered slightly.
"2020-06-05T14:52:54Z"
It's super annoying because I now have to make a customer date decoding strategy.
static func make() -> JSONDecoder {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
// decoder.dateDecodingStrategy = .iso8601
decoder.dateDecodingStrategy = .custom({ decoder in
let container = try decoder.singleValueContainer()
let dateStr = try container.decode(String.self)
guard let date = formatter.date(from: dateStr) else {
preconditionFailure("Unexpected date format.")
}
return date
})
return decoder
}
I don't have control of the data source. Is there anything I can do to avoid a custom decoding strategy in this case?