summaryrefslogtreecommitdiff
path: root/json.go
diff options
context:
space:
mode:
authorEthel Morgan <eth@ethulhu.co.uk>2020-07-07 16:01:10 +0100
committerEthel Morgan <eth@ethulhu.co.uk>2020-07-07 16:01:10 +0100
commit8e97a7dbf08d8930a440daa49e71d73822bde205 (patch)
tree824fc839b0551321b858ce1d7b2ecaeae6eddfa4 /json.go
parentb2ec81445874882023280901f8304d73b4e1b6eb (diff)
import MustWriteJSONv0.0.1
Diffstat (limited to 'json.go')
-rw-r--r--json.go21
1 files changed, 21 insertions, 0 deletions
diff --git a/json.go b/json.go
new file mode 100644
index 0000000..62894cb
--- /dev/null
+++ b/json.go
@@ -0,0 +1,21 @@
+// SPDX-FileCopyrightText: 2020 Ethel Morgan
+//
+// SPDX-License-Identifier: MIT
+
+package httputil
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+)
+
+// MustWriteJSON writes data to w, panicking if it cannot.
+// It panics because being unable to marshal JSON is programmer error, and not recoverable.
+func MustWriteJSON(w http.ResponseWriter, data interface{}) {
+ blob, err := json.Marshal(data)
+ if err != nil {
+ panic(fmt.Sprintf("could not marshal JSON: %v", err))
+ }
+ w.Write(blob)
+}