diff --git a/configure/fopsConfigure.go b/configure/fopsConfigure.go index 14a0d8948..198ee5b03 100644 --- a/configure/fopsConfigure.go +++ b/configure/fopsConfigure.go @@ -7,8 +7,8 @@ import ( "net/http" "time" - "github.com/bytedance/sonic" "github.com/farseer-go/fs/core" + "github.com/farseer-go/fs/snc" ) // DomainObject 配置中心 @@ -20,7 +20,7 @@ type fopsConfigureVO struct { } func getFopsConfigure() ([]fopsConfigureVO, error) { - bodyByte, _ := sonic.Marshal(map[string]string{"AppName": core.AppName}) + bodyByte, _ := snc.Marshal(map[string]string{"AppName": core.AppName}) url := fopsServer + "configure/list" newRequest, _ := http.NewRequest("POST", url, bytes.NewReader(bodyByte)) newRequest.Header.Set("Content-Type", "application/json") diff --git a/core/apiResponse.go b/core/apiResponse.go index 6f3c1accb..3cff4cfa0 100644 --- a/core/apiResponse.go +++ b/core/apiResponse.go @@ -3,8 +3,8 @@ package core import ( "io" - "github.com/bytedance/sonic" "github.com/farseer-go/fs/dateTime" + "github.com/farseer-go/fs/snc" ) // ApiResponse 标准的API Response结构 @@ -33,13 +33,13 @@ func (receiver *ApiResponse[TData]) SetData(data TData) { // ToJson 转成Json func (receiver *ApiResponse[TData]) ToJson() string { - bytes, _ := sonic.Marshal(receiver) + bytes, _ := snc.Marshal(receiver) return string(bytes) } // ToBytes 转成Json字节 func (receiver *ApiResponse[TData]) ToBytes() []byte { - bytes, _ := sonic.Marshal(receiver) + bytes, _ := snc.Marshal(receiver) return bytes } @@ -83,6 +83,6 @@ func NewApiResponseByReader[TData any](reader io.Reader) ApiResponse[TData] { // NewApiResponseByByte 创建实例 func NewApiResponseByByte[TData any](body []byte) ApiResponse[TData] { var apiResponse ApiResponse[TData] - _ = sonic.Unmarshal(body, &apiResponse) + _ = snc.Unmarshal(body, &apiResponse) return apiResponse } diff --git a/core/eumLogLevel/enum.go b/core/eumLogLevel/enum.go index 1787b1efc..064b93320 100644 --- a/core/eumLogLevel/enum.go +++ b/core/eumLogLevel/enum.go @@ -3,7 +3,7 @@ package eumLogLevel import ( "strings" - "github.com/bytedance/sonic" + "github.com/farseer-go/fs/snc" ) // Enum 日志等级 @@ -60,13 +60,13 @@ func (receiver Enum) ToString() string { // MarshalJSON to output non base64 encoded []byte // 此处不能用指针,否则json序列化时不执行 func (receiver Enum) MarshalJSON() ([]byte, error) { - return sonic.Marshal(receiver.ToString()) + return snc.Marshal(receiver.ToString()) } // UnmarshalJSON to deserialize []byte func (receiver *Enum) UnmarshalJSON(b []byte) error { var numStr string - err := sonic.Unmarshal(b, &numStr) + err := snc.Unmarshal(b, &numStr) *receiver = GetEnum(numStr) return err } diff --git a/dateTime/dt.go b/dateTime/dt.go index 09bbed44c..30aeb1bf5 100644 --- a/dateTime/dt.go +++ b/dateTime/dt.go @@ -8,7 +8,7 @@ import ( "strings" "time" - "github.com/bytedance/sonic" + "github.com/farseer-go/fs/snc" ) type DateTime struct { @@ -212,7 +212,7 @@ func (receiver DateTime) Before(dt DateTime) bool { // MarshalJSON to output non base64 encoded []byte // 此处不能用指针,否则json序列化时不执行 func (receiver DateTime) MarshalJSON() ([]byte, error) { - return sonic.Marshal(receiver.ToString("yyyy-MM-dd hh:mm:ss")) + return snc.Marshal(receiver.ToString("yyyy-MM-dd hh:mm:ss")) } // UnmarshalJSON to deserialize []byte diff --git a/flog/fopsProvider.go b/flog/fopsProvider.go index 2dd1c1a47..f0d12c494 100644 --- a/flog/fopsProvider.go +++ b/flog/fopsProvider.go @@ -8,11 +8,11 @@ import ( "strconv" "time" - "github.com/bytedance/sonic" "github.com/farseer-go/fs/configure" "github.com/farseer-go/fs/core" "github.com/farseer-go/fs/core/eumLogLevel" "github.com/farseer-go/fs/dateTime" + "github.com/farseer-go/fs/snc" "github.com/farseer-go/fs/sonyflake" "github.com/farseer-go/fs/trace" ) @@ -85,7 +85,7 @@ type UploadRequest struct { } func (r *fopsLoggerPersistent) upload(lstLog []*LogData) error { - bodyByte, _ := sonic.Marshal(UploadRequest{List: lstLog}) + bodyByte, _ := snc.Marshal(UploadRequest{List: lstLog}) url := r.fopsServer + "flog/upload" newRequest, _ := http.NewRequest("POST", url, bytes.NewReader(bodyByte)) newRequest.Header.Set("Content-Type", "application/json") diff --git a/flog/iFormatter.go b/flog/iFormatter.go index 3202e8e08..f44480949 100644 --- a/flog/iFormatter.go +++ b/flog/iFormatter.go @@ -3,8 +3,8 @@ package flog import ( "fmt" - "github.com/bytedance/sonic" "github.com/farseer-go/fs/core/eumLogLevel" + "github.com/farseer-go/fs/snc" ) // IFormatter 日志格式 @@ -17,7 +17,7 @@ type JsonFormatter struct { } func (r JsonFormatter) Formatter(log *LogData) string { - marshal, _ := sonic.Marshal(LogData{ + marshal, _ := snc.Marshal(LogData{ CreateAt: log.CreateAt, LogLevel: log.LogLevel, Component: log.Component, diff --git a/modules/farseerKernelModule.go b/modules/farseerKernelModule.go index d67655252..8e8215ab2 100644 --- a/modules/farseerKernelModule.go +++ b/modules/farseerKernelModule.go @@ -1,13 +1,14 @@ package modules import ( + "time" + "github.com/farseer-go/fs/configure" "github.com/farseer-go/fs/container" "github.com/farseer-go/fs/core" "github.com/farseer-go/fs/flog" "github.com/farseer-go/fs/timingWheel" "github.com/farseer-go/fs/trace" - "time" ) type FarseerKernelModule struct { diff --git a/snc/json.go b/snc/json.go new file mode 100644 index 000000000..2f3742094 --- /dev/null +++ b/snc/json.go @@ -0,0 +1,30 @@ +package snc + +import ( + "github.com/bytedance/sonic" + "github.com/bytedance/sonic/option" +) + +var snc sonic.API + +func init() { + // 设置较小的缓冲区 + option.DefaultEncoderBufferSize = 32 * 1024 + snc = sonic.Config{ + CompactMarshaler: true, + UseNumber: true, + CopyString: true, + }.Froze() +} + +func Unmarshal(data []byte, v any) error { + return snc.Unmarshal(data, v) +} + +func Marshal(val any) ([]byte, error) { + return snc.Marshal(val) +} + +func MarshalIndent(v any, prefix, indent string) ([]byte, error) { + return snc.MarshalIndent(v, prefix, indent) +} diff --git a/test/apiResponse_test.go b/test/apiResponse_test.go index 3bbc2a73f..1c9203211 100644 --- a/test/apiResponse_test.go +++ b/test/apiResponse_test.go @@ -4,14 +4,14 @@ import ( "bytes" "testing" - "github.com/bytedance/sonic" "github.com/farseer-go/fs/core" + "github.com/farseer-go/fs/snc" "github.com/stretchr/testify/assert" ) func TestApiResponse(t *testing.T) { api := core.Success("成功", "nice") - apiByte, _ := sonic.Marshal(api) + apiByte, _ := snc.Marshal(api) assert.Equal(t, string(apiByte), api.ToJson()) assert.Equal(t, apiByte, api.ToBytes()) diff --git a/test/eumLogLevel_test.go b/test/eumLogLevel_test.go index f1a02a7b2..9c2797e51 100644 --- a/test/eumLogLevel_test.go +++ b/test/eumLogLevel_test.go @@ -3,8 +3,8 @@ package test import ( "testing" - "github.com/bytedance/sonic" "github.com/farseer-go/fs/core/eumLogLevel" + "github.com/farseer-go/fs/snc" "github.com/stretchr/testify/assert" ) @@ -33,7 +33,7 @@ func TestEumLogLevel(t *testing.T) { assert.Equal(t, eumLogLevel.Warning, eumLogLevel.GetEnum("Warn")) var e = eumLogLevel.Debug - b, _ := sonic.Marshal(e) + b, _ := snc.Marshal(e) e = eumLogLevel.Information _ = e.UnmarshalJSON(b) assert.True(t, e == eumLogLevel.Debug)