1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vweb: add json_pretty method (#12745)

This commit is contained in:
Toby Webb 2021-12-06 23:31:17 +01:00 committed by GitHub
parent 047f059fb8
commit ef16a8ec54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 0 deletions

View File

@ -244,6 +244,13 @@ pub fn (mut ctx Context) json<T>(j T) Result {
return Result{}
}
// Response HTTP_OK with a pretty-printed JSON result
pub fn (mut ctx Context) json_pretty<T>(j T) Result {
json_s := json.encode_pretty(j)
ctx.send_response_to_client('application/json', json_s)
return Result{}
}
// Response HTTP_OK with file as payload
pub fn (mut ctx Context) file(f_path string) Result {
ext := os.file_ext(f_path)

View File

@ -64,3 +64,15 @@ pub fn (mut app App) new_article() vweb.Result {
fn (mut app App) time() {
app.text(time.now().format())
}
fn (mut app App) time_json() {
app.json({
'time': time.now().format()
})
}
fn (mut app App) time_json_pretty() {
app.json_pretty({
'time': time.now().format()
})
}