-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handy JSON response #50
Comments
Thank you for the suggestion 🙏🏻. I also use extensions similar to these in my own projects. As you have illustrated the difficulty is in choosing appropriate defaults for things like Date encoding and the status code to return. |
Hi @gotev I've been thinking a lot about your suggestion think there is a nice declarative solution SE-0389 Attached Macros available in Swift 5.9 and later. I'm interested in your feedback, the implementation is currently available on the branch @HTTPHandler
struct MyHandler {
static let jsonTimestampMillisEncoder: JSONEncoder = {
let jsonEncoder = JSONEncoder()
jsonEncoder.dateEncodingStrategy = .millisecondsSince1970
return jsonEncoder
}()
@JSONRoute("/cat", encoder: Self.jsonTimestampMillisEncoder)
func getCat() -> Cat {
Cat(birthDate: Date(), name: "Loris")
}
}
let server = HTTPServer(port: 80, handler: MyHandler())
try await server.start() The macro synthesises conformance to |
JSON responses are common for a web server. With this extension (serializing dates as millis by default to ease JS clients):
one can easily return a JSON ready to be consumed by another application:
If you think it's a valuable addition, feel free to add it in the library 🍻
The text was updated successfully, but these errors were encountered: