github.com/night-codes/go-json@v0.9.15/option.go (about) 1 package json 2 3 import ( 4 "io" 5 6 "github.com/night-codes/go-json/internal/decoder" 7 "github.com/night-codes/go-json/internal/encoder" 8 ) 9 10 type EncodeOption = encoder.Option 11 type EncodeOptionFunc func(*EncodeOption) 12 13 // UnorderedMap doesn't sort when encoding map type. 14 func UnorderedMap() EncodeOptionFunc { 15 return func(opt *EncodeOption) { 16 opt.Flag |= encoder.UnorderedMapOption 17 } 18 } 19 20 // DisableHTMLEscape disables escaping of HTML characters ( '&', '<', '>' ) when encoding string. 21 func DisableHTMLEscape() EncodeOptionFunc { 22 return func(opt *EncodeOption) { 23 opt.Flag &= ^encoder.HTMLEscapeOption 24 } 25 } 26 27 // DisableNormalizeUTF8 28 // By default, when encoding string, UTF8 characters in the range of 0x80 - 0xFF are processed by applying \ufffd for invalid code and escaping for \u2028 and \u2029. 29 // This option disables this behaviour. You can expect faster speeds by applying this option, but be careful. 30 // encoding/json implements here: https://github.com/golang/go/blob/6178d25fc0b28724b1b5aec2b1b74fc06d9294c7/src/encoding/json/encode.go#L1067-L1093. 31 func DisableNormalizeUTF8() EncodeOptionFunc { 32 return func(opt *EncodeOption) { 33 opt.Flag &= ^encoder.NormalizeUTF8Option 34 } 35 } 36 37 // Debug outputs debug information when panic occurs during encoding. 38 func Debug() EncodeOptionFunc { 39 return func(opt *EncodeOption) { 40 opt.Flag |= encoder.DebugOption 41 } 42 } 43 44 // DebugWith sets the destination to write debug messages. 45 func DebugWith(w io.Writer) EncodeOptionFunc { 46 return func(opt *EncodeOption) { 47 opt.DebugOut = w 48 } 49 } 50 51 // Colorize add an identifier for coloring to the string of the encoded result. 52 func Colorize(scheme *ColorScheme) EncodeOptionFunc { 53 return func(opt *EncodeOption) { 54 opt.Flag |= encoder.ColorizeOption 55 opt.ColorScheme = scheme 56 } 57 } 58 59 type DecodeOption = decoder.Option 60 type DecodeOptionFunc func(*DecodeOption) 61 62 // DecodeFieldPriorityFirstWin 63 // in the default behavior, go-json, like encoding/json, 64 // will reflect the result of the last evaluation when a field with the same name exists. 65 // This option allow you to change this behavior. 66 // this option reflects the result of the first evaluation if a field with the same name exists. 67 // This behavior has a performance advantage as it allows the subsequent strings to be skipped if all fields have been evaluated. 68 func DecodeFieldPriorityFirstWin() DecodeOptionFunc { 69 return func(opt *DecodeOption) { 70 opt.Flags |= decoder.FirstWinOption 71 } 72 }