feat(json): improve JSON performance with bytedance/sonic

This commit is contained in:
yusing
2025-09-29 17:43:34 +08:00
parent 024100aa8c
commit f411e17d80
29 changed files with 86 additions and 78 deletions

View File

@@ -1,7 +1,6 @@
package serialization
import (
"encoding/json"
"errors"
"os"
"reflect"
@@ -10,6 +9,7 @@ import (
"strings"
"time"
"github.com/bytedance/sonic"
"github.com/go-playground/validator/v10"
"github.com/goccy/go-yaml"
"github.com/puzpuzpuz/xsync/v4"
@@ -594,7 +594,7 @@ func loadSerialized[T any](path string, dst *T, deserialize func(data []byte, ds
}
func SaveJSON[T any](path string, src *T, perm os.FileMode) error {
data, err := json.Marshal(src)
data, err := sonic.Marshal(src)
if err != nil {
return err
}
@@ -602,7 +602,7 @@ func SaveJSON[T any](path string, src *T, perm os.FileMode) error {
}
func LoadJSONIfExist[T any](path string, dst *T) error {
err := loadSerialized(path, dst, json.Unmarshal)
err := loadSerialized(path, dst, sonic.Unmarshal)
if os.IsNotExist(err) {
return nil
}