I haven't found any info/similar questions here. I need to use .iso8601 for my JSONDecoder and JSONEncoder. Current I am doing this for each HTTP call:
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
URLSession.shared.dataTaskPublisher(for: request)
.receive(on: DispatchQueue.main)
.tryMap(handleHTTPOutput)
.decode(type: [TaskModel].self, decoder: decoder)
...
Is there any way to change the default value of dateDecodingStrategy for all the JSONEncoder and JSONDecoder?
I was trying to do the below but it doesn't work:
JSONDecoder.DateDecodingStrategy = iso8601
The error says the DateDecodingStrategy is immutable.
No matter what you do, you will have to go to every spot where you are constructing a JSONEncoder/Decoder and change it. Just change them all to use a single global Encoder/Decoder.
You can store the global in an extension to avoid name pollution:
Now do a global find/replace from
JSONDecoder().toJSONDecoder.shared.and fromJSONEncoder().toJSONEncoder.shared.