Skip to content

Commit

Permalink
Use ISO8601 format for JSON date encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
mdales committed Aug 23, 2022
1 parent c615bbe commit 0d4b1e6
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Sources/MicroExpress/ServerResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,18 @@ public extension ServerResponse {

/// Send a Codable object as JSON to the client.
func json<T: Encodable>(_ model: T) {
let encoder = JSONEncoder()
// The default Swift date encoding isn't Javascript
// compatible, so pick a more generally accepted
// date format.
if #available(macOS 10.12, iOS 10.0, *) {
encoder.dateEncodingStrategy = .iso8601
}

// create a Data struct from the Codable object
let data : Data
do {
data = try JSONEncoder().encode(model)
data = try encoder.encode(model)
}
catch {
return handleError(error)
Expand Down

0 comments on commit 0d4b1e6

Please sign in to comment.