// 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 json implements encoding and decoding of JSON as defined in // RFC 7159. The mapping between JSON and Go values is described // in the documentation for the Marshal and Unmarshal functions. // // See [JSON and Go] for an introduction to this package. // // # Security Considerations // // See the "Security Considerations" section in [encoding/json/v2]. // // For historical reasons, the default behavior of v1 [encoding/json] // unfortunately operates with less secure defaults. // New usages of JSON in Go are encouraged to use [encoding/json/v2] instead. // // [JSON and Go]: https://go.dev/blog/json // Migrating to v2 // // This package (i.e., [encoding/json]) is now formally known as the v1 package // since a v2 package now exists at [encoding/json/v2]. // All the behavior of the v1 package is implemented in terms of // the v2 package with the appropriate set of options specified that // preserve the historical behavior of v1. // // The [jsonv2.Marshal] function is the newer equivalent of v1 [Marshal]. // The [jsonv2.Unmarshal] function is the newer equivalent of v1 [Unmarshal]. // The v2 functions have the same calling signature as the v1 equivalent // except that they take in variadic [Options] arguments that can be specified // to alter the behavior of marshal or unmarshal. Both v1 and v2 generally // behave in similar ways, but there are some notable differences. // // The following is a list of differences between v1 and v2: // // - In v1, JSON object members are unmarshaled into a Go struct using a // case-insensitive name match with the JSON name of the fields. // In contrast, v2 matches fields using an exact, case-sensitive match. // The [jsonv2.MatchCaseInsensitiveNames] and [MatchCaseSensitiveDelimiter] // options control this behavior difference. To explicitly specify a Go struct // field to use a particular name matching scheme, either the `case:ignore` // or the `case:strict` field option can be specified. // Field-specified options take precedence over caller-specified options. // // - In v1, when marshaling a Go struct, a field marked as `omitempty` // is omitted if the field value is an "empty" Go value, which is defined as // false, 0, a nil pointer, a nil interface value, and // any empty array, slice, map, or string. In contrast, v2 redefines // `omitempty` to omit a field if it encodes as an "empty" JSON value, // which is defined as a JSON null, or an empty JSON string, object, or array. // The [OmitEmptyWithLegacySemantics] option controls this behavior difference. // Note that `omitempty` behaves identically in both v1 and v2 for a // Go array, slice, map, or string (assuming no user-defined MarshalJSON method // overrides the default representation). Existing usages of `omitempty` on a // Go bool, number, pointer, or interface value should migrate to specifying // `omitzero` instead (which is identically supported in both v1 and v2). // // - In v1, a Go struct field marked as `string` can be used to quote a // Go string, bool, number, or pointer to such as a JSON string. // In contrast, v2 restricts the `string` option to only quote a value // that would normally be represented as a JSON number, // but also expands support for it to operate with any Go type // that would normally be represented as a JSON number. // The [StringifyWithLegacySemantics] option controls this behavior difference. // // - In v1, a nil Go slice or Go map is marshaled as a JSON null. // In contrast, v2 marshals a nil Go slice or Go map as // an empty JSON array or JSON object, respectively. // The [jsonv2.FormatNilSliceAsNull] and [jsonv2.FormatNilMapAsNull] options // control this behavior difference. // // - In v1, a Go array may be unmarshaled from a JSON array of any length. // In contrast, in v2 a Go array must be unmarshaled from a JSON array // of the same length, otherwise it results in an error. // The [UnmarshalArrayFromAnyLength] option controls this behavior difference. // // - In v1, a Go byte array is represented as a JSON array of JSON numbers. // In contrast, in v2 a Go byte array is represented as a Base64-encoded JSON string. // The [FormatByteArrayAsArray] option controls this behavior difference. // // - In v1, MarshalJSON methods declared on a pointer receiver are only called // if the Go value is addressable. In contrast, in v2 a MarshalJSON method // is always callable regardless of addressability. // The [CallMethodsWithLegacySemantics] option controls this behavior difference. // // - In v1, MarshalJSON and UnmarshalJSON methods are never called for Go map keys. // In contrast, in v2 a MarshalJSON or UnmarshalJSON method is eligible for // being called for Go map keys. // The [CallMethodsWithLegacySemantics] option controls this behavior difference. // // - In v1, a Go map is marshaled in a deterministic order. // In contrast, in v2 a Go map is marshaled in a non-deterministic order. // The [jsonv2.Deterministic] option controls this behavior difference. // // - In v1, JSON strings are encoded with HTML-specific or JavaScript-specific // characters being escaped. In contrast, in v2 JSON strings use the minimal // encoding and only escape if required by the JSON grammar. // The [jsontext.EscapeForHTML] and [jsontext.EscapeForJS] options // control this behavior difference. // // - In v1, bytes of invalid UTF-8 within a string are silently replaced with // the Unicode replacement character. In contrast, in v2 the presence of // invalid UTF-8 results in an error. The [jsontext.AllowInvalidUTF8] option // controls this behavior difference. // // - In v1, a JSON object with duplicate names is permitted. // In contrast, in v2 a JSON object with duplicate names results in an error. // The [jsontext.AllowDuplicateNames] option controls this behavior difference. // // - In v1, when unmarshaling a JSON null into a non-empty Go value it will // inconsistently either zero out the value or do nothing. // In contrast, in v2 unmarshaling a JSON null will consistently and always // zero out the underlying Go value. The [MergeWithLegacySemantics] option // controls this behavior difference. // // - In v1, when unmarshaling a JSON value into a non-zero Go value, // it merges into the original Go value for array elements, slice elements, // struct fields (but not map values), // pointer values, and interface values (only if a non-nil pointer). // In contrast, in v2 unmarshal merges into the Go value // for struct fields, map values, pointer values, and interface values. // In general, the v2 semantic merges when unmarshaling a JSON object, // otherwise it replaces the value. The [MergeWithLegacySemantics] option // controls this behavior difference. // // - In v1, a [time.Duration] is represented as a JSON number containing // the decimal number of nanoseconds. In contrast, in v2 a [time.Duration] // has no default representation and results in a runtime error. // The [FormatDurationAsNano] option controls this behavior difference. // // - In v1, errors are never reported at runtime for Go struct types // that have some form of structural error (e.g., a malformed tag option). // In contrast, v2 reports a runtime error for Go types that are invalid // as they relate to JSON serialization. For example, a Go struct // with only unexported fields cannot be serialized. // The [ReportErrorsWithLegacySemantics] option controls this behavior difference. // // As mentioned, the entirety of v1 is implemented in terms of v2, // where options are implicitly specified to opt into legacy behavior. // For example, [Marshal] directly calls [jsonv2.Marshal] with [DefaultOptionsV1]. // Similarly, [Unmarshal] directly calls [jsonv2.Unmarshal] with [DefaultOptionsV1]. // The [DefaultOptionsV1] option represents the set of all options that specify // default v1 behavior. // // For many of the behavior differences, there are Go struct field options // that the author of a Go type can specify to control the behavior such that // the type is represented identically in JSON under either v1 or v2 semantics. // // The availability of [DefaultOptionsV1] and [jsonv2.DefaultOptionsV2], // where later options take precedence over former options allows for // a gradual migration from v1 to v2. For example: // // - jsonv1.Marshal(v) // uses default v1 semantics. // // - jsonv2.Marshal(v, jsonv1.DefaultOptionsV1()) // is semantically equivalent to jsonv1.Marshal // and thus uses default v1 semantics. // // - jsonv2.Marshal(v, jsonv1.DefaultOptionsV1(), jsontext.AllowDuplicateNames(false)) // uses mostly v1 semantics, but opts into one particular v2-specific behavior. // // - jsonv2.Marshal(v, jsonv1.CallMethodsWithLegacySemantics(true)) // uses mostly v2 semantics, but opts into one particular v1-specific behavior. // // - jsonv2.Marshal(v, ..., jsonv2.DefaultOptionsV2()) // is semantically equivalent to jsonv2.Marshal since // jsonv2.DefaultOptionsV2 overrides any options specified earlier // and thus uses default v2 semantics. // // - jsonv2.Marshal(v) // uses default v2 semantics. // // All new usages of "json" in Go should use the v2 package, // but the v1 package will forever remain supported. // // See the [encoding/json/v2 Migration Guide] for additional detail on migration approaches. // // [encoding/json/v2 Migration Guide]: https://go.dev/doc/jsonv2-migration package json import ( "bytes" "encoding/json" "io" ) // Unmarshal parses the JSON-encoded data and stores the result // in the value pointed to by v. If v is nil or not a pointer, // Unmarshal returns an [InvalidUnmarshalError]. // // Unmarshal uses the inverse of the encodings that // [Marshal] uses, allocating maps, slices, and pointers as necessary, // with the following additional rules: // // To unmarshal JSON into a pointer, Unmarshal first handles the case of // the JSON being the JSON literal null. In that case, Unmarshal sets // the pointer to nil. Otherwise, Unmarshal unmarshals the JSON into // the value pointed at by the pointer. If the pointer is nil, Unmarshal // allocates a new value for it to point to. // // The JSON input is decoded according the following rules: // // - If the value type implements [jsonv2.UnmarshalerFrom], // then the UnmarshalJSONFrom method is called to decode the JSON value. // If the method returns [errors.ErrUnsupported], // then the input is decoded according to subsequent rules. // // - If the value type implements [Unmarshaler], // then the UnmarshalJSON method is called to decode the JSON value, // including when the input is a JSON null. // // - If the value implements [encoding.TextUnmarshaler] and // the input is a JSON string, then the UnmarshalText method // is called with the unquoted form of the string. // // Otherwise, Unmarshal uses the following type-dependent default decodings: // // To unmarshal JSON into a struct, Unmarshal matches incoming object // keys to the keys used by [Marshal] (either the struct field name or its tag), // preferring an exact match but also accepting a case-insensitive match. By // default, object keys which don't have a corresponding struct field are // ignored (see [Decoder.DisallowUnknownFields] for an alternative). // // To unmarshal JSON into an interface value, // Unmarshal stores one of these in the interface value: // // - bool, for JSON booleans // - float64, for JSON numbers // - string, for JSON strings // - []any, for JSON arrays // - map[string]any, for JSON objects // - nil for JSON null // // To unmarshal a JSON array into a slice, Unmarshal decodes each JSON array // element into the corresponding slice element, reusing existing slice // elements in-place. The slice grows to accommodate additional elements, // or is truncated if the JSON array is shorter. // As a special case, to unmarshal an empty JSON array into a slice, // Unmarshal replaces the slice with a new empty slice. // // To unmarshal a JSON array into a Go array, Unmarshal decodes // JSON array elements into corresponding Go array elements. // If the Go array is smaller than the JSON array, // the additional JSON array elements are discarded. // If the JSON array is smaller than the Go array, // the additional Go array elements are set to zero values. // // To unmarshal a JSON object into a map, Unmarshal first establishes a map to // use. If the map is nil, Unmarshal allocates a new map. Otherwise Unmarshal // reuses the existing map, keeping existing entries. Unmarshal then stores // key-value pairs from the JSON object into the map. The map's key type must // either be any string type, an integer, or implement [encoding.TextUnmarshaler]. // // If the JSON-encoded data contain a syntax error, Unmarshal returns a [SyntaxError]. // // If a JSON value is not appropriate for a given target type, // or if a JSON number overflows the target type, Unmarshal // skips that field and completes the unmarshaling as best it can. // If no more serious errors are encountered, Unmarshal returns // an [UnmarshalTypeError] describing the earliest such error. In any // case, it's not guaranteed that all the remaining fields following // the problematic one will be unmarshaled into the target object. // // The JSON null value unmarshals into an interface, map, pointer, or slice // by setting that Go value to nil. Because null is often used in JSON to mean // “not present,” unmarshaling a JSON null into any other Go type has no effect // on the value and produces no error. // // When unmarshaling quoted strings, invalid UTF-8 or // invalid UTF-16 surrogate pairs are not treated as an error. // Instead, they are replaced by the Unicode replacement // character U+FFFD. func Unmarshal(data []byte, v any) error { return json.Unmarshal(data, v) } // Unmarshaler is the interface implemented by types // that can unmarshal a JSON description of themselves. // The input can be assumed to be a valid encoding of // a JSON value. UnmarshalJSON must copy the JSON data // if it wishes to retain the data after returning. type Unmarshaler = json.Unmarshaler // An UnmarshalTypeError describes a JSON value that was // not appropriate for a value of a specific Go type. type UnmarshalTypeError = json.UnmarshalTypeError // An UnmarshalFieldError describes a JSON object key that // led to an unexported (and therefore unwritable) struct field. // // Deprecated: No longer used; kept for compatibility. type UnmarshalFieldError = json.UnmarshalFieldError // An InvalidUnmarshalError describes an invalid argument passed to [Unmarshal]. // (The argument to [Unmarshal] must be a non-nil pointer.) type InvalidUnmarshalError = json.InvalidUnmarshalError // A Number represents a JSON number literal. type Number = json.Number // Marshal returns the JSON encoding of v. // // Marshal traverses the value v recursively. // // The input value is encoded as JSON according the following rules: // // - If the value type implements [jsonv2.MarshalerTo], // then the MarshalJSONTo method is called to encode the value. // If the method returns [errors.ErrUnsupported], // then the input is encoded according to subsequent rules. // // - If the value type implements [Marshaler], // then the MarshalJSON method is called to encode the value. // // - If the value type implements [encoding.TextAppender], // then the AppendText method is called to encode the value and // subsequently encode its result as a JSON string. // // - If the value type implements [encoding.TextMarshaler], // then the MarshalText method is called to encode the value and // subsequently encode its result as a JSON string. // // Otherwise, Marshal uses the following type-dependent default encodings: // // Boolean values encode as JSON booleans. // // Floating point, integer, and [Number] values encode as JSON numbers. // NaN and +/-Inf values will return an [UnsupportedValueError]. // // String values encode as JSON strings coerced to valid UTF-8, // replacing invalid bytes with the Unicode replacement rune. // So that the JSON will be safe to embed inside HTML