Debian Codegraph @

pkg/internal/third_party/go-json-experiment/json/jsontext/options.go

raw
1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package jsontext
6
7 import (
8 "strings"
9
10 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json/internal/jsonflags"
11 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json/internal/jsonopts"
12 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json/internal/jsonwire"
13 )
14
15 // Options configures [NewEncoder], [Encoder.Reset], [NewDecoder],
16 // and [Decoder.Reset] with specific features.
17 // Each function takes in a variadic list of options, where properties
18 // set in latter options override the value of previously set properties.
19 //
20 // There is a single Options type, which is used with both encoding and decoding.
21 // Some options affect both operations, while others only affect one operation:
22 //
23 // - [AllowDuplicateNames] affects encoding and decoding
24 // - [AllowInvalidUTF8] affects encoding and decoding
25 // - [EscapeForHTML] affects encoding only
26 // - [EscapeForJS] affects encoding only
27 // - [PreserveRawStrings] affects encoding only
28 // - [CanonicalizeRawInts] affects encoding only
29 // - [CanonicalizeRawFloats] affects encoding only
30 // - [ReorderRawObjects] affects encoding only
31 // - [SpaceAfterColon] affects encoding only
32 // - [SpaceAfterComma] affects encoding only
33 // - [Multiline] affects encoding only
34 // - [WithIndent] affects encoding only
35 // - [WithIndentPrefix] affects encoding only
36 //
37 // Options that do not affect a particular operation are ignored.
38 //
39 // The Options type is identical to [encoding/json.Options] and
40 // [encoding/json/v2.Options]. Options from the other packages may
41 // be passed to functionality in this package, but are ignored.
42 // Options from this package may be used with the other packages.
43 type Options = jsonopts.Options
44
45 // AllowDuplicateNames specifies that JSON objects may contain
46 // duplicate member names. Disabling the duplicate name check may provide
47 // performance benefits, but breaks compliance with RFC 7493, section 2.3.
48 // The input or output will still be compliant with RFC 8259,
49 // which leaves the handling of duplicate names as unspecified behavior.
50 //
51 // This affects either encoding or decoding.
52 func AllowDuplicateNames(v bool) Options {
53 if v {
54 return jsonflags.AllowDuplicateNames | 1
55 } else {
56 return jsonflags.AllowDuplicateNames | 0
57 }
58 }
59
60 // AllowInvalidUTF8 specifies that JSON strings may contain invalid UTF-8,
61 // which will be mangled as the Unicode replacement character, U+FFFD.
62 // This causes the encoder or decoder to break compliance with
63 // RFC 7493, section 2.1, and RFC 8259, section 8.1.
64 //
65 // This affects either encoding or decoding.
66 func AllowInvalidUTF8(v bool) Options {
67 if v {
68 return jsonflags.AllowInvalidUTF8 | 1
69 } else {
70 return jsonflags.AllowInvalidUTF8 | 0
71 }
72 }
73
74 // EscapeForHTML specifies that '<', '>', and '&' characters within JSON strings
75 // should be escaped as a hexadecimal Unicode codepoint (e.g., \u003c) so that
76 // the output is safe to embed within HTML.
77 //
78 // This only affects encoding and is ignored when decoding.
79 func EscapeForHTML(v bool) Options {
80 if v {
81 return jsonflags.EscapeForHTML | 1
82 } else {
83 return jsonflags.EscapeForHTML | 0
84 }
85 }
86
87 // EscapeForJS specifies that U+2028 and U+2029 characters within JSON strings
88 // should be escaped as a hexadecimal Unicode codepoint (e.g., \u2028) so that
89 // the output is valid to embed within JavaScript. See RFC 8259, section 12.
90 //
91 // This only affects encoding and is ignored when decoding.
92 func EscapeForJS(v bool) Options {
93 if v {
94 return jsonflags.EscapeForJS | 1
95 } else {
96 return jsonflags.EscapeForJS | 0
97 }
98 }
99
100 // PreserveRawStrings specifies that when encoding a raw JSON string in a
101 // [Token] or [Value], pre-escaped sequences
102 // in a JSON string are preserved to the output.
103 // However, raw strings still respect [EscapeForHTML] and [EscapeForJS]
104 // such that the relevant characters are escaped.
105 // If [AllowInvalidUTF8] is enabled, bytes of invalid UTF-8
106 // are preserved to the output.
107 //
108 // This only affects encoding and is ignored when decoding.
109 func PreserveRawStrings(v bool) Options {
110 if v {
111 return jsonflags.PreserveRawStrings | 1
112 } else {
113 return jsonflags.PreserveRawStrings | 0
114 }
115 }
116
117 // CanonicalizeRawInts specifies that when encoding a raw JSON
118 // integer number (i.e., a number without a fraction and exponent) in a
119 // [Token] or [Value], the number is canonicalized
120 // according to RFC 8785, section 3.2.2.3. As a special case,
121 // the number -0 is canonicalized as 0.
122 //
123 // JSON numbers are treated as IEEE 754 double precision numbers.
124 // Any numbers with precision beyond what is representable by that form
125 // will lose their precision when canonicalized. For example,
126 // integer values beyond ±2⁵³ will lose their precision.
127 // For example, 1234567890123456789 is formatted as 1234567890123456800.
128 //
129 // This only affects encoding and is ignored when decoding.
130 func CanonicalizeRawInts(v bool) Options {
131 if v {
132 return jsonflags.CanonicalizeRawInts | 1
133 } else {
134 return jsonflags.CanonicalizeRawInts | 0
135 }
136 }
137
138 // CanonicalizeRawFloats specifies that when encoding a raw JSON
139 // floating-point number (i.e., a number with a fraction or exponent) in a
140 // [Token] or [Value], the number is canonicalized
141 // according to RFC 8785, section 3.2.2.3. As a special case,
142 // the number -0 is canonicalized as 0.
143 //
144 // JSON numbers are treated as IEEE 754 double precision numbers.
145 // It is safe to canonicalize a serialized single precision number and
146 // parse it back as a single precision number and expect the same value.
147 // If a number exceeds ±1.7976931348623157e+308, which is the maximum
148 // finite number, then it saturated at that value and formatted as such.
149 //
150 // This only affects encoding and is ignored when decoding.
151 func CanonicalizeRawFloats(v bool) Options {
152 if v {
153 return jsonflags.CanonicalizeRawFloats | 1
154 } else {
155 return jsonflags.CanonicalizeRawFloats | 0
156 }
157 }
158
159 // ReorderRawObjects specifies that when encoding a raw JSON object in a
160 // [Value], the object members are reordered according to
161 // RFC 8785, section 3.2.3.
162 //
163 // This only affects encoding and is ignored when decoding.
164 func ReorderRawObjects(v bool) Options {
165 if v {
166 return jsonflags.ReorderRawObjects | 1
167 } else {
168 return jsonflags.ReorderRawObjects | 0
169 }
170 }
171
172 // SpaceAfterColon specifies that the JSON output should emit a space character
173 // after each colon separator following a JSON object name.
174 // If false, then no space character appears after the colon separator.
175 //
176 // This only affects encoding and is ignored when decoding.
177 func SpaceAfterColon(v bool) Options {
178 if v {
179 return jsonflags.SpaceAfterColon | 1
180 } else {
181 return jsonflags.SpaceAfterColon | 0
182 }
183 }
184
185 // SpaceAfterComma specifies that the JSON output should emit a space character
186 // after each comma separator following a JSON object value or array element.
187 // If false, then no space character appears after the comma separator.
188 //
189 // This only affects encoding and is ignored when decoding.
190 func SpaceAfterComma(v bool) Options {
191 if v {
192 return jsonflags.SpaceAfterComma | 1
193 } else {
194 return jsonflags.SpaceAfterComma | 0
195 }
196 }
197
198 // Multiline specifies that the JSON output should expand to multiple lines,
199 // where every JSON object member or JSON array element appears on
200 // a new, indented line according to the nesting depth.
201 //
202 // If [SpaceAfterColon] is not specified, then the default is true.
203 // If [SpaceAfterComma] is not specified, then the default is false.
204 // If [WithIndent] is not specified, then the default is "\t".
205 //
206 // If set to false, then the output is a single-line,
207 // where the only whitespace emitted is determined by the current
208 // values of [SpaceAfterColon] and [SpaceAfterComma].
209 //
210 // This only affects encoding and is ignored when decoding.
211 func Multiline(v bool) Options {
212 if v {
213 return jsonflags.Multiline | 1
214 } else {
215 return jsonflags.Multiline | 0
216 }
217 }
218
219 // WithIndent specifies that the encoder should emit multiline output
220 // where each element in a JSON object or array begins on a new, indented line
221 // beginning with the indent prefix (see [WithIndentPrefix])
222 // followed by one or more copies of indent according to the nesting depth.
223 // The indent must only be composed of space or tab characters.
224 //
225 // If the intent to emit indented output without a preference for
226 // the particular indent string, then use [Multiline] instead.
227 //
228 // This only affects encoding and is ignored when decoding.
229 // Use of this option implies [Multiline] being set to true.
230 func WithIndent(indent string) Options {
231 // Fast-path: Return a constant for common indents, which avoids allocating.
232 // These are derived from analyzing the Go module proxy on 2023-07-01.
233 switch indent {
234 case "\t":
235 return jsonopts.Indent("\t") // ~14k usages
236 case " ":
237 return jsonopts.Indent(" ") // ~18k usages
238 case " ":
239 return jsonopts.Indent(" ") // ~1.7k usages
240 case " ":
241 return jsonopts.Indent(" ") // ~52k usages
242 case " ":
243 return jsonopts.Indent(" ") // ~12k usages
244 case "":
245 return jsonopts.Indent("") // ~1.5k usages
246 }
247
248 // Otherwise, allocate for this unique value.
249 if s := strings.Trim(indent, " \t"); len(s) > 0 {
250 panic("json: invalid character " + jsonwire.QuoteRune(s) + " in indent")
251 }
252 return jsonopts.Indent(indent)
253 }
254
255 // WithIndentPrefix specifies that the encoder should emit multiline output
256 // where each element in a JSON object or array begins on a new, indented line
257 // beginning with the indent prefix followed by one or more copies of indent
258 // (see [WithIndent]) according to the nesting depth.
259 // The prefix must only be composed of space or tab characters.
260 //
261 // This only affects encoding and is ignored when decoding.
262 // Use of this option implies [Multiline] being set to true.
263 func WithIndentPrefix(prefix string) Options {
264 if s := strings.Trim(prefix, " \t"); len(s) > 0 {
265 panic("json: invalid character " + jsonwire.QuoteRune(s) + " in indent prefix")
266 }
267 return jsonopts.IndentPrefix(prefix)
268 }
269
270 /*
271 // TODO(https://go.dev/issue/56733): Implement WithByteLimit and WithDepthLimit.
272 // Remember to also update the "Security Considerations" section.
273
274 // WithByteLimit sets a limit on the number of bytes of input or output bytes
275 // that may be consumed or produced for each top-level JSON value.
276 // If a [Decoder] or [Encoder] method call would need to consume/produce
277 // more than a total of n bytes to make progress on the top-level JSON value,
278 // then the call will report an error.
279 // Whitespace before and within the top-level value are counted against the limit.
280 // Whitespace after a top-level value are counted against the limit
281 // for the next top-level value.
282 //
283 // A non-positive limit is equivalent to no limit at all.
284 // If unspecified, the default limit is no limit at all.
285 // This affects either encoding or decoding.
286 func WithByteLimit(n int64) Options {
287 return jsonopts.ByteLimit(max(n, 0))
288 }
289
290 // WithDepthLimit sets a limit on the maximum depth of JSON nesting
291 // that may be consumed or produced for each top-level JSON value.
292 // If a [Decoder] or [Encoder] method call would need to consume or produce
293 // a depth greater than n to make progress on the top-level JSON value,
294 // then the call will report an error.
295 //
296 // A non-positive limit is equivalent to no limit at all.
297 // If unspecified, the default limit is 10000.
298 // This affects either encoding or decoding.
299 func WithDepthLimit(n int) Options {
300 return jsonopts.DepthLimit(max(n, 0))
301 }
302 */