github.com/3JoB/go-json@v0.10.4/option.go (about)

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