prettify/main.go

57 lines
1.0 KiB
Go

package prettify
import (
"encoding/json"
"fmt"
"github.com/TylerBrock/colorjson"
)
// Print will beautify the output of body
func Print(body interface{}) {
b, err := json.Marshal(body)
if err != nil {
panic(err)
}
var obj map[string]interface{}
json.Unmarshal(b, &obj)
// Make a custom formatter with indent set
f := colorjson.NewFormatter()
f.Indent = 2
// Marshal the Colorized JSON
s, err := f.Marshal(obj)
if err != nil {
panic(err)
}
// Print out string to stdout
fmt.Println(string(s))
}
// PrintArray will beautify the output of an interface array (bodyArray)
func PrintArray(bodyArray interface{}) {
b, err := json.Marshal(bodyArray)
if err != nil {
panic(err)
}
var obj []map[string]interface{}
json.Unmarshal(b, &obj)
// Make a custom formatter with indent set
f := colorjson.NewFormatter()
f.Indent = 2
for i := range obj {
// Marshal the Colorized JSON
s, err := f.Marshal(obj[i])
if err != nil {
panic(err)
}
// Print out string to stdout
fmt.Println(string(s))
}
}