summaryrefslogtreecommitdiff
path: root/json.go
blob: 62894cb390e99f736c68f5c4a4106ae46b2a9d07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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)
}