Skip to content

Commit

Permalink
Merge pull request #13 from mdales/mwd-json-date-format-iso8601
Browse files Browse the repository at this point in the history
Use ISO8601 format for JSON date encoding
  • Loading branch information
helje5 authored Aug 23, 2022
2 parents c615bbe + 0d4b1e6 commit a0f2dc7
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 a0f2dc7

Please sign in to comment.