| 1 | // Copyright 2020 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 json implements serialization of JSON |
| 6 | // as specified in RFC 4627, RFC 7159, RFC 7493, RFC 8259, and RFC 8785. |
| 7 | // JSON is a simple data interchange format that can represent |
| 8 | // primitive data types such as booleans, strings, and numbers, |
| 9 | // in addition to structured data types such as objects and arrays. |
| 10 | // |
| 11 | // # Terminology |
| 12 | // |
| 13 | // This package uses the terms "encode" and "decode" for syntactic functionality |
| 14 | // that is concerned with processing JSON based on its grammar, and |
| 15 | // uses the terms "marshal" and "unmarshal" for semantic functionality |
| 16 | // that determines the meaning of JSON values as Go values and vice-versa. |
| 17 | // It aims to provide a clear distinction between functionality that |
| 18 | // is purely concerned with encoding versus that of marshaling. |
| 19 | // For example, one can directly encode a stream of JSON tokens without |
| 20 | // needing to marshal a concrete Go value representing them. |
| 21 | // Similarly, one can decode a stream of JSON tokens without |
| 22 | // needing to unmarshal them into a concrete Go value. |
| 23 | // |
| 24 | // This package uses JSON terminology when discussing JSON, which may differ |
| 25 | // from related concepts in Go or elsewhere in computing literature. |
| 26 | // |
| 27 | // - A JSON "object" refers to an unordered collection of name/value members. |
| 28 | // - A JSON "array" refers to an ordered sequence of elements. |
| 29 | // - A JSON "value" refers to either a literal (i.e., null, false, or true), |
| 30 | // string, number, object, or array. |
| 31 | // |
| 32 | // See RFC 8259 for more information. |
| 33 | // |
| 34 | // # Specifications |
| 35 | // |
| 36 | // Relevant specifications include RFC 4627, RFC 7159, RFC 7493, RFC 8259, |
| 37 | // and RFC 8785. Each RFC is generally a stricter subset of another RFC. |
| 38 | // In increasing order of strictness: |
| 39 | // |
| 40 | // - RFC 4627 and RFC 7159 do not require (but recommend) the use of UTF-8 |
| 41 | // and also do not require (but recommend) that object names be unique. |
| 42 | // - RFC 8259 requires the use of UTF-8, |
| 43 | // but does not require (but recommends) that object names be unique. |
| 44 | // - RFC 7493 requires the use of UTF-8 |
| 45 | // and also requires that object names be unique. |
| 46 | // - RFC 8785 defines a canonical representation. It requires the use of UTF-8 |
| 47 | // and also requires that object names be unique and in a specific ordering. |
| 48 | // It specifies exactly how strings and numbers must be formatted. |
| 49 | // |
| 50 | // The primary difference between RFC 4627 and RFC 7159 is that the former |
| 51 | // restricted top-level values to only JSON objects and arrays, while |
| 52 | // RFC 7159 and subsequent RFCs permit top-level values to additionally be |
| 53 | // JSON nulls, booleans, strings, or numbers. |
| 54 | // |
| 55 | // By default, this package operates on RFC 7493, but can be configured |
| 56 | // to operate according to the other RFC specifications. |
| 57 | // RFC 7493 is a stricter subset of RFC 8259 and fully compliant with it. |
| 58 | // In particular, it makes specific choices about behavior that RFC 8259 |
| 59 | // leaves as undefined in order to ensure greater interoperability. |
| 60 | // |
| 61 | // # JSON Representation of Go structs |
| 62 | // |
| 63 | // A Go struct is naturally represented as a JSON object, |
| 64 | // where each Go struct field corresponds with a JSON object member. |
| 65 | // When marshaling, all Go struct fields are recursively encoded in depth-first |
| 66 | // order as JSON object members except those that are ignored or omitted. |
| 67 | // When unmarshaling, JSON object members are recursively decoded |
| 68 | // into the corresponding Go struct fields. |
| 69 | // Object members that do not match any struct fields, |
| 70 | // also known as “unknown members”, are ignored by default or rejected |
| 71 | // if UnmarshalOptions.RejectUnknownMembers is specified. |
| 72 | // |
| 73 | // The representation of each struct field can be customized in the |
| 74 | // "json" struct field tag, where the tag is a comma separated list of options. |
| 75 | // As a special case, if the entire tag is `json:"-"`, |
| 76 | // then the field is ignored with regard to its JSON representation. |
| 77 | // |
| 78 | // The first option is the JSON object name override for the Go struct field. |
| 79 | // If the name is not specified, then the Go struct field name |
| 80 | // is used as the JSON object name. JSON names containing commas or quotes, |
| 81 | // or names identical to "" or "-", can be specified using |
| 82 | // a single-quoted string literal, where the syntax is identical to |
| 83 | // the Go grammar for a double-quoted string literal, |
| 84 | // but instead uses single quotes as the delimiters. |
| 85 | // By default, unmarshaling uses case-sensitive matching to identify |
| 86 | // the Go struct field associated with a JSON object name. |
| 87 | // |
| 88 | // After the name, the following tag options are supported: |
| 89 | // |
| 90 | // - omitzero: When marshaling, the "omitzero" option specifies that |
| 91 | // the struct field should be omitted if the field value is zero |
| 92 | // as determined by the "IsZero() bool" method if present, |
| 93 | // otherwise based on whether the field is the zero Go value. |
| 94 | // This option has no effect when unmarshaling. |
| 95 | // |
| 96 | // - omitempty: When marshaling, the "omitempty" option specifies that |
| 97 | // the struct field should be omitted if the field value would have been |
| 98 | // encoded as a JSON null, empty string, empty object, or empty array. |
| 99 | // This option has no effect when unmarshaling. |
| 100 | // |
| 101 | // - string: The "string" option specifies that |
| 102 | // MarshalOptions.StringifyNumbers and UnmarshalOptions.StringifyNumbers |
| 103 | // be set when marshaling or unmarshaling a struct field value. |
| 104 | // This causes numeric types to be encoded as a JSON number |
| 105 | // within a JSON string, and to be decoded from either a JSON number or |
| 106 | // a JSON string containing a JSON number. |
| 107 | // This extra level of encoding is often necessary since |
| 108 | // many JSON parsers cannot precisely represent 64-bit integers. |
| 109 | // |
| 110 | // - nocase: When unmarshaling, the "nocase" option specifies that |
| 111 | // if the JSON object name does not exactly match the JSON name |
| 112 | // for any of the struct fields, then it attempts to match the struct field |
| 113 | // using a case-insensitive match that also ignores dashes and underscores. |
| 114 | // If multiple fields match, the first declared field in breadth-first order |
| 115 | // takes precedence. This option has no effect when marshaling. |
| 116 | // |
| 117 | // - inline: The "inline" option specifies that |
| 118 | // the JSON representable content of this field type is to be promoted |
| 119 | // as if they were specified in the parent struct. |
| 120 | // It is the JSON equivalent of Go struct embedding. |
| 121 | // A Go embedded field is implicitly inlined unless an explicit JSON name |
| 122 | // is specified. The inlined field must be a Go struct |
| 123 | // (that does not implement any JSON methods), RawValue, map[string]T, |
| 124 | // or an unnamed pointer to such types. When marshaling, |
| 125 | // inlined fields from a pointer type are omitted if it is nil. |
| 126 | // Inlined fields of type RawValue and map[string]T are called |
| 127 | // “inlined fallbacks” as they can represent all possible |
| 128 | // JSON object members not directly handled by the parent struct. |
| 129 | // Only one inlined fallback field may be specified in a struct, |
| 130 | // while many non-fallback fields may be specified. This option |
| 131 | // must not be specified with any other option (including the JSON name). |
| 132 | // |
| 133 | // - unknown: The "unknown" option is a specialized variant |
| 134 | // of the inlined fallback to indicate that this Go struct field |
| 135 | // contains any number of unknown JSON object members. The field type |
| 136 | // must be a RawValue, map[string]T, or an unnamed pointer to such types. |
| 137 | // If MarshalOptions.DiscardUnknownMembers is specified when marshaling, |
| 138 | // the contents of this field are ignored. |
| 139 | // If UnmarshalOptions.RejectUnknownMembers is specified when unmarshaling, |
| 140 | // any unknown object members are rejected regardless of whether |
| 141 | // an inlined fallback with the "unknown" option exists. This option |
| 142 | // must not be specified with any other option (including the JSON name). |
| 143 | // |
| 144 | // - format: The "format" option specifies a format flag |
| 145 | // used to specialize the formatting of the field value. |
| 146 | // The option is a key-value pair specified as "format:value" where |
| 147 | // the value must be either a literal consisting of letters and numbers |
| 148 | // (e.g., "format:RFC3339") or a single-quoted string literal |
| 149 | // (e.g., "format:'2006-01-02'"). The interpretation of the format flag |
| 150 | // is determined by the struct field type. |
| 151 | // |
| 152 | // The "omitzero" and "omitempty" options are mostly semantically identical. |
| 153 | // The former is defined in terms of the Go type system, |
| 154 | // while the latter in terms of the JSON type system. |
| 155 | // Consequently they behave differently in some circumstances. |
| 156 | // For example, only a nil slice or map is omitted under "omitzero", while |
| 157 | // an empty slice or map is omitted under "omitempty" regardless of nilness. |
| 158 | // The "omitzero" option is useful for types with a well-defined zero value |
| 159 | // (e.g., netip.Addr) or have an IsZero method (e.g., time.Time). |
| 160 | // |
| 161 | // Every Go struct corresponds to a list of JSON representable fields |
| 162 | // which is constructed by performing a breadth-first search over |
| 163 | // all struct fields (excluding unexported or ignored fields), |
| 164 | // where the search recursively descends into inlined structs. |
| 165 | // The set of non-inlined fields in a struct must have unique JSON names. |
| 166 | // If multiple fields all have the same JSON name, then the one |
| 167 | // at shallowest depth takes precedence and the other fields at deeper depths |
| 168 | // are excluded from the list of JSON representable fields. |
| 169 | // If multiple fields at the shallowest depth have the same JSON name, |
| 170 | // then all of those fields are excluded from the list. This is analogous to |
| 171 | // Go visibility rules for struct field selection with embedded struct types. |
| 172 | // |
| 173 | // Marshaling or unmarshaling a non-empty struct |
| 174 | // without any JSON representable fields results in a SemanticError. |
| 175 | // Unexported fields must not have any `json` tags except for `json:"-"`. |
| 176 | package json |
| 177 | |
| 178 | // requireKeyedLiterals can be embedded in a struct to require keyed literals. |
| 179 | type requireKeyedLiterals struct{} |
| 180 | |
| 181 | // nonComparable can be embedded in a struct to prevent comparability. |
| 182 | type nonComparable [0]func() |