// Copyright 2025 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Code generated by alias_gen.go; DO NOT EDIT. //go:build goexperiment.jsonv2 && go1.25 // Package jsontext implements syntactic processing of JSON // as specified in RFC 4627, RFC 7159, RFC 7493, RFC 8259, and RFC 8785. // JSON is a simple data interchange format that can represent // primitive data types such as booleans, strings, and numbers, // in addition to structured data types such as objects and arrays. // // The [Encoder] and [Decoder] types are used to encode or decode // a stream of JSON tokens or values. // // # Tokens and Values // // A JSON token refers to the basic structural elements of JSON: // // - a JSON literal (i.e., null, true, or false) // - a JSON string (e.g., "hello, world!") // - a JSON number (e.g., 123.456) // - a begin or end delimiter for a JSON object (i.e., '{' or '}') // - a begin or end delimiter for a JSON array (i.e., '[' or ']') // // A JSON token is represented by the [Token] type in Go. Technically, // there are two additional structural characters (i.e., ':' and ','), // but there is no [Token] representation for them since their presence // can be inferred by the structure of the JSON grammar itself. // For example, there must always be an implicit colon between // the name and value of a JSON object member. // // A JSON value refers to a complete unit of JSON data: // // - a JSON literal, string, or number // - a JSON object (e.g., `{"name":"value"}`) // - a JSON array (e.g., `[1,2,3,]`) // // A JSON value is represented by the [Value] type in Go and is a []byte // containing the raw textual representation of the value. There is some overlap // between tokens and values as both contain literals, strings, and numbers. // However, only a value can represent the entirety of a JSON object or array. // // The [Encoder] and [Decoder] types contain methods to read or write the next // [Token] or [Value] in a sequence. They maintain a state machine to validate // whether the sequence of JSON tokens and/or values produces a valid JSON. // [Options] may be passed to the [NewEncoder] or [NewDecoder] constructors // to configure the syntactic behavior of encoding and decoding. // // # Terminology // // The terms "encode" and "decode" are used for syntactic functionality // that is concerned with processing JSON based on its grammar, and // the terms "marshal" and "unmarshal" are used for semantic functionality // that determines the meaning of JSON values as Go values and vice-versa. // This package (i.e., [jsontext]) deals with JSON at a syntactic layer, // while [encoding/json/v2] deals with JSON at a semantic layer. // The goal is to provide a clear distinction between functionality that // is purely concerned with encoding versus that of marshaling. // For example, one can directly encode a stream of JSON tokens without // needing to marshal a concrete Go value representing them. // Similarly, one can decode a stream of JSON tokens without // needing to unmarshal them into a concrete Go value. // // This package uses JSON terminology when discussing JSON, which may differ // from related concepts in Go or elsewhere in computing literature. // // - a JSON "object" refers to an unordered collection of name/value members. // - a JSON "array" refers to an ordered sequence of elements. // - a JSON "value" refers to either a literal (i.e., null, false, or true), // string, number, object, or array. // // See RFC 8259 for more information. // // # Specifications // // Relevant specifications include RFC 4627, RFC 7159, RFC 7493, RFC 8259, // and RFC 8785. Each RFC is generally a stricter subset of another RFC. // In increasing order of strictness: // // - RFC 4627 and RFC 7159 do not require (but recommend) the use of UTF-8 // and also do not require (but recommend) that object names be unique. // - RFC 8259 requires the use of UTF-8, // but does not require (but recommends) that object names be unique. // - RFC 7493 requires the use of UTF-8 // and also requires that object names be unique. // - RFC 8785 defines a canonical representation. It requires the use of UTF-8 // and also requires that object names be unique and in a specific ordering. // It specifies exactly how strings and numbers must be formatted. // // The primary difference between RFC 4627 and RFC 7159 is that the former // restricted top-level values to only JSON objects and arrays, while // RFC 7159 and subsequent RFCs permit top-level values to additionally be // JSON nulls, booleans, strings, or numbers. // // By default, this package operates on RFC 7493, but can be configured // to operate according to the other RFC specifications. // RFC 7493 is a stricter subset of RFC 8259 and fully compliant with it. // In particular, it makes specific choices about behavior that RFC 8259 // leaves as undefined in order to ensure greater interoperability. // // # Security Considerations // // See the "Security Considerations" section in [encoding/json/v2]. package jsontext import ( "encoding/json/jsontext" "io" ) // Decoder is a streaming decoder for raw JSON tokens and values. // It is used to read a stream of top-level JSON values, // each separated by optional whitespace characters. // // [Decoder.ReadToken] and [Decoder.ReadValue] calls may be interleaved. // For example, the following JSON value: // // {"name":"value","array":[null,false,true,3.14159],"object":{"k":"v"}} // // can be parsed with the following calls (ignoring errors for brevity): // // d.ReadToken() // { // d.ReadToken() // "name" // d.ReadToken() // "value" // d.ReadValue() // "array" // d.ReadToken() // [ // d.ReadToken() // null // d.ReadToken() // false // d.ReadValue() // true // d.ReadToken() // 3.14159 // d.ReadToken() // ] // d.ReadValue() // "object" // d.ReadValue() // {"k":"v"} // d.ReadToken() // } // // The above is one of many possible sequence of calls and // may not represent the most sensible method to call for any given token/value. // For example, it is probably more common to call [Decoder.ReadToken] to obtain a // string token for object names. type Decoder = jsontext.Decoder // NewDecoder constructs a new streaming decoder reading from r. // // If r is a [bytes.Buffer], then the decoder parses directly from the buffer // without first copying the contents to an intermediate buffer. // Additional writes to the buffer must not occur while the decoder is in use. func NewDecoder(r io.Reader, opts ...Options) *Decoder { return jsontext.NewDecoder(r, opts...) } // Encoder is a streaming encoder from raw JSON tokens and values. // It is used to write a stream of top-level JSON values, // each terminated with a newline character. // // [Encoder.WriteToken] and [Encoder.WriteValue] calls may be interleaved. // For example, the following JSON value: // // {"name":"value","array":[null,false,true,3.14159],"object":{"k":"v"}} // // can be composed with the following calls (ignoring errors for brevity): // // e.WriteToken(BeginObject) // { // e.WriteToken(String("name")) // "name" // e.WriteToken(String("value")) // "value" // e.WriteValue(Value(`"array"`)) // "array" // e.WriteToken(BeginArray) // [ // e.WriteToken(Null) // null // e.WriteToken(False) // false // e.WriteValue(Value("true")) // true // e.WriteToken(Float(3.14159)) // 3.14159 // e.WriteToken(EndArray) // ] // e.WriteValue(Value(`"object"`)) // "object" // e.WriteValue(Value(`{"k":"v"}`)) // {"k":"v"} // e.WriteToken(EndObject) // } // // The above is one of many possible sequence of calls and // may not represent the most sensible method to call for any given token/value. // For example, it is probably more common to call [Encoder.WriteToken] with a string // for object names. type Encoder = jsontext.Encoder // NewEncoder constructs a new streaming encoder writing to w // configured with the provided options. // It flushes the internal buffer when the buffer is sufficiently full or // when a top-level value has been written. // // If w is a [bytes.Buffer], then the encoder appends directly into the buffer // without copying the contents from an intermediate buffer. func NewEncoder(w io.Writer, opts ...Options) *Encoder { return jsontext.NewEncoder(w, opts...) } // SyntacticError is a description of a syntactic error that occurred when // encoding or decoding JSON according to the grammar. // // The contents of this error as produced by this package may change over time. type SyntacticError = jsontext.SyntacticError // Options configures [NewEncoder], [Encoder.Reset], [NewDecoder], // and [Decoder.Reset] with specific features. // Each function takes in a variadic list of options, where properties // set in latter options override the value of previously set properties. // // There is a single Options type, which is used with both encoding and decoding. // Some options affect both operations, while others only affect one operation: // // - [AllowDuplicateNames] affects encoding and decoding // - [AllowInvalidUTF8] affects encoding and decoding // - [EscapeForHTML] affects encoding only // - [EscapeForJS] affects encoding only // - [PreserveRawStrings] affects encoding only // - [CanonicalizeRawInts] affects encoding only // - [CanonicalizeRawFloats] affects encoding only // - [ReorderRawObjects] affects encoding only // - [SpaceAfterColon] affects encoding only // - [SpaceAfterComma] affects encoding only // - [Multiline] affects encoding only // - [WithIndent] affects encoding only // - [WithIndentPrefix] affects encoding only // // Options that do not affect a particular operation are ignored. // // The Options type is identical to [encoding/json.Options] and // [encoding/json/v2.Options]. Options from the other packages may // be passed to functionality in this package, but are ignored. // Options from this package may be used with the other packages. type Options = jsontext.Options // AllowDuplicateNames specifies that JSON objects may contain // duplicate member names. Disabling the duplicate name check may provide // performance benefits, but breaks compliance with RFC 7493, section 2.3. // The input or output will still be compliant with RFC 8259, // which leaves the handling of duplicate names as unspecified behavior. // // This affects either encoding or decoding. func AllowDuplicateNames(v bool) Options { return jsontext.AllowDuplicateNames(v) } // AllowInvalidUTF8 specifies that JSON strings may contain invalid UTF-8, // which will be mangled as the Unicode replacement character, U+FFFD. // This causes the encoder or decoder to break compliance with // RFC 7493, section 2.1, and RFC 8259, section 8.1. // // This affects either encoding or decoding. func AllowInvalidUTF8(v bool) Options { return jsontext.AllowInvalidUTF8(v) } // EscapeForHTML specifies that '<', '>', and '&' characters within JSON strings // should be escaped as a hexadecimal Unicode codepoint (e.g., \u003c) so that // the output is safe to embed within HTML. // // This only affects encoding and is ignored when decoding. func EscapeForHTML(v bool) Options { return jsontext.EscapeForHTML(v) } // EscapeForJS specifies that U+2028 and U+2029 characters within JSON strings // should be escaped as a hexadecimal Unicode codepoint (e.g., \u2028) so that // the output is valid to embed within JavaScript. See RFC 8259, section 12. // // This only affects encoding and is ignored when decoding. func EscapeForJS(v bool) Options { return jsontext.EscapeForJS(v) } // PreserveRawStrings specifies that when encoding a raw JSON string in a // [Token] or [Value], pre-escaped sequences // in a JSON string are preserved to the output. // However, raw strings still respect [EscapeForHTML] and [EscapeForJS] // such that the relevant characters are escaped. // If [AllowInvalidUTF8] is enabled, bytes of invalid UTF-8 // are preserved to the output. // // This only affects encoding and is ignored when decoding. func PreserveRawStrings(v bool) Options { return jsontext.PreserveRawStrings(v) } // CanonicalizeRawInts specifies that when encoding a raw JSON // integer number (i.e., a number without a fraction and exponent) in a // [Token] or [Value], the number is canonicalized // according to RFC 8785, section 3.2.2.3. As a special case, // the number -0 is canonicalized as 0. // // JSON numbers are treated as IEEE 754 double precision numbers. // Any numbers with precision beyond what is representable by that form // will lose their precision when canonicalized. For example, // integer values beyond ±2⁵³ will lose their precision. // For example, 1234567890123456789 is formatted as 1234567890123456800. // // This only affects encoding and is ignored when decoding. func CanonicalizeRawInts(v bool) Options { return jsontext.CanonicalizeRawInts(v) } // CanonicalizeRawFloats specifies that when encoding a raw JSON // floating-point number (i.e., a number with a fraction or exponent) in a // [Token] or [Value], the number is canonicalized // according to RFC 8785, section 3.2.2.3. As a special case, // the number -0 is canonicalized as 0. // // JSON numbers are treated as IEEE 754 double precision numbers. // It is safe to canonicalize a serialized single precision number and // parse it back as a single precision number and expect the same value. // If a number exceeds ±1.7976931348623157e+308, which is the maximum // finite number, then it saturated at that value and formatted as such. // // This only affects encoding and is ignored when decoding. func CanonicalizeRawFloats(v bool) Options { return jsontext.CanonicalizeRawFloats(v) } // ReorderRawObjects specifies that when encoding a raw JSON object in a // [Value], the object members are reordered according to // RFC 8785, section 3.2.3. // // This only affects encoding and is ignored when decoding. func ReorderRawObjects(v bool) Options { return jsontext.ReorderRawObjects(v) } // SpaceAfterColon specifies that the JSON output should emit a space character // after each colon separator following a JSON object name. // If false, then no space character appears after the colon separator. // // This only affects encoding and is ignored when decoding. func SpaceAfterColon(v bool) Options { return jsontext.SpaceAfterColon(v) } // SpaceAfterComma specifies that the JSON output should emit a space character // after each comma separator following a JSON object value or array element. // If false, then no space character appears after the comma separator. // // This only affects encoding and is ignored when decoding. func SpaceAfterComma(v bool) Options { return jsontext.SpaceAfterComma(v) } // Multiline specifies that the JSON output should expand to multiple lines, // where every JSON object member or JSON array element appears on // a new, indented line according to the nesting depth. // // If [SpaceAfterColon] is not specified, then the default is true. // If [SpaceAfterComma] is not specified, then the default is false. // If [WithIndent] is not specified, then the default is "\t". // // If set to false, then the output is a single-line, // where the only whitespace emitted is determined by the current // values of [SpaceAfterColon] and [SpaceAfterComma]. // // This only affects encoding and is ignored when decoding. func Multiline(v bool) Options { return jsontext.Multiline(v) } // WithIndent specifies that the encoder should emit multiline output // where each element in a JSON object or array begins on a new, indented line // beginning with the indent prefix (see [WithIndentPrefix]) // followed by one or more copies of indent according to the nesting depth. // The indent must only be composed of space or tab characters. // // If the intent to emit indented output without a preference for // the particular indent string, then use [Multiline] instead. // // This only affects encoding and is ignored when decoding. // Use of this option implies [Multiline] being set to true. func WithIndent(indent string) Options { return jsontext.WithIndent(indent) } // WithIndentPrefix specifies that the encoder should emit multiline output // where each element in a JSON object or array begins on a new, indented line // beginning with the indent prefix followed by one or more copies of indent // (see [WithIndent]) according to the nesting depth. // The prefix must only be composed of space or tab characters. // // This only affects encoding and is ignored when decoding. // Use of this option implies [Multiline] being set to true. func WithIndentPrefix(prefix string) Options { return jsontext.WithIndentPrefix(prefix) } // AppendQuote appends a double-quoted JSON string literal representing src // to dst and returns the extended buffer. // It uses the minimal string representation per RFC 8785, section 3.2.2.2. // Invalid UTF-8 bytes are replaced with the Unicode replacement character // and an error is returned at the end indicating the presence of invalid UTF-8. // The dst must not overlap with the src. func AppendQuote[Bytes ~[]byte | ~string](dst []byte, src Bytes) ([]byte, error) { return jsontext.AppendQuote[Bytes](dst, src) } // AppendUnquote appends the decoded interpretation of src as a // double-quoted JSON string literal to dst and returns the extended buffer. // The input src must be a JSON string without any surrounding whitespace. // Invalid UTF-8 bytes are replaced with the Unicode replacement character // and an error is returned at the end indicating the presence of invalid UTF-8. // Any trailing bytes after the JSON string literal results in an error. // The dst must not overlap with the src. func AppendUnquote[Bytes ~[]byte | ~string](dst []byte, src Bytes) ([]byte, error) { return jsontext.AppendUnquote[Bytes](dst, src) } // ErrDuplicateName indicates that a JSON token could not be // encoded or decoded because it results in a duplicate JSON object name. // This error is directly wrapped within a [SyntacticError] when produced. // // The name of a duplicate JSON object member can be extracted as: // // err := ... // var serr jsontext.SyntacticError // if errors.As(err, &serr) && serr.Err == jsontext.ErrDuplicateName { // ptr := serr.JSONPointer // JSON pointer to duplicate name // name := ptr.LastToken() // duplicate name itself // ... // } // // This error is only returned if [AllowDuplicateNames] is false. var ErrDuplicateName = jsontext.ErrDuplicateName // ErrNonStringName indicates that a JSON token could not be // encoded or decoded because it is not a string, // as required for JSON object names according to RFC 8259, section 4. // This error is directly wrapped within a [SyntacticError] when produced. var ErrNonStringName = jsontext.ErrNonStringName // Pointer is a JSON Pointer (RFC 6901) that references a particular JSON value // relative to the root of the top-level JSON value. // // A Pointer is a slash-separated list of tokens, where each token is // either a JSON object name or an index to a JSON array element // encoded as a base-10 integer value. // It is impossible to distinguish between an array index and an object name // (that happens to be an base-10 encoded integer) without also knowing // the structure of the top-level JSON value that the pointer refers to. // // There is exactly one representation of a pointer to a particular value, // so comparability of Pointer values is equivalent to checking whether // they both point to the exact same value. type Pointer = jsontext.Pointer // Token represents a lexical JSON token, which may be one of the following: // - a JSON literal (i.e., null, true, or false) // - a JSON string (e.g., "hello, world!") // - a JSON number (e.g., 123.456) // - a begin or end delimiter for a JSON object (i.e., { or } ) // - a begin or end delimiter for a JSON array (i.e., [ or ] ) // // A Token cannot represent entire array or object values, while a [Value] can. // There is no Token to represent commas and colons since // these structural tokens can be inferred from the surrounding context. type Token = jsontext.Token var ( Null = jsontext.Null False = jsontext.False True = jsontext.True BeginObject = jsontext.BeginObject EndObject = jsontext.EndObject BeginArray = jsontext.BeginArray EndArray = jsontext.EndArray ) // Bool constructs a Token representing a JSON boolean. func Bool(b bool) Token { return jsontext.Bool(b) } // String constructs a Token representing a JSON string. // The provided string should contain valid UTF-8, otherwise invalid characters // may be mangled as the Unicode replacement character. func String(s string) Token { return jsontext.String(s) } // Float constructs a Token representing a JSON number. // The values NaN, +Inf, and -Inf will be represented // as a JSON string with the values "NaN", "Infinity", and "-Infinity". func Float(n float64) Token { return jsontext.Float(n) } // Int constructs a Token representing a JSON number from an int64. func Int(n int64) Token { return jsontext.Int(n) } // Uint constructs a Token representing a JSON number from a uint64. func Uint(n uint64) Token { return jsontext.Uint(n) } // Kind represents each possible JSON token kind with a single byte, // which is conveniently the first byte of that kind's grammar // with the restriction that numbers always be represented with '0': // // - 'n': null // - 'f': false // - 't': true // - '"': string // - '0': number // - '{': object begin // - '}': object end // - '[': array begin // - ']': array end // // An invalid kind is usually represented using 0, // but may be non-zero due to invalid JSON data. type Kind = jsontext.Kind // AppendFormat formats the JSON value in src and appends it to dst // according to the specified options. // See [Value.Format] for more details about the formatting behavior. // // The dst and src may overlap. // If an error is reported, then the entirety of src is appended to dst. func AppendFormat(dst, src []byte, opts ...Options) ([]byte, error) { return jsontext.AppendFormat(dst, src, opts...) } // Value represents a single raw JSON value, which may be one of the following: // - a JSON literal (i.e., null, true, or false) // - a JSON string (e.g., "hello, world!") // - a JSON number (e.g., 123.456) // - an entire JSON object (e.g., {"fizz":"buzz"} ) // - an entire JSON array (e.g., [1,2,3] ) // // Value can represent entire array or object values, while [Token] cannot. // Value may contain leading and/or trailing whitespace. type Value = jsontext.Value