github.com/goccy/go-json@v0.10.3-0.20240509105655-5e2ae3f23c1d/option.go (about)

     1  package json
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/goccy/go-json/internal/decoder"
     7  	"github.com/goccy/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  // DebugDOT sets the destination to write opcodes graph.
    52  func DebugDOT(w io.WriteCloser) EncodeOptionFunc {
    53  	return func(opt *EncodeOption) {
    54  		opt.DebugDOTOut = w
    55  	}
    56  }
    57  
    58  // Colorize add an identifier for coloring to the string of the encoded result.
    59  func Colorize(scheme *ColorScheme) EncodeOptionFunc {
    60  	return func(opt *EncodeOption) {
    61  		opt.Flag |= encoder.ColorizeOption
    62  		opt.ColorScheme = scheme
    63  	}
    64  }
    65  
    66  type DecodeOption = decoder.Option
    67  type DecodeOptionFunc func(*DecodeOption)
    68  
    69  // DecodeFieldPriorityFirstWin
    70  // in the default behavior, go-json, like encoding/json,
    71  // will reflect the result of the last evaluation when a field with the same name exists.
    72  // This option allow you to change this behavior.
    73  // this option reflects the result of the first evaluation if a field with the same name exists.
    74  // This behavior has a performance advantage as it allows the subsequent strings to be skipped if all fields have been evaluated.
    75  func DecodeFieldPriorityFirstWin() DecodeOptionFunc {
    76  	return func(opt *DecodeOption) {
    77  		opt.Flags |= decoder.FirstWinOption
    78  	}
    79  }