AFAICS, the solution is simple. We just need to change one line of JSONEncoder.swift.
Where the _iso8601Formatter
is being created:
private var _iso8601Formatter: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = .withInternetDateTime
return formatter
}()
needs to be changed to:
private var _iso8601Formatter: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
return formatter
}()
But by this change 'fractions of second' becomes mandatory. A workaround could be adding a associated value to .iso8601
case where we can specify if withFractionalSeconds
must be included in formatOptions
or not. A less efficient solution is trying to decode data without .withFractionalSeconds
first and if the result is nil, trying again but this time with .withFractionalSeconds
. Also, we can add a new case to DateEncodingStrategy
, .iso8601FractionalSeconds
.
Any way, I guess the right direction is to pitch a proposal in this forum.