ahmetb / go-httpbin

http://httpbin.org endpoints for your Go tests

Home Page:https://godoc.org/github.com/ahmetalpbalkan/go-httpbin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add pretty output for json

egorsmkv opened this issue · comments

Add pretty output for json

@egorsmkv nice catch. Would you like to contribute? It should be as simple as extracting the encoder into a variable and calling SetIndent("", " ") here?

@ahmetalpbalkan Yes. I'll send a PR after some time.

I just patched like this, additionally eliminating the error check (errors.Wrap handles that):

commit 8be476d20feeee97a216596fa63ed5a791d33812
Author: Ahmet Alp Balkan <ahmetalpbalkan@gmail.com>
Date:   Wed Nov 16 14:34:25 2016 -0800

    Indent JSON responses, eliminate redundant err chk

    Signed-off-by: Ahmet Alp Balkan <ahmetalpbalkan@gmail.com>

diff --git a/util.go b/util.go
index cc414e3..fa75ac9 100644
--- a/util.go
+++ b/util.go
@@ -10,10 +10,9 @@ import (
 )

 func writeJSON(w io.Writer, v interface{}) error {
-   if err := json.NewEncoder(w).Encode(v); err != nil {
-       return errors.Wrap(err, "failed to encode JSON")
-   }
-   return nil
+   e := json.NewEncoder(w)
+   e.SetIndent("", "  ")
+   return errors.Wrap(e.Encode(v), "failed to encode JSON")
 }

 func writeErrorJSON(w http.ResponseWriter, err error) {

Want me to just go ahead and submit?

Yes, patch.