Writes an object to the response in JSON format. If status is nonzero, the header will be written with that status.
(status int, value interface{})
| 1541 | // Writes an object to the response in JSON format. |
| 1542 | // If status is nonzero, the header will be written with that status. |
| 1543 | func (h *handler) writeJSONStatus(status int, value interface{}) { |
| 1544 | if !h.requestAccepts("application/json") { |
| 1545 | base.WarnfCtx(h.ctx(), "Client won't accept JSON, only %s", h.rq.Header.Get("Accept")) |
| 1546 | h.writeStatus(http.StatusNotAcceptable, "only application/json available") |
| 1547 | return |
| 1548 | } |
| 1549 | |
| 1550 | jsonOut, err := base.JSONMarshalCanonical(value) |
| 1551 | if err != nil { |
| 1552 | base.WarnfCtx(h.ctx(), "Couldn't serialize JSON for %v : %s", base.UD(value), err) |
| 1553 | h.writeStatus(http.StatusInternalServerError, "JSON serialization failed") |
| 1554 | return |
| 1555 | } |
| 1556 | if base.ValDefault(h.server.Config.API.Pretty, false) { |
| 1557 | var buffer bytes.Buffer |
| 1558 | _ = json.Indent(&buffer, jsonOut, "", " ") |
| 1559 | jsonOut = append(buffer.Bytes(), '\n') |
| 1560 | } |
| 1561 | |
| 1562 | h.writeRawJSONWithoutClientVerification(status, jsonOut) |
| 1563 | } |
| 1564 | |
| 1565 | // writeRawJSON writes the given bytes as a JSON response with a 200 OK status. |
| 1566 | func (h *handler) writeRawJSON(b []byte) { |
no test coverage detected