Debian Codegraph @

jsontext/alias.go

raw symbols SCIP
1 // Copyright 2025 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 // Code generated by alias_gen.go; DO NOT EDIT.
6
7 //go:build goexperiment.jsonv2 && go1.25
8
9 // Package jsontext implements syntactic processing of JSON
10 // as specified in RFC 4627, RFC 7159, RFC 7493, RFC 8259, and RFC 8785.
11 // JSON is a simple data interchange format that can represent
12 // primitive data types such as booleans, strings, and numbers,
13 // in addition to structured data types such as objects and arrays.
14 //
15 // The [Encoder] and [Decoder] types are used to encode or decode
16 // a stream of JSON tokens or values.
17 //
18 // # Tokens and Values
19 //
20 // A JSON token refers to the basic structural elements of JSON:
21 //
22 // - a JSON literal (i.e., null, true, or false)
23 // - a JSON string (e.g., "hello, world!")
24 // - a JSON number (e.g., 123.456)
25 // - a begin or end delimiter for a JSON object (i.e., '{' or '}')
26 // - a begin or end delimiter for a JSON array (i.e., '[' or ']')
27 //
28 // A JSON token is represented by the [Token] type in Go. Technically,
29 // there are two additional structural characters (i.e., ':' and ','),
30 // but there is no [Token] representation for them since their presence
31 // can be inferred by the structure of the JSON grammar itself.
32 // For example, there must always be an implicit colon between
33 // the name and value of a JSON object member.
34 //
35 // A JSON value refers to a complete unit of JSON data:
36 //
37 // - a JSON literal, string, or number
38 // - a JSON object (e.g., `{"name":"value"}`)
39 // - a JSON array (e.g., `[1,2,3,]`)
40 //
41 // A JSON value is represented by the [Value] type in Go and is a []byte
42 // containing the raw textual representation of the value. There is some overlap
43 // between tokens and values as both contain literals, strings, and numbers.
44 // However, only a value can represent the entirety of a JSON object or array.
45 //
46 // The [Encoder] and [Decoder] types contain methods to read or write the next
47 // [Token] or [Value] in a sequence. They maintain a state machine to validate
48 // whether the sequence of JSON tokens and/or values produces a valid JSON.
49 // [Options] may be passed to the [NewEncoder] or [NewDecoder] constructors
50 // to configure the syntactic behavior of encoding and decoding.
51 //
52 // # Terminology
53 //
54 // The terms "encode" and "decode" are used for syntactic functionality
55 // that is concerned with processing JSON based on its grammar, and
56 // the terms "marshal" and "unmarshal" are used for semantic functionality
57 // that determines the meaning of JSON values as Go values and vice-versa.
58 // This package (i.e., [jsontext]) deals with JSON at a syntactic layer,
59 // while [encoding/json/v2] deals with JSON at a semantic layer.
60 // The goal is to provide a clear distinction between functionality that
61 // is purely concerned with encoding versus that of marshaling.
62 // For example, one can directly encode a stream of JSON tokens without
63 // needing to marshal a concrete Go value representing them.
64 // Similarly, one can decode a stream of JSON tokens without
65 // needing to unmarshal them into a concrete Go value.
66 //
67 // This package uses JSON terminology when discussing JSON, which may differ
68 // from related concepts in Go or elsewhere in computing literature.
69 //
70 // - a JSON "object" refers to an unordered collection of name/value members.
71 // - a JSON "array" refers to an ordered sequence of elements.
72 // - a JSON "value" refers to either a literal (i.e., null, false, or true),
73 // string, number, object, or array.
74 //
75 // See RFC 8259 for more information.
76 //
77 // # Specifications
78 //
79 // Relevant specifications include RFC 4627, RFC 7159, RFC 7493, RFC 8259,
80 // and RFC 8785. Each RFC is generally a stricter subset of another RFC.
81 // In increasing order of strictness:
82 //
83 // - RFC 4627 and RFC 7159 do not require (but recommend) the use of UTF-8
84 // and also do not require (but recommend) that object names be unique.
85 // - RFC 8259 requires the use of UTF-8,
86 // but does not require (but recommends) that object names be unique.
87 // - RFC 7493 requires the use of UTF-8
88 // and also requires that object names be unique.
89 // - RFC 8785 defines a canonical representation. It requires the use of UTF-8
90 // and also requires that object names be unique and in a specific ordering.
91 // It specifies exactly how strings and numbers must be formatted.
92 //
93 // The primary difference between RFC 4627 and RFC 7159 is that the former
94 // restricted top-level values to only JSON objects and arrays, while
95 // RFC 7159 and subsequent RFCs permit top-level values to additionally be
96 // JSON nulls, booleans, strings, or numbers.
97 //
98 // By default, this package operates on RFC 7493, but can be configured
99 // to operate according to the other RFC specifications.
100 // RFC 7493 is a stricter subset of RFC 8259 and fully compliant with it.
101 // In particular, it makes specific choices about behavior that RFC 8259
102 // leaves as undefined in order to ensure greater interoperability.
103 //
104 // # Security Considerations
105 //
106 // See the "Security Considerations" section in [encoding/json/v2].
107 package jsontext
108
109 import (
110 "encoding/json/jsontext"
111 "io"
112 )
113
114 // Decoder is a streaming decoder for raw JSON tokens and values.
115 // It is used to read a stream of top-level JSON values,
116 // each separated by optional whitespace characters.
117 //
118 // [Decoder.ReadToken] and [Decoder.ReadValue] calls may be interleaved.
119 // For example, the following JSON value:
120 //
121 // {"name":"value","array":[null,false,true,3.14159],"object":{"k":"v"}}
122 //
123 // can be parsed with the following calls (ignoring errors for brevity):
124 //
125 // d.ReadToken() // {
126 // d.ReadToken() // "name"
127 // d.ReadToken() // "value"
128 // d.ReadValue() // "array"
129 // d.ReadToken() // [
130 // d.ReadToken() // null
131 // d.ReadToken() // false
132 // d.ReadValue() // true
133 // d.ReadToken() // 3.14159
134 // d.ReadToken() // ]
135 // d.ReadValue() // "object"
136 // d.ReadValue() // {"k":"v"}
137 // d.ReadToken() // }
138 //
139 // The above is one of many possible sequence of calls and
140 // may not represent the most sensible method to call for any given token/value.
141 // For example, it is probably more common to call [Decoder.ReadToken] to obtain a
142 // string token for object names.
143 type Decoder = jsontext.Decoder
144
145 // NewDecoder constructs a new streaming decoder reading from r.
146 //
147 // If r is a [bytes.Buffer], then the decoder parses directly from the buffer
148 // without first copying the contents to an intermediate buffer.
149 // Additional writes to the buffer must not occur while the decoder is in use.
150 func NewDecoder(r io.Reader, opts ...Options) *Decoder {
151 return jsontext.NewDecoder(r, opts...)
152 }
153
154 // Encoder is a streaming encoder from raw JSON tokens and values.
155 // It is used to write a stream of top-level JSON values,
156 // each terminated with a newline character.
157 //
158 // [Encoder.WriteToken] and [Encoder.WriteValue] calls may be interleaved.
159 // For example, the following JSON value:
160 //
161 // {"name":"value","array":[null,false,true,3.14159],"object":{"k":"v"}}
162 //
163 // can be composed with the following calls (ignoring errors for brevity):
164 //
165 // e.WriteToken(BeginObject) // {
166 // e.WriteToken(String("name")) // "name"
167 // e.WriteToken(String("value")) // "value"
168 // e.WriteValue(Value(`"array"`)) // "array"
169 // e.WriteToken(BeginArray) // [
170 // e.WriteToken(Null) // null
171 // e.WriteToken(False) // false
172 // e.WriteValue(Value("true")) // true
173 // e.WriteToken(Float(3.14159)) // 3.14159
174 // e.WriteToken(EndArray) // ]
175 // e.WriteValue(Value(`"object"`)) // "object"
176 // e.WriteValue(Value(`{"k":"v"}`)) // {"k":"v"}
177 // e.WriteToken(EndObject) // }
178 //
179 // The above is one of many possible sequence of calls and
180 // may not represent the most sensible method to call for any given token/value.
181 // For example, it is probably more common to call [Encoder.WriteToken] with a string
182 // for object names.
183 type Encoder = jsontext.Encoder
184
185 // NewEncoder constructs a new streaming encoder writing to w
186 // configured with the provided options.
187 // It flushes the internal buffer when the buffer is sufficiently full or
188 // when a top-level value has been written.
189 //
190 // If w is a [bytes.Buffer], then the encoder appends directly into the buffer
191 // without copying the contents from an intermediate buffer.
192 func NewEncoder(w io.Writer, opts ...Options) *Encoder {
193 return jsontext.NewEncoder(w, opts...)
194 }
195
196 // SyntacticError is a description of a syntactic error that occurred when
197 // encoding or decoding JSON according to the grammar.
198 //
199 // The contents of this error as produced by this package may change over time.
200 type SyntacticError = jsontext.SyntacticError
201
202 // Options configures [NewEncoder], [Encoder.Reset], [NewDecoder],
203 // and [Decoder.Reset] with specific features.
204 // Each function takes in a variadic list of options, where properties
205 // set in latter options override the value of previously set properties.
206 //
207 // There is a single Options type, which is used with both encoding and decoding.
208 // Some options affect both operations, while others only affect one operation:
209 //
210 // - [AllowDuplicateNames] affects encoding and decoding
211 // - [AllowInvalidUTF8] affects encoding and decoding
212 // - [EscapeForHTML] affects encoding only
213 // - [EscapeForJS] affects encoding only
214 // - [PreserveRawStrings] affects encoding only
215 // - [CanonicalizeRawInts] affects encoding only
216 // - [CanonicalizeRawFloats] affects encoding only
217 // - [ReorderRawObjects] affects encoding only
218 // - [SpaceAfterColon] affects encoding only
219 // - [SpaceAfterComma] affects encoding only
220 // - [Multiline] affects encoding only
221 // - [WithIndent] affects encoding only
222 // - [WithIndentPrefix] affects encoding only
223 //
224 // Options that do not affect a particular operation are ignored.
225 //
226 // The Options type is identical to [encoding/json.Options] and
227 // [encoding/json/v2.Options]. Options from the other packages may
228 // be passed to functionality in this package, but are ignored.
229 // Options from this package may be used with the other packages.
230 type Options = jsontext.Options
231
232 // AllowDuplicateNames specifies that JSON objects may contain
233 // duplicate member names. Disabling the duplicate name check may provide
234 // performance benefits, but breaks compliance with RFC 7493, section 2.3.
235 // The input or output will still be compliant with RFC 8259,
236 // which leaves the handling of duplicate names as unspecified behavior.
237 //
238 // This affects either encoding or decoding.
239 func AllowDuplicateNames(v bool) Options {
240 return jsontext.AllowDuplicateNames(v)
241 }
242
243 // AllowInvalidUTF8 specifies that JSON strings may contain invalid UTF-8,
244 // which will be mangled as the Unicode replacement character, U+FFFD.
245 // This causes the encoder or decoder to break compliance with
246 // RFC 7493, section 2.1, and RFC 8259, section 8.1.
247 //
248 // This affects either encoding or decoding.
249 func AllowInvalidUTF8(v bool) Options {
250 return jsontext.AllowInvalidUTF8(v)
251 }
252
253 // EscapeForHTML specifies that '<', '>', and '&' characters within JSON strings
254 // should be escaped as a hexadecimal Unicode codepoint (e.g., \u003c) so that
255 // the output is safe to embed within HTML.
256 //
257 // This only affects encoding and is ignored when decoding.
258 func EscapeForHTML(v bool) Options {
259 return jsontext.EscapeForHTML(v)
260 }
261
262 // EscapeForJS specifies that U+2028 and U+2029 characters within JSON strings
263 // should be escaped as a hexadecimal Unicode codepoint (e.g., \u2028) so that
264 // the output is valid to embed within JavaScript. See RFC 8259, section 12.
265 //
266 // This only affects encoding and is ignored when decoding.
267 func EscapeForJS(v bool) Options {
268 return jsontext.EscapeForJS(v)
269 }
270
271 // PreserveRawStrings specifies that when encoding a raw JSON string in a
272 // [Token] or [Value], pre-escaped sequences
273 // in a JSON string are preserved to the output.
274 // However, raw strings still respect [EscapeForHTML] and [EscapeForJS]
275 // such that the relevant characters are escaped.
276 // If [AllowInvalidUTF8] is enabled, bytes of invalid UTF-8
277 // are preserved to the output.
278 //
279 // This only affects encoding and is ignored when decoding.
280 func PreserveRawStrings(v bool) Options {
281 return jsontext.PreserveRawStrings(v)
282 }
283
284 // CanonicalizeRawInts specifies that when encoding a raw JSON
285 // integer number (i.e., a number without a fraction and exponent) in a
286 // [Token] or [Value], the number is canonicalized
287 // according to RFC 8785, section 3.2.2.3. As a special case,
288 // the number -0 is canonicalized as 0.
289 //
290 // JSON numbers are treated as IEEE 754 double precision numbers.
291 // Any numbers with precision beyond what is representable by that form
292 // will lose their precision when canonicalized. For example,
293 // integer values beyond ±2⁵³ will lose their precision.
294 // For example, 1234567890123456789 is formatted as 1234567890123456800.
295 //
296 // This only affects encoding and is ignored when decoding.
297 func CanonicalizeRawInts(v bool) Options {
298 return jsontext.CanonicalizeRawInts(v)
299 }
300
301 // CanonicalizeRawFloats specifies that when encoding a raw JSON
302 // floating-point number (i.e., a number with a fraction or exponent) in a
303 // [Token] or [Value], the number is canonicalized
304 // according to RFC 8785, section 3.2.2.3. As a special case,
305 // the number -0 is canonicalized as 0.
306 //
307 // JSON numbers are treated as IEEE 754 double precision numbers.
308 // It is safe to canonicalize a serialized single precision number and
309 // parse it back as a single precision number and expect the same value.
310 // If a number exceeds ±1.7976931348623157e+308, which is the maximum
311 // finite number, then it saturated at that value and formatted as such.
312 //
313 // This only affects encoding and is ignored when decoding.
314 func CanonicalizeRawFloats(v bool) Options {
315 return jsontext.CanonicalizeRawFloats(v)
316 }
317
318 // ReorderRawObjects specifies that when encoding a raw JSON object in a
319 // [Value], the object members are reordered according to
320 // RFC 8785, section 3.2.3.
321 //
322 // This only affects encoding and is ignored when decoding.
323 func ReorderRawObjects(v bool) Options {
324 return jsontext.ReorderRawObjects(v)
325 }
326
327 // SpaceAfterColon specifies that the JSON output should emit a space character
328 // after each colon separator following a JSON object name.
329 // If false, then no space character appears after the colon separator.
330 //
331 // This only affects encoding and is ignored when decoding.
332 func SpaceAfterColon(v bool) Options {
333 return jsontext.SpaceAfterColon(v)
334 }
335
336 // SpaceAfterComma specifies that the JSON output should emit a space character
337 // after each comma separator following a JSON object value or array element.
338 // If false, then no space character appears after the comma separator.
339 //
340 // This only affects encoding and is ignored when decoding.
341 func SpaceAfterComma(v bool) Options {
342 return jsontext.SpaceAfterComma(v)
343 }
344
345 // Multiline specifies that the JSON output should expand to multiple lines,
346 // where every JSON object member or JSON array element appears on
347 // a new, indented line according to the nesting depth.
348 //
349 // If [SpaceAfterColon] is not specified, then the default is true.
350 // If [SpaceAfterComma] is not specified, then the default is false.
351 // If [WithIndent] is not specified, then the default is "\t".
352 //
353 // If set to false, then the output is a single-line,
354 // where the only whitespace emitted is determined by the current
355 // values of [SpaceAfterColon] and [SpaceAfterComma].
356 //
357 // This only affects encoding and is ignored when decoding.
358 func Multiline(v bool) Options {
359 return jsontext.Multiline(v)
360 }
361
362 // WithIndent specifies that the encoder should emit multiline output
363 // where each element in a JSON object or array begins on a new, indented line
364 // beginning with the indent prefix (see [WithIndentPrefix])
365 // followed by one or more copies of indent according to the nesting depth.
366 // The indent must only be composed of space or tab characters.
367 //
368 // If the intent to emit indented output without a preference for
369 // the particular indent string, then use [Multiline] instead.
370 //
371 // This only affects encoding and is ignored when decoding.
372 // Use of this option implies [Multiline] being set to true.
373 func WithIndent(indent string) Options {
374 return jsontext.WithIndent(indent)
375 }
376
377 // WithIndentPrefix specifies that the encoder should emit multiline output
378 // where each element in a JSON object or array begins on a new, indented line
379 // beginning with the indent prefix followed by one or more copies of indent
380 // (see [WithIndent]) according to the nesting depth.
381 // The prefix must only be composed of space or tab characters.
382 //
383 // This only affects encoding and is ignored when decoding.
384 // Use of this option implies [Multiline] being set to true.
385 func WithIndentPrefix(prefix string) Options {
386 return jsontext.WithIndentPrefix(prefix)
387 }
388
389 // AppendQuote appends a double-quoted JSON string literal representing src
390 // to dst and returns the extended buffer.
391 // It uses the minimal string representation per RFC 8785, section 3.2.2.2.
392 // Invalid UTF-8 bytes are replaced with the Unicode replacement character
393 // and an error is returned at the end indicating the presence of invalid UTF-8.
394 // The dst must not overlap with the src.
395 func AppendQuote[Bytes ~[]byte | ~string](dst []byte, src Bytes) ([]byte, error) {
396 return jsontext.AppendQuote[Bytes](dst, src)
397 }
398
399 // AppendUnquote appends the decoded interpretation of src as a
400 // double-quoted JSON string literal to dst and returns the extended buffer.
401 // The input src must be a JSON string without any surrounding whitespace.
402 // Invalid UTF-8 bytes are replaced with the Unicode replacement character
403 // and an error is returned at the end indicating the presence of invalid UTF-8.
404 // Any trailing bytes after the JSON string literal results in an error.
405 // The dst must not overlap with the src.
406 func AppendUnquote[Bytes ~[]byte | ~string](dst []byte, src Bytes) ([]byte, error) {
407 return jsontext.AppendUnquote[Bytes](dst, src)
408 }
409
410 // ErrDuplicateName indicates that a JSON token could not be
411 // encoded or decoded because it results in a duplicate JSON object name.
412 // This error is directly wrapped within a [SyntacticError] when produced.
413 //
414 // The name of a duplicate JSON object member can be extracted as:
415 //
416 // err := ...
417 // serr, ok := errors.AsType[*jsontext.SyntacticError](err)
418 // if ok && serr.Err == jsontext.ErrDuplicateName {
419 // ptr := serr.JSONPointer // JSON pointer to duplicate name
420 // name := ptr.LastToken() // duplicate name itself
421 // ...
422 // }
423 //
424 // This error is only returned if [AllowDuplicateNames] is false.
425 var ErrDuplicateName = jsontext.ErrDuplicateName
426
427 // ErrNonStringName indicates that a JSON token could not be
428 // encoded or decoded because it is not a string,
429 // as required for JSON object names according to RFC 8259, section 4.
430 // This error is directly wrapped within a [SyntacticError] when produced.
431 var ErrNonStringName = jsontext.ErrNonStringName
432
433 // Pointer is a JSON Pointer (RFC 6901) that references a particular JSON value
434 // relative to the root of the top-level JSON value.
435 //
436 // A Pointer is a slash-separated list of tokens, where each token is
437 // either a JSON object name or an index to a JSON array element
438 // encoded as a base-10 integer value.
439 // It is impossible to distinguish between an array index and an object name
440 // (that happens to be an base-10 encoded integer) without also knowing
441 // the structure of the top-level JSON value that the pointer refers to.
442 //
443 // There is exactly one representation of a pointer to a particular value,
444 // so comparability of Pointer values is equivalent to checking whether
445 // they both point to the exact same value.
446 type Pointer = jsontext.Pointer
447
448 // Token represents a lexical JSON token, which may be one of the following:
449 // - a JSON literal (i.e., null, true, or false)
450 // - a JSON string (e.g., "hello, world!")
451 // - a JSON number (e.g., 123.456)
452 // - a begin or end delimiter for a JSON object (i.e., { or } )
453 // - a begin or end delimiter for a JSON array (i.e., [ or ] )
454 //
455 // A Token cannot represent entire array or object values, while a [Value] can.
456 // There is no Token to represent commas and colons since
457 // these structural tokens can be inferred from the surrounding context.
458 type Token = jsontext.Token
459
460 var (
461 Null = jsontext.Null
462 False = jsontext.False
463 True = jsontext.True
464 BeginObject = jsontext.BeginObject
465 EndObject = jsontext.EndObject
466 BeginArray = jsontext.BeginArray
467 EndArray = jsontext.EndArray
468 )
469
470 // Bool constructs a Token representing a JSON boolean.
471 func Bool(b bool) Token {
472 return jsontext.Bool(b)
473 }
474
475 // String constructs a Token representing a JSON string.
476 // The provided string should contain valid UTF-8, otherwise invalid characters
477 // may be mangled as the Unicode replacement character.
478 func String(s string) Token {
479 return jsontext.String(s)
480 }
481
482 // Float constructs a Token representing a JSON number.
483 // The values NaN, +Inf, and -Inf will be represented
484 // as a JSON string with the values "NaN", "Infinity", and "-Infinity".
485 func Float(n float64) Token {
486 return jsontext.Float(n)
487 }
488
489 // Int constructs a Token representing a JSON number from an int64.
490 func Int(n int64) Token {
491 return jsontext.Int(n)
492 }
493
494 // Uint constructs a Token representing a JSON number from a uint64.
495 func Uint(n uint64) Token {
496 return jsontext.Uint(n)
497 }
498
499 // A Kind represents the kind of a JSON token.
500 //
501 // Kind represents each possible JSON token kind with a single byte,
502 // which is conveniently the first byte of that kind's grammar
503 // with the restriction that numbers always be represented with '0'.
504 type Kind = jsontext.Kind
505
506 const (
507 KindInvalid = jsontext.KindInvalid
508 KindNull = jsontext.KindNull
509 KindFalse = jsontext.KindFalse
510 KindTrue = jsontext.KindTrue
511 KindString = jsontext.KindString
512 KindNumber = jsontext.KindNumber
513 KindBeginObject = jsontext.KindBeginObject
514 KindEndObject = jsontext.KindEndObject
515 KindBeginArray = jsontext.KindBeginArray
516 KindEndArray = jsontext.KindEndArray
517 )
518
519 // AppendFormat formats the JSON value in src and appends it to dst
520 // according to the specified options.
521 // See [Value.Format] for more details about the formatting behavior.
522 //
523 // The dst and src may overlap.
524 // If an error is reported, then the entirety of src is appended to dst.
525 func AppendFormat(dst, src []byte, opts ...Options) ([]byte, error) {
526 return jsontext.AppendFormat(dst, src, opts...)
527 }
528
529 // Value represents a single raw JSON value, which may be one of the following:
530 // - a JSON literal (i.e., null, true, or false)
531 // - a JSON string (e.g., "hello, world!")
532 // - a JSON number (e.g., 123.456)
533 // - an entire JSON object (e.g., {"fizz":"buzz"} )
534 // - an entire JSON array (e.g., [1,2,3] )
535 //
536 // Value can represent entire array or object values, while [Token] cannot.
537 // Value may contain leading and/or trailing whitespace.
538 type Value = jsontext.Value