prettify/main.go

37 lines
700 B
Go
Raw Permalink Normal View History

2020-05-10 13:36:48 +00:00
package prettify
import (
"encoding/json"
"fmt"
"github.com/TylerBrock/colorjson"
)
2020-05-10 14:03:39 +00:00
// Print will beautify the output of body
2020-05-10 13:38:17 +00:00
func Print(body interface{}) {
2020-05-10 13:36:48 +00:00
b, err := json.Marshal(body)
var obj map[string]interface{}
//json.Unmarshal([]byte(body), &obj)
json.Unmarshal(b, &obj)
// Make a custom formatter with indent set
f := colorjson.NewFormatter()
f.Indent = 2
// Marshall the Colorized JSON
s, err := f.Marshal(obj)
if err != nil {
panic(err)
}
2020-08-03 19:45:34 +00:00
// Print out string to stdout
2020-05-10 13:36:48 +00:00
fmt.Println(string(s))
}
// PrintArray will beautify the output of an interface array (bodyArray)
func PrintArray(bodyArray []interface{}) {
for i := range bodyArray {
Print(bodyArray[i])
}
}