package main import ( "encoding/json" "net/http" ) type envelope map[string]any // This was the original function signature without enveloping // func (app *application) writeJSON(w http.ResponseWriter, status int, data any, headers http.Header) error { func (app *application) writeJSON(w http.ResponseWriter, status int, data envelope, headers http.Header) error { // TODO set a flag for wether to indent or not. Indenting cost CPU time, more memory, and more allocs // approximately 65% more time 30% more mem. //js, err := json.Marshal(data) // Use the json.MarshalIndent() function so that whitespace is added to the encoded // JSON. Here we use no line prefix ("") and tab indents ("\t") for each element. js, err := json.MarshalIndent(data, "", "\t") if err != nil { return err } js = append(js, '\n') // At this point, we know that we won't encounter any more errors before writing the // response, so it's safe to add any headers that we want to include. We loop // through the header map and add each header to the http.ResponseWriter header map. // Note that it's OK if the provided header map is nil. Go doesn't throw an error // if you try to range over (or generally, read from) a nil map. for key, value := range headers { w.Header()[key] = value } // Add the "Content-Type: application/json" header, then write the status code and // JSON response. w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) w.Write(js) return nil }