mirror of
https://gitee.com/johng/gf
synced 2026-07-08 14:39:50 +08:00
add DumpJson for package gutil (#2651)
This commit is contained in:
@ -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)
|
||||
|
||||
@ -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())
|
||||
}
|
||||
|
||||
@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user