github.com/wfusion/gofusion@v1.1.14/common/utils/serialize/types.go (about) 1 package serialize 2 3 import ( 4 "encoding/gob" 5 "io" 6 7 "github.com/fxamacker/cbor/v2" 8 "github.com/vmihailenco/msgpack/v5" 9 10 "github.com/wfusion/gofusion/common/utils" 11 "github.com/wfusion/gofusion/common/utils/serialize/json" 12 ) 13 14 var ( 15 marshalFuncMap = map[Algorithm]func(dst io.Writer, src any, opt *marshalOption) (err error){ 16 AlgorithmGob: func(dst io.Writer, src any, opt *marshalOption) (err error) { 17 return gob.NewEncoder(dst).Encode(src) 18 }, 19 AlgorithmJson: func(dst io.Writer, src any, opt *marshalOption) (err error) { 20 enc := json.NewEncoder(dst) 21 if !opt.jsonEscapeHTML { 22 enc.SetEscapeHTML(false) 23 } 24 enc.SetIndent(opt.jsonIndentPrefix, opt.jsonIndent) 25 return enc.Encode(src) 26 }, 27 AlgorithmMsgpack: func(dst io.Writer, src any, opt *marshalOption) (err error) { 28 enc := msgpack.NewEncoder(dst) 29 enc.UseCompactInts(opt.msgpackUseCompactInts) 30 enc.UseCompactFloats(opt.msgpackUseCompactFloats) 31 return enc.Encode(src) 32 }, 33 AlgorithmCbor: func(dst io.Writer, src any, opt *marshalOption) (err error) { 34 return cbor.NewEncoder(dst).Encode(src) 35 }, 36 } 37 38 unmarshalFuncMap = map[Algorithm]func(dst any, src io.Reader, opt *unmarshalOption) (err error){ 39 AlgorithmGob: func(dst any, src io.Reader, opt *unmarshalOption) (err error) { 40 return gob.NewDecoder(src).Decode(dst) 41 }, 42 AlgorithmJson: func(dst any, src io.Reader, opt *unmarshalOption) (err error) { 43 dec := json.NewDecoder(src) 44 if opt.jsonNumber { 45 dec.UseNumber() 46 } 47 if opt.disallowUnknownFields { 48 dec.DisallowUnknownFields() 49 } 50 return dec.Decode(dst) 51 }, 52 AlgorithmMsgpack: func(dst any, src io.Reader, opt *unmarshalOption) (err error) { 53 dec := msgpack.NewDecoder(src) 54 dec.DisallowUnknownFields(opt.disallowUnknownFields) 55 return dec.Decode(dst) 56 }, 57 AlgorithmCbor: func(dst any, src io.Reader, opt *unmarshalOption) (err error) { 58 return cbor.NewDecoder(src).Decode(dst) 59 }, 60 } 61 ) 62 63 type marshalOption struct { 64 jsonEscapeHTML bool 65 jsonIndent, jsonIndentPrefix string 66 msgpackUseCompactInts bool 67 msgpackUseCompactFloats bool 68 } 69 70 func MsgpackUseCompactInts(on bool) utils.OptionFunc[marshalOption] { 71 return func(o *marshalOption) { 72 o.msgpackUseCompactInts = on 73 } 74 } 75 76 func MsgpackUseCompactFloats(on bool) utils.OptionFunc[marshalOption] { 77 return func(o *marshalOption) { 78 o.msgpackUseCompactFloats = on 79 } 80 } 81 82 func JsonEscapeHTML(on bool) utils.OptionFunc[marshalOption] { 83 return func(o *marshalOption) { 84 o.jsonEscapeHTML = on 85 } 86 } 87 88 func JsonIndent(prefix, indent string) utils.OptionFunc[marshalOption] { 89 return func(o *marshalOption) { 90 o.jsonIndentPrefix, o.jsonIndent = prefix, indent 91 } 92 } 93 94 type unmarshalOption struct { 95 jsonNumber bool 96 disallowUnknownFields bool 97 } 98 99 func JsonNumber() utils.OptionFunc[unmarshalOption] { 100 return func(o *unmarshalOption) { 101 o.jsonNumber = true 102 } 103 } 104 105 func DisallowUnknownFields() utils.OptionFunc[unmarshalOption] { 106 return func(o *unmarshalOption) { 107 o.disallowUnknownFields = true 108 } 109 }