From 0b93c46b71dbf5babb77569b75cc3c686e86bc5f Mon Sep 17 00:00:00 2001 From: Pancakes Date: Thu, 21 Nov 2024 16:08:39 -0500 Subject: [PATCH] Use JSON encoders where possible --- api/news/list.go | 8 +++----- api/packages/get.go | 8 +++----- api/packages/list.go | 8 +++----- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/api/news/list.go b/api/news/list.go index d05e85f..008368c 100644 --- a/api/news/list.go +++ b/api/news/list.go @@ -16,12 +16,10 @@ func List(w http.ResponseWriter, r *http.Request) { return } - resp, err := json.Marshal(entries) + w.Header().Set("Content-Type", "application/json") + err = json.NewEncoder(w).Encode(entries) if err != nil { - utils.WriteError(w, r, fmt.Sprintf("failed to marshal response: %s", err)) + utils.WriteError(w, r, fmt.Sprintf("failed to encode response: %s", err)) return } - - w.Header().Set("Content-Type", "application/json") - w.Write(resp) } diff --git a/api/packages/get.go b/api/packages/get.go index df7ca88..e8bb690 100644 --- a/api/packages/get.go +++ b/api/packages/get.go @@ -50,12 +50,10 @@ func Get(w http.ResponseWriter, r *http.Request) { return } - resp, err := json.Marshal(pkg) + w.Header().Set("Content-Type", "application/json") + err = json.NewEncoder(w).Encode(pkg) if err != nil { - utils.WriteError(w, r, fmt.Sprintf("failed to marshal response: %s", err)) + utils.WriteError(w, r, fmt.Sprintf("failed to encode response: %s", err)) return } - - w.Header().Set("Content-Type", "application/json") - w.Write(resp) } diff --git a/api/packages/list.go b/api/packages/list.go index e9b0e12..eeac46d 100644 --- a/api/packages/list.go +++ b/api/packages/list.go @@ -67,12 +67,10 @@ func List(w http.ResponseWriter, r *http.Request) { return } - resp, err := json.Marshal(list) + w.Header().Set("Content-Type", "application/json") + err = json.NewEncoder(w).Encode(list) if err != nil { - utils.WriteError(w, r, fmt.Sprintf("failed to marshal response: %s", err)) + utils.WriteError(w, r, fmt.Sprintf("failed to encode response: %s", err)) return } - - w.Header().Set("Content-Type", "application/json") - w.Write(resp) }