diff --git a/frame/g/g_func.go b/frame/g/g_func.go index 23e5ae7b9..1725daceb 100644 --- a/frame/g/g_func.go +++ b/frame/g/g_func.go @@ -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) diff --git a/util/gutil/gutil_dump.go b/util/gutil/gutil_dump.go index be5ba91e1..e25509a49 100644 --- a/util/gutil/gutil_dump.go +++ b/util/gutil/gutil_dump.go @@ -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()) +} diff --git a/util/gutil/gutil_z_unit_dump_test.go b/util/gutil/gutil_z_unit_dump_test.go index 3e255e91f..04a966500 100755 --- a/util/gutil/gutil_z_unit_dump_test.go +++ b/util/gutil/gutil_z_unit_dump_test.go @@ -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) + }) +}