mirror of
https://github.com/go-eagle/eagle.git
synced 2025-09-26 20:41:26 +08:00

* chore: improve repo template gen * chore: import cache template gen * chore: import protoc-gen-go-gin template gen * feat: add sonic json encoding * docs: add feature Co-authored-by: lvjiapeng <319161@myj.com.cn>
46 lines
905 B
Go
46 lines
905 B
Go
package encoding
|
|
|
|
import (
|
|
"github.com/bytedance/sonic"
|
|
"github.com/golang/snappy"
|
|
)
|
|
|
|
type SonicEncoding struct {
|
|
}
|
|
|
|
func (s SonicEncoding) Marshal(v interface{}) ([]byte, error) {
|
|
buf, err := sonic.Marshal(v)
|
|
return buf, err
|
|
}
|
|
|
|
func (s SonicEncoding) Unmarshal(data []byte, value interface{}) error {
|
|
err := sonic.Unmarshal(data, value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SonicSnappyEncoding Sonic json格式和snappy压缩
|
|
type SonicSnappyEncoding struct{}
|
|
|
|
// Marshal 序列化
|
|
func (s SonicSnappyEncoding) Marshal(v interface{}) (data []byte, err error) {
|
|
b, err := sonic.Marshal(v)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
d := snappy.Encode(nil, b)
|
|
return d, nil
|
|
}
|
|
|
|
// Unmarshal 反序列化
|
|
func (s SonicSnappyEncoding) Unmarshal(data []byte, value interface{}) error {
|
|
b, err := snappy.Decode(nil, data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return sonic.Unmarshal(b, value)
|
|
}
|