26 lines
592 B
Go
26 lines
592 B
Go
|
package eighty
|
||
|
|
||
|
import (
|
||
|
"amuz.es/src/go/misc"
|
||
|
"github.com/valyala/fasthttp"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
jsonMimeType = "application/json; charset=utf-8"
|
||
|
)
|
||
|
|
||
|
// DumpJSONFasthttp is a simple JSON renderer for the fasthttp.
|
||
|
func DumpJSONFasthttp(ctx *fasthttp.RequestCtx, code int, serializable any) {
|
||
|
stream := misc.JSONCodec.BorrowStream(nil)
|
||
|
defer misc.JSONCodec.ReturnStream(stream)
|
||
|
|
||
|
if stream.WriteVal(serializable); stream.Error != nil {
|
||
|
panic(stream.Error)
|
||
|
} else if _, err := ctx.Write(stream.Buffer()); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
ctx.SetContentType(jsonMimeType)
|
||
|
ctx.SetStatusCode(code)
|
||
|
}
|