While I am on a Vapor based back-end in Swift, I am noticing that the Webhook is not sending a valid ISO8601 Date and Swift’s JSONDecoder is unable to decode them using the iso8601 decoding strategy.
Dates are being sent in the following style: 2018-02-22T13:42:47.483Z
Take the following Swift / Xcode playground as an example:
// define the json to test
let json = "{\"published_at\":\"2018-02-22T13:42:47.483Z\"}"
// Dummy Swift Model
struct MyModel: Codable {
var publishedAt: Date
enum CodingKeys: String, CodingKey {
case publishedAt = "published_at"
}
}
// Swift 4 JSON Decoder
var decoder = JSONDecoder()
if #available(OSX 10.12, *) {
// Decode dates as ISO8601
decoder.dateDecodingStrategy = .iso8601
}
// Get a Data representation of the JSON string
if let data = json.data(using: .utf8) {
// decode the JSON data using the ISO8601 strategy
do {
// this call can throw
let foo = try decoder.decode(MyModel.self, from: data)
// dump foo if we successfully decoded the json data
print("Success!")
dump(foo)
} catch {
print("Failure :'(")
dump(error)
}
}
This will actually fail with the following error:
Failure :'(
▿ Swift.DecodingError.dataCorrupted
▿ dataCorrupted: Swift.DecodingError.Context
▿ codingPath: 1 element
- __lldb_expr_113.MyModel.CodingKeys.publishedAt
- debugDescription: "Expected date string to be ISO8601-formatted."
- underlyingError: nil
Of course I can create my own custom date formatter to handle this, but as this is documented as being an ISO8601 date, this should work.