mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-04 09:49:50 +00:00
Format JSON by default. Fixes #2243.
This commit is contained in:
parent
12add3bc66
commit
e18dcb87c4
@ -17,6 +17,7 @@ limitations under the License.
|
|||||||
package apiserver
|
package apiserver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -286,9 +287,17 @@ func writeJSON(statusCode int, codec runtime.Codec, object runtime.Object, w htt
|
|||||||
errorJSON(err, codec, w)
|
errorJSON(err, codec, w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// PR #2243: Pretty-print JSON by default.
|
||||||
|
formatted := &bytes.Buffer{}
|
||||||
|
err = json.Indent(formatted, output, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
// Note: If codec is broken, this results in an infinite recursion
|
||||||
|
errorJSON(err, codec, w)
|
||||||
|
return
|
||||||
|
}
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(statusCode)
|
w.WriteHeader(statusCode)
|
||||||
w.Write(output)
|
w.Write(formatted.Bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
// errorJSON renders an error to the response.
|
// errorJSON renders an error to the response.
|
||||||
@ -299,7 +308,8 @@ func errorJSON(err error, codec runtime.Codec, w http.ResponseWriter) {
|
|||||||
|
|
||||||
// writeRawJSON writes a non-API object in JSON.
|
// writeRawJSON writes a non-API object in JSON.
|
||||||
func writeRawJSON(statusCode int, object interface{}, w http.ResponseWriter) {
|
func writeRawJSON(statusCode int, object interface{}, w http.ResponseWriter) {
|
||||||
output, err := json.Marshal(object)
|
// PR #2243: Pretty-print JSON by default.
|
||||||
|
output, err := json.MarshalIndent(object, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user