Debian Codegraph @

option.go

raw
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: the entries of a map are written in an order which is not
14 // specified, and which may differ from the order of a range over the map.
15 func UnorderedMap() EncodeOptionFunc {
16 return func(opt *EncodeOption) {
17 opt.Flag |= encoder.UnorderedMapOption
18 }
19 }
20
21 // OptimizeFieldOrder lets the encoder order the fields of a struct as it encodes them fastest, instead of in
22 // the order of the struct as encoding/json does: the fields of the same kind ( int, uint, float64, string, bool )
23 // are put together, in the order of the first field of each kind, and are encoded without a dispatch between
24 // them; and a field of the struct's own type, if there is one, is put last, so that a list of values is encoded
25 // without a frame for each. The keys of the JSON object are the same, only their order differs, which a JSON
26 // object doesn't define.
27 func OptimizeFieldOrder() EncodeOptionFunc {
28 return func(opt *EncodeOption) {
29 opt.Flag |= encoder.OptimizeFieldOrderOption
30 }
31 }
32
33 // DisableHTMLEscape disables escaping of HTML characters ( '&', '<', '>' ) when encoding string.
34 func DisableHTMLEscape() EncodeOptionFunc {
35 return func(opt *EncodeOption) {
36 opt.Flag &= ^encoder.HTMLEscapeOption
37 }
38 }
39
40 // DisableNormalizeUTF8
41 // By default, when encoding string, UTF8 characters in the range of 0x80 - 0xFF are processed by replacing invalid code with U+FFFD and escaping \u2028 and \u2029, as encoding/json does:
42 // the replacement character is written escaped as \ufffd before Go 1.27, and as it is by Go 1.27, whose encoding/json is made of encoding/json/v2.
43 // This option disables this behaviour. You can expect faster speeds by applying this option, but be careful.
44 // encoding/json implements here: https://github.com/golang/go/blob/6178d25fc0b28724b1b5aec2b1b74fc06d9294c7/src/encoding/json/encode.go#L1067-L1093.
45 func DisableNormalizeUTF8() EncodeOptionFunc {
46 return func(opt *EncodeOption) {
47 opt.Flag &= ^encoder.NormalizeUTF8Option
48 }
49 }
50
51 // Debug outputs debug information when panic occurs during encoding.
52 func Debug() EncodeOptionFunc {
53 return func(opt *EncodeOption) {
54 opt.Flag |= encoder.DebugOption
55 }
56 }
57
58 // DebugWith sets the destination to write debug messages.
59 func DebugWith(w io.Writer) EncodeOptionFunc {
60 return func(opt *EncodeOption) {
61 opt.DebugOut = w
62 }
63 }
64
65 // DebugDOT sets the destination to write opcodes graph.
66 func DebugDOT(w io.WriteCloser) EncodeOptionFunc {
67 return func(opt *EncodeOption) {
68 opt.DebugDOTOut = w
69 }
70 }
71
72 // Colorize add an identifier for coloring to the string of the encoded result.
73 func Colorize(scheme *ColorScheme) EncodeOptionFunc {
74 return func(opt *EncodeOption) {
75 opt.Flag |= encoder.ColorizeOption
76 opt.ColorScheme = scheme
77 }
78 }
79
80 type DecodeOption = decoder.Option
81 type DecodeOptionFunc func(*DecodeOption)
82
83 // DecodeFieldPriorityFirstWin
84 // in the default behavior, go-json, like encoding/json,
85 // will reflect the result of the last evaluation when a field with the same name exists.
86 // This option allow you to change this behavior.
87 // this option reflects the result of the first evaluation if a field with the same name exists.
88 // This behavior has a performance advantage as it allows the subsequent strings to be skipped if all fields have been evaluated.
89 func DecodeFieldPriorityFirstWin() DecodeOptionFunc {
90 return func(opt *DecodeOption) {
91 opt.Flags |= decoder.FirstWinOption
92 }
93 }
94
95 // DecodeNoCopyString makes the decoded strings refer to the input instead of copies of their bytes,
96 // as sonic does by default: a string which has no escape is not copied. Then the input must not be
97 // modified while the decoded strings are used.
98 //
99 // By default, a decoded string is a copy, as with encoding/json: the input may be modified or reused
100 // after the call. The copies of the short strings share buffers of up to 16 KB, so a decoded string keeps
101 // at most 16 KB alive with it.
102 func DecodeNoCopyString() DecodeOptionFunc {
103 return func(opt *DecodeOption) {
104 opt.Flags |= decoder.NoCopyStringOption
105 }
106 }