代码拉取完成,页面将自动刷新
package encoding
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"github.com/golang/snappy"
)
// JSONEncoding json format
type JSONEncoding struct{}
// Marshal json encode
func (j JSONEncoding) Marshal(v interface{}) ([]byte, error) {
buf, err := json.Marshal(v)
return buf, err
}
// Unmarshal json decode
func (j JSONEncoding) Unmarshal(data []byte, value interface{}) error {
err := json.Unmarshal(data, value)
if err != nil {
return err
}
return nil
}
// JSONGzipEncoding json and gzip
type JSONGzipEncoding struct{}
// Marshal json encode and gzip
func (jz JSONGzipEncoding) Marshal(v interface{}) ([]byte, error) {
buf, err := json.Marshal(v)
if err != nil {
return nil, err
}
// var bufSizeBefore = len(buf)
buf, err = GzipEncode(buf)
// log.Infof("gzip_json_compress_ratio=%d/%d=%.2f", bufSizeBefore, len(buf), float64(bufSizeBefore)/float64(len(buf)))
return buf, err
}
// Unmarshal json encode and gzip
func (jz JSONGzipEncoding) Unmarshal(data []byte, value interface{}) error {
jsonData, err := GzipDecode(data)
if err != nil {
return err
}
err = json.Unmarshal(jsonData, value)
if err != nil {
return err
}
return nil
}
// GzipEncode encoding
func GzipEncode(in []byte) ([]byte, error) {
var (
buffer bytes.Buffer
out []byte
err error
)
writer, err := gzip.NewWriterLevel(&buffer, gzip.BestCompression)
if err != nil {
return nil, err
}
_, err = writer.Write(in)
if err != nil {
err = writer.Close()
if err != nil {
return out, err
}
return out, err
}
err = writer.Close()
if err != nil {
return out, err
}
return buffer.Bytes(), nil
}
// GzipDecode decode
func GzipDecode(in []byte) ([]byte, error) {
reader, err := gzip.NewReader(bytes.NewReader(in))
if err != nil {
var out []byte
return out, err
}
defer func() {
err = reader.Close()
if err != nil {
fmt.Printf("reader close err: %+v", err)
}
}()
return io.ReadAll(reader)
}
// JSONSnappyEncoding json format and snappy compression
type JSONSnappyEncoding struct{}
// Marshal serialization
func (s JSONSnappyEncoding) Marshal(v interface{}) (data []byte, err error) {
b, err := json.Marshal(v)
if err != nil {
return nil, err
}
d := snappy.Encode(nil, b)
return d, nil
}
// Unmarshal deserialization
func (s JSONSnappyEncoding) Unmarshal(data []byte, value interface{}) error {
b, err := snappy.Decode(nil, data)
if err != nil {
return err
}
return json.Unmarshal(b, value)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。