vweb: add json_pretty method (#12745)
parent
047f059fb8
commit
ef16a8ec54
vlib/vweb
|
@ -244,6 +244,13 @@ pub fn (mut ctx Context) json<T>(j T) Result {
|
||||||
return 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
|
// Response HTTP_OK with file as payload
|
||||||
pub fn (mut ctx Context) file(f_path string) Result {
|
pub fn (mut ctx Context) file(f_path string) Result {
|
||||||
ext := os.file_ext(f_path)
|
ext := os.file_ext(f_path)
|
||||||
|
|
|
@ -64,3 +64,15 @@ pub fn (mut app App) new_article() vweb.Result {
|
||||||
fn (mut app App) time() {
|
fn (mut app App) time() {
|
||||||
app.text(time.now().format())
|
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()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue