add DumpJson for package gutil (#2651)

This commit is contained in:
John Guo
2023-05-24 10:04:19 +08:00
committed by GitHub
parent 879283685d
commit a6fff37be8
3 changed files with 25 additions and 0 deletions

View File

@ -56,6 +56,11 @@ func DumpWithOption(value interface{}, option gutil.DumpOption) {
gutil.DumpWithOption(value, option)
}
// DumpJson pretty dumps json content to stdout.
func DumpJson(jsonContent string) {
gutil.DumpJson(jsonContent)
}
// Throw throws an exception, which can be caught by TryCatch function.
func Throw(exception interface{}) {
gutil.Throw(exception)

View File

@ -8,6 +8,7 @@ package gutil
import (
"bytes"
"encoding/json"
"fmt"
"io"
"reflect"
@ -469,3 +470,15 @@ func addSlashesForString(s string) string {
"\n": `\n`,
})
}
// DumpJson pretty dumps json content to stdout.
func DumpJson(jsonContent string) {
var (
buffer = bytes.NewBuffer(nil)
jsonBytes = []byte(jsonContent)
)
if err := json.Indent(buffer, jsonBytes, "", "\t"); err != nil {
fmt.Println(err.Error())
}
fmt.Println(buffer.String())
}

View File

@ -288,3 +288,10 @@ func Test_Dump_Cycle_Attribute(t *testing.T) {
t.Assert(gstr.Contains(buffer.String(), "cycle"), true)
})
}
func Test_DumpJson(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var jsonContent = `{"a":1,"b":2}`
gutil.DumpJson(jsonContent)
})
}