diff --git a/net.go b/net.go index 419476f..7c62d29 100644 --- a/net.go +++ b/net.go @@ -1,6 +1,7 @@ package ksrv import ( + "encoding/json" "net/http" "strconv" ) @@ -35,6 +36,15 @@ func Response(w http.ResponseWriter, status int, body []byte) (int, error) { return w.Write(body) } +func JSON(w http.ResponseWriter, value interface{}) { + data, err := json.Marshal(value) + if err != nil { + panic(err) + } + w.Header().Set("Content-Type", "application/json") + w.Write(data) +} + // Abort shorthand for aborting with error, strings and status code could also be passed func Abort(w http.ResponseWriter, errs ...interface{}) (int, error) { code := 500