Skip to content

Commit

Permalink
移除sonic
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Dec 15, 2024
1 parent 09fce5e commit a8d598f
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 26 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ go 1.21
toolchain go1.23.3

require (
github.com/bytedance/sonic v1.12.4
github.com/stretchr/testify v1.9.0
github.com/timandy/routine v1.1.4
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/bytedance/sonic/loader v0.2.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
25 changes: 1 addition & 24 deletions snc/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,20 @@ package snc
import (
"bytes"
"encoding/json"

"github.com/bytedance/sonic"
"github.com/bytedance/sonic/option"
)

var snc sonic.API

func init() {
// 设置较小的缓冲区,以减少内存使用
option.LimitBufferSize = 2 * 1024
option.DefaultDecoderBufferSize = 2 * 1024
option.DefaultEncoderBufferSize = 2 * 1024
option.DefaultAstBufferSize = 2 * 1024

snc = sonic.Config{
CompactMarshaler: true, // 输出紧凑json
NoNullSliceOrMap: true, // 空对象编码为:[] {}
UseInt64: true, // 整数对象转换为int64,否则为float64
UseNumber: true, // 不要转换成float64而是json.number
CopyString: true, // 不要引用字符串,而是复制一份出来
}.Froze()
}

// 将json转换成对象(反序列化)
func Unmarshal(data []byte, v any) error {
d := json.NewDecoder(bytes.NewReader(data))
d.UseNumber()
return d.Decode(v)
//return snc.Unmarshal(data, v)
}

// 将对象转换成json(序列化)
func Marshal(val any) ([]byte, error) {
return json.Marshal(val)
//return snc.Marshal(val)
}

func MarshalIndent(v any, prefix, indent string) ([]byte, error) {
return snc.MarshalIndent(v, prefix, indent)
return json.MarshalIndent(v, prefix, indent)
}

0 comments on commit a8d598f

Please sign in to comment.