Skip to content

Commit

Permalink
Use JSON encoders where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
patapancakes committed Nov 21, 2024
1 parent d2c86fa commit 0b93c46
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
8 changes: 3 additions & 5 deletions api/news/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
8 changes: 3 additions & 5 deletions api/packages/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
8 changes: 3 additions & 5 deletions api/packages/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 0b93c46

Please sign in to comment.