diff --git a/helper/converter.go b/helper/converter.go index 8fec0bf..2656c8d 100644 --- a/helper/converter.go +++ b/helper/converter.go @@ -3,12 +3,14 @@ package helper import ( "errors" "fmt" - json "github.com/bytedance/sonic" + "github.com/bytedance/sonic" "github.com/hdt3213/rdb/core" "github.com/hdt3213/rdb/model" "os" ) +var jsonEncoder = sonic.ConfigDefault + // ToJsons read rdb file and convert to json file func ToJsons(rdbFilename string, jsonFilename string, options ...interface{}) error { if rdbFilename == "" { @@ -44,7 +46,7 @@ func ToJsons(rdbFilename string, jsonFilename string, options ...interface{}) er } empty := true err = dec.Parse(func(object model.RedisObject) bool { - data, err := json.Marshal(object) + data, err := jsonEncoder.Marshal(object) // enable SortMapKeys to ensure same result if err != nil { fmt.Printf("json marshal failed: %v", err) return true diff --git a/helper/converter_test.go b/helper/converter_test.go index f4e6d93..bd839af 100644 --- a/helper/converter_test.go +++ b/helper/converter_test.go @@ -2,6 +2,7 @@ package helper import ( "bufio" + "github.com/bytedance/sonic" "os" "path/filepath" "testing" @@ -48,6 +49,8 @@ func compareFileByLine(t *testing.T, fn1, fn2 string) (bool, error) { } func TestToJson(t *testing.T) { + // SortMapKeys will cause performance losses, only enabled during test + jsonEncoder = sonic.ConfigStd // use same time zone to ensure RedisObject.Expiration has same json value var cstZone = time.FixedZone("CST", 8*3600) time.Local = cstZone