1
This commit is contained in:
36
pkg/jsonhelper/jsonhelper.go
Normal file
36
pkg/jsonhelper/jsonhelper.go
Normal file
@ -0,0 +1,36 @@
|
||||
package jsonhelper
|
||||
|
||||
import (
|
||||
"github.com/bytedance/sonic"
|
||||
jsonIterator "github.com/json-iterator/go"
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
)
|
||||
|
||||
var (
|
||||
IJson = jsonIterator.ConfigCompatibleWithStandardLibrary
|
||||
// Marshal is exported by common package.
|
||||
Marshal = IJson.Marshal
|
||||
// Unmarshal is exported by common package.
|
||||
Unmarshal = IJson.Unmarshal
|
||||
// MarshalIndent is exported by common package.
|
||||
MarshalIndent = IJson.MarshalIndent
|
||||
// NewDecoder is exported by common package.
|
||||
NewDecoder = IJson.NewDecoder
|
||||
// NewEncoder is exported by common package.
|
||||
NewEncoder = IJson.NewEncoder
|
||||
|
||||
// MarshalMsgPack msgpack方式序列化
|
||||
MarshalMsgPack = msgpack.Marshal
|
||||
// msgpack方式反序列化
|
||||
UnmarshalMsgPack = msgpack.Unmarshal
|
||||
NewDecoderMsgPack = msgpack.NewDecoder
|
||||
NewEncoderMsgPack = msgpack.NewEncoder
|
||||
)
|
||||
|
||||
func ToJsonString(v interface{}) string {
|
||||
if result, err := sonic.Marshal(v); err == nil {
|
||||
return string(result)
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
31
pkg/jsonhelper/jsonhelper_test.go
Normal file
31
pkg/jsonhelper/jsonhelper_test.go
Normal file
@ -0,0 +1,31 @@
|
||||
package jsonhelper
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var jsonStr = `{"W":1561651}`
|
||||
|
||||
var obj struct {
|
||||
W int
|
||||
}
|
||||
|
||||
// 对比下 IJson 和 系统自带的 json 的效率
|
||||
// go test -bench=_QE_ -benchmem -run=^$
|
||||
// -benchtime 默认为1秒 -benchmem 获得内存分配的统计数据
|
||||
func Benchmark_QE_1(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
json.Unmarshal([]byte(jsonStr), &obj)
|
||||
}
|
||||
}
|
||||
|
||||
func Benchmark_QE_2(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
Unmarshal([]byte(jsonStr), &obj)
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark_QE_1-6 2252055 531.2 ns/op 240 B/op 6 allocs/op
|
||||
// Benchmark_QE_2-6 7650109 158.7 ns/op 16 B/op 1 allocs/op
|
||||
// 确实快了很多 不知道复杂的结构会不会不一样
|
||||
Reference in New Issue
Block a user