github.com/15mga/kiwi@v0.0.2-0.20240324021231-b95d5c3ac751/util/json.go (about) 1 package util 2 3 import jsoniter "github.com/json-iterator/go" 4 5 var _JsonConf = jsoniter.Config{ 6 UseNumber: true, 7 EscapeHTML: true, 8 SortMapKeys: true, 9 }.Froze() 10 11 func SetJsonConf(conf jsoniter.Config) { 12 _JsonConf = conf.Froze() 13 } 14 15 func JsonConf() jsoniter.API { 16 return _JsonConf 17 } 18 19 func JsonMarshal(o any) ([]byte, *Err) { 20 bytes, err := _JsonConf.Marshal(o) 21 if err != nil { 22 return nil, WrapErr(EcMarshallErr, err) 23 } 24 return bytes, nil 25 } 26 27 func JsonMarshalIndent(o any, prefix, indent string) ([]byte, *Err) { 28 bytes, err := _JsonConf.MarshalIndent(o, prefix, indent) 29 if err != nil { 30 return nil, WrapErr(EcMarshallErr, err) 31 } 32 return bytes, nil 33 } 34 35 func JsonUnmarshal(bytes []byte, o any) *Err { 36 return WrapErr(EcUnmarshallErr, _JsonConf.Unmarshal(bytes, o)) 37 }