bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/util/json.go (about) 1 package util 2 3 import ( 4 "bytes" 5 "compress/gzip" 6 "encoding/json" 7 ) 8 9 func MarshalGzipJson(data interface{}) ([]byte, error) { 10 buf := &bytes.Buffer{} 11 g := gzip.NewWriter(buf) 12 enc := json.NewEncoder(g) 13 if err := enc.Encode(data); err != nil { 14 return nil, err 15 } 16 if err := g.Flush(); err != nil { 17 return nil, err 18 } 19 return buf.Bytes(), nil 20 } 21 22 func UnmarshalGzipJson(b []byte, dst interface{}) error { 23 r := bytes.NewReader(b) 24 g, err := gzip.NewReader(r) 25 if err != nil { 26 return err 27 } 28 dec := json.NewDecoder(g) 29 return dec.Decode(dst) 30 }