| 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 |
//go:build goexperiment.jsonv2 |
| 6 |
|
| 7 |
// Migrating to v2 |
| 8 |
// |
| 9 |
// This package (i.e., [encoding/json]) is now formally known as the v1 package |
| 10 |
// since a v2 package now exists at [encoding/json/v2]. |
| 11 |
// All the behavior of the v1 package is implemented in terms of |
| 12 |
// the v2 package with the appropriate set of options specified that |
| 13 |
// preserve the historical behavior of v1. |
| 14 |
// |
| 15 |
// The [jsonv2.Marshal] function is the newer equivalent of v1 [Marshal]. |
| 16 |
// The [jsonv2.Unmarshal] function is the newer equivalent of v1 [Unmarshal]. |
| 17 |
// The v2 functions have the same calling signature as the v1 equivalent |
| 18 |
// except that they take in variadic [Options] arguments that can be specified |
| 19 |
// to alter the behavior of marshal or unmarshal. Both v1 and v2 generally |
| 20 |
// behave in similar ways, but there are some notable differences. |
| 21 |
// |
| 22 |
// The following is a list of differences between v1 and v2: |
| 23 |
// |
| 24 |
// - In v1, JSON object members are unmarshaled into a Go struct using a |
| 25 |
// case-insensitive name match with the JSON name of the fields. |
| 26 |
// In contrast, v2 matches fields using an exact, case-sensitive match. |
| 27 |
// The [jsonv2.MatchCaseInsensitiveNames] and [MatchCaseSensitiveDelimiter] |
| 28 |
// options control this behavior difference. To explicitly specify a Go struct |
| 29 |
// field to use a particular name matching scheme, either the `case:ignore` |
| 30 |
// or the `case:strict` field option can be specified. |
| 31 |
// Field-specified options take precedence over caller-specified options. |
| 32 |
// |
| 33 |
// - In v1, when marshaling a Go struct, a field marked as `omitempty` |
| 34 |
// is omitted if the field value is an "empty" Go value, which is defined as |
| 35 |
// false, 0, a nil pointer, a nil interface value, and |
| 36 |
// any empty array, slice, map, or string. In contrast, v2 redefines |
| 37 |
// `omitempty` to omit a field if it encodes as an "empty" JSON value, |
| 38 |
// which is defined as a JSON null, or an empty JSON string, object, or array. |
| 39 |
// The [OmitEmptyWithLegacySemantics] option controls this behavior difference. |
| 40 |
// Note that `omitempty` behaves identically in both v1 and v2 for a |
| 41 |
// Go array, slice, map, or string (assuming no user-defined MarshalJSON method |
| 42 |
// overrides the default representation). Existing usages of `omitempty` on a |
| 43 |
// Go bool, number, pointer, or interface value should migrate to specifying |
| 44 |
// `omitzero` instead (which is identically supported in both v1 and v2). |
| 45 |
// |
| 46 |
// - In v1, a Go struct field marked as `string` can be used to quote a |
| 47 |
// Go string, bool, or number as a JSON string. It does not recursively |
| 48 |
// take effect on composite Go types. In contrast, v2 restricts |
| 49 |
// the `string` option to only quote a Go number as a JSON string. |
| 50 |
// It does recursively take effect on Go numbers within a composite Go type. |
| 51 |
// The [StringifyWithLegacySemantics] option controls this behavior difference. |
| 52 |
// |
| 53 |
// - In v1, a nil Go slice or Go map is marshaled as a JSON null. |
| 54 |
// In contrast, v2 marshals a nil Go slice or Go map as |
| 55 |
// an empty JSON array or JSON object, respectively. |
| 56 |
// The [jsonv2.FormatNilSliceAsNull] and [jsonv2.FormatNilMapAsNull] options |
| 57 |
// control this behavior difference. To explicitly specify a Go struct field |
| 58 |
// to use a particular representation for nil, either the `format:emitempty` |
| 59 |
// or `format:emitnull` field option can be specified. |
| 60 |
// Field-specified options take precedence over caller-specified options. |
| 61 |
// |
| 62 |
// - In v1, a Go array may be unmarshaled from a JSON array of any length. |
| 63 |
// In contrast, in v2 a Go array must be unmarshaled from a JSON array |
| 64 |
// of the same length, otherwise it results in an error. |
| 65 |
// The [UnmarshalArrayFromAnyLength] option controls this behavior difference. |
| 66 |
// |
| 67 |
// - In v1, a Go byte array is represented as a JSON array of JSON numbers. |
| 68 |
// In contrast, in v2 a Go byte array is represented as a Base64-encoded JSON string. |
| 69 |
// The [FormatByteArrayAsArray] option controls this behavior difference. |
| 70 |
// To explicitly specify a Go struct field to use a particular representation, |
| 71 |
// either the `format:array` or `format:base64` field option can be specified. |
| 72 |
// Field-specified options take precedence over caller-specified options. |
| 73 |
// |
| 74 |
// - In v1, MarshalJSON methods declared on a pointer receiver are only called |
| 75 |
// if the Go value is addressable. In contrast, in v2 a MarshalJSON method |
| 76 |
// is always callable regardless of addressability. |
| 77 |
// The [CallMethodsWithLegacySemantics] option controls this behavior difference. |
| 78 |
// |
| 79 |
// - In v1, MarshalJSON and UnmarshalJSON methods are never called for Go map keys. |
| 80 |
// In contrast, in v2 a MarshalJSON or UnmarshalJSON method is eligible for |
| 81 |
// being called for Go map keys. |
| 82 |
// The [CallMethodsWithLegacySemantics] option controls this behavior difference. |
| 83 |
// |
| 84 |
// - In v1, a Go map is marshaled in a deterministic order. |
| 85 |
// In contrast, in v2 a Go map is marshaled in a non-deterministic order. |
| 86 |
// The [jsonv2.Deterministic] option controls this behavior difference. |
| 87 |
// |
| 88 |
// - In v1, JSON strings are encoded with HTML-specific or JavaScript-specific |
| 89 |
// characters being escaped. In contrast, in v2 JSON strings use the minimal |
| 90 |
// encoding and only escape if required by the JSON grammar. |
| 91 |
// The [jsontext.EscapeForHTML] and [jsontext.EscapeForJS] options |
| 92 |
// control this behavior difference. |
| 93 |
// |
| 94 |
// - In v1, bytes of invalid UTF-8 within a string are silently replaced with |
| 95 |
// the Unicode replacement character. In contrast, in v2 the presence of |
| 96 |
// invalid UTF-8 results in an error. The [jsontext.AllowInvalidUTF8] option |
| 97 |
// controls this behavior difference. |
| 98 |
// |
| 99 |
// - In v1, a JSON object with duplicate names is permitted. |
| 100 |
// In contrast, in v2 a JSON object with duplicate names results in an error. |
| 101 |
// The [jsontext.AllowDuplicateNames] option controls this behavior difference. |
| 102 |
// |
| 103 |
// - In v1, when unmarshaling a JSON null into a non-empty Go value it will |
| 104 |
// inconsistently either zero out the value or do nothing. |
| 105 |
// In contrast, in v2 unmarshaling a JSON null will consistently and always |
| 106 |
// zero out the underlying Go value. The [MergeWithLegacySemantics] option |
| 107 |
// controls this behavior difference. |
| 108 |
// |
| 109 |
// - In v1, when unmarshaling a JSON value into a non-zero Go value, |
| 110 |
// it merges into the original Go value for array elements, slice elements, |
| 111 |
// struct fields (but not map values), |
| 112 |
// pointer values, and interface values (only if a non-nil pointer). |
| 113 |
// In contrast, in v2 unmarshal merges into the Go value |
| 114 |
// for struct fields, map values, pointer values, and interface values. |
| 115 |
// In general, the v2 semantic merges when unmarshaling a JSON object, |
| 116 |
// otherwise it replaces the value. The [MergeWithLegacySemantics] option |
| 117 |
// controls this behavior difference. |
| 118 |
// |
| 119 |
// - In v1, a [time.Duration] is represented as a JSON number containing |
| 120 |
// the decimal number of nanoseconds. In contrast, in v2 a [time.Duration] |
| 121 |
// has no default representation and results in a runtime error. |
| 122 |
// The [FormatDurationAsNano] option controls this behavior difference. |
| 123 |
// To explicitly specify a Go struct field to use a particular representation, |
| 124 |
// either the `format:nano` or `format:units` field option can be specified. |
| 125 |
// Field-specified options take precedence over caller-specified options. |
| 126 |
// |
| 127 |
// - In v1, errors are never reported at runtime for Go struct types |
| 128 |
// that have some form of structural error (e.g., a malformed tag option). |
| 129 |
// In contrast, v2 reports a runtime error for Go types that are invalid |
| 130 |
// as they relate to JSON serialization. For example, a Go struct |
| 131 |
// with only unexported fields cannot be serialized. |
| 132 |
// The [ReportErrorsWithLegacySemantics] option controls this behavior difference. |
| 133 |
// |
| 134 |
// As mentioned, the entirety of v1 is implemented in terms of v2, |
| 135 |
// where options are implicitly specified to opt into legacy behavior. |
| 136 |
// For example, [Marshal] directly calls [jsonv2.Marshal] with [DefaultOptionsV1]. |
| 137 |
// Similarly, [Unmarshal] directly calls [jsonv2.Unmarshal] with [DefaultOptionsV1]. |
| 138 |
// The [DefaultOptionsV1] option represents the set of all options that specify |
| 139 |
// default v1 behavior. |
| 140 |
// |
| 141 |
// For many of the behavior differences, there are Go struct field options |
| 142 |
// that the author of a Go type can specify to control the behavior such that |
| 143 |
// the type is represented identically in JSON under either v1 or v2 semantics. |
| 144 |
// |
| 145 |
// The availability of [DefaultOptionsV1] and [jsonv2.DefaultOptionsV2], |
| 146 |
// where later options take precedence over former options allows for |
| 147 |
// a gradual migration from v1 to v2. For example: |
| 148 |
// |
| 149 |
// - jsonv1.Marshal(v) |
| 150 |
// uses default v1 semantics. |
| 151 |
// |
| 152 |
// - jsonv2.Marshal(v, jsonv1.DefaultOptionsV1()) |
| 153 |
// is semantically equivalent to jsonv1.Marshal |
| 154 |
// and thus uses default v1 semantics. |
| 155 |
// |
| 156 |
// - jsonv2.Marshal(v, jsonv1.DefaultOptionsV1(), jsontext.AllowDuplicateNames(false)) |
| 157 |
// uses mostly v1 semantics, but opts into one particular v2-specific behavior. |
| 158 |
// |
| 159 |
// - jsonv2.Marshal(v, jsonv1.CallMethodsWithLegacySemantics(true)) |
| 160 |
// uses mostly v2 semantics, but opts into one particular v1-specific behavior. |
| 161 |
// |
| 162 |
// - jsonv2.Marshal(v, ..., jsonv2.DefaultOptionsV2()) |
| 163 |
// is semantically equivalent to jsonv2.Marshal since |
| 164 |
// jsonv2.DefaultOptionsV2 overrides any options specified earlier |
| 165 |
// and thus uses default v2 semantics. |
| 166 |
// |
| 167 |
// - jsonv2.Marshal(v) |
| 168 |
// uses default v2 semantics. |
| 169 |
// |
| 170 |
// All new usages of "json" in Go should use the v2 package, |
| 171 |
// but the v1 package will forever remain supported. |
| 172 |
package json |
| 173 |
|
| 174 |
// TODO(https://go.dev/issue/71631): Update the "Migrating to v2" documentation |
| 175 |
// with default v2 behavior for [time.Duration]. |
| 176 |
|
| 177 |
import ( |
| 178 |
"encoding" |
| 179 |
|
| 180 |
"encoding/json/internal/jsonflags" |
| 181 |
"encoding/json/internal/jsonopts" |
| 182 |
"encoding/json/jsontext" |
| 183 |
jsonv2 "encoding/json/v2" |
| 184 |
) |
| 185 |
|
| 186 |
// Reference encoding, jsonv2, and jsontext packages to assist pkgsite |
| 187 |
// in being able to hotlink references to those packages. |
| 188 |
var ( |
| 189 |
_ encoding.TextMarshaler |
| 190 |
_ encoding.TextUnmarshaler |
| 191 |
_ jsonv2.Options |
| 192 |
_ jsontext.Options |
| 193 |
) |
| 194 |
|
| 195 |
// Options are a set of options to configure the v2 "json" package |
| 196 |
// to operate with v1 semantics for particular features. |
| 197 |
// Values of this type can be passed to v2 functions like |
| 198 |
// [jsonv2.Marshal] or [jsonv2.Unmarshal]. |
| 199 |
// Instead of referencing this type, use [jsonv2.Options]. |
| 200 |
// |
| 201 |
// See the "Migrating to v2" section for guidance on how to migrate usage |
| 202 |
// of "json" from using v1 to using v2 instead. |
| 203 |
type Options = jsonopts.Options |
| 204 |
|
| 205 |
// DefaultOptionsV1 is the full set of all options that define v1 semantics. |
| 206 |
// It is equivalent to the following boolean options being set to true: |
| 207 |
// |
| 208 |
// - [CallMethodsWithLegacySemantics] |
| 209 |
// - [FormatByteArrayAsArray] |
| 210 |
// - [FormatBytesWithLegacySemantics] |
| 211 |
// - [FormatDurationAsNano] |
| 212 |
// - [MatchCaseSensitiveDelimiter] |
| 213 |
// - [MergeWithLegacySemantics] |
| 214 |
// - [OmitEmptyWithLegacySemantics] |
| 215 |
// - [ParseBytesWithLooseRFC4648] |
| 216 |
// - [ParseTimeWithLooseRFC3339] |
| 217 |
// - [ReportErrorsWithLegacySemantics] |
| 218 |
// - [StringifyWithLegacySemantics] |
| 219 |
// - [UnmarshalArrayFromAnyLength] |
| 220 |
// - [jsonv2.Deterministic] |
| 221 |
// - [jsonv2.FormatNilMapAsNull] |
| 222 |
// - [jsonv2.FormatNilSliceAsNull] |
| 223 |
// - [jsonv2.MatchCaseInsensitiveNames] |
| 224 |
// - [jsontext.AllowDuplicateNames] |
| 225 |
// - [jsontext.AllowInvalidUTF8] |
| 226 |
// - [jsontext.EscapeForHTML] |
| 227 |
// - [jsontext.EscapeForJS] |
| 228 |
// - [jsontext.PreserveRawStrings] |
| 229 |
// |
| 230 |
// All other boolean options are set to false. |
| 231 |
// All non-boolean options are set to the zero value, |
| 232 |
// except for [jsontext.WithIndent], which defaults to "\t". |
| 233 |
// |
| 234 |
// The [Marshal] and [Unmarshal] functions in this package are |
| 235 |
// semantically identical to calling the v2 equivalents with this option: |
| 236 |
// |
| 237 |
// jsonv2.Marshal(v, jsonv1.DefaultOptionsV1()) |
| 238 |
// jsonv2.Unmarshal(b, v, jsonv1.DefaultOptionsV1()) |
| 239 |
func DefaultOptionsV1() Options { |
| 240 |
return &jsonopts.DefaultOptionsV1 |
| 241 |
} |
| 242 |
|
| 243 |
// CallMethodsWithLegacySemantics specifies that calling of type-provided |
| 244 |
// marshal and unmarshal methods follow legacy semantics: |
| 245 |
// |
| 246 |
// - When marshaling, a marshal method declared on a pointer receiver |
| 247 |
// is only called if the Go value is addressable. |
| 248 |
// Values obtained from an interface or map element are not addressable. |
| 249 |
// Values obtained from a pointer or slice element are addressable. |
| 250 |
// Values obtained from an array element or struct field inherit |
| 251 |
// the addressability of the parent. In contrast, the v2 semantic |
| 252 |
// is to always call marshal methods regardless of addressability. |
| 253 |
// |
| 254 |
// - When marshaling or unmarshaling, the [Marshaler] or [Unmarshaler] |
| 255 |
// methods are ignored for map keys. However, [encoding.TextMarshaler] |
| 256 |
// or [encoding.TextUnmarshaler] are still callable. |
| 257 |
// In contrast, the v2 semantic is to serialize map keys |
| 258 |
// like any other value (with regard to calling methods), |
| 259 |
// which may include calling [Marshaler] or [Unmarshaler] methods, |
| 260 |
// where it is the implementation's responsibility to represent the |
| 261 |
// Go value as a JSON string (as required for JSON object names). |
| 262 |
// |
| 263 |
// - When marshaling, if a map key value implements a marshal method |
| 264 |
// and is a nil pointer, then it is serialized as an empty JSON string. |
| 265 |
// In contrast, the v2 semantic is to report an error. |
| 266 |
// |
| 267 |
// - When marshaling, if an interface type implements a marshal method |
| 268 |
// and the interface value is a nil pointer to a concrete type, |
| 269 |
// then the marshal method is always called. |
| 270 |
// In contrast, the v2 semantic is to never directly call methods |
| 271 |
// on interface values and to instead defer evaluation based upon |
| 272 |
// the underlying concrete value. Similar to non-interface values, |
| 273 |
// marshal methods are not called on nil pointers and |
| 274 |
// are instead serialized as a JSON null. |
| 275 |
// |
| 276 |
// This affects either marshaling or unmarshaling. |
| 277 |
// The v1 default is true. |
| 278 |
func CallMethodsWithLegacySemantics(v bool) Options { |
| 279 |
if v { |
| 280 |
return jsonflags.CallMethodsWithLegacySemantics | 1 |
| 281 |
} else { |
| 282 |
return jsonflags.CallMethodsWithLegacySemantics | 0 |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
// FormatByteArrayAsArray specifies that a Go [N]byte is |
| 287 |
// formatted as as a normal Go array in contrast to the v2 default of |
| 288 |
// formatting [N]byte as using binary data encoding (RFC 4648). |
| 289 |
// If a struct field has a `format` tag option, |
| 290 |
// then the specified formatting takes precedence. |
| 291 |
// |
| 292 |
// This affects either marshaling or unmarshaling. |
| 293 |
// The v1 default is true. |
| 294 |
func FormatByteArrayAsArray(v bool) Options { |
| 295 |
if v { |
| 296 |
return jsonflags.FormatByteArrayAsArray | 1 |
| 297 |
} else { |
| 298 |
return jsonflags.FormatByteArrayAsArray | 0 |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
// FormatBytesWithLegacySemantics specifies that handling of |
| 303 |
// []~byte and [N]~byte types follow legacy semantics: |
| 304 |
// |
| 305 |
// - A Go []~byte is to be treated as using some form of |
| 306 |
// binary data encoding (RFC 4648) in contrast to the v2 default |
| 307 |
// of only treating []byte as such. In particular, v2 does not |
| 308 |
// treat slices of named byte types as representing binary data. |
| 309 |
// |
| 310 |
// - When marshaling, if a named byte implements a marshal method, |
| 311 |
// then the slice is serialized as a JSON array of elements, |
| 312 |
// each of which call the marshal method. |
| 313 |
// |
| 314 |
// - When unmarshaling, if the input is a JSON array, |
| 315 |
// then unmarshal into the []~byte as if it were a normal Go slice. |
| 316 |
// In contrast, the v2 default is to report an error unmarshaling |
| 317 |
// a JSON array when expecting some form of binary data encoding. |
| 318 |
// |
| 319 |
// This affects either marshaling or unmarshaling. |
| 320 |
// The v1 default is true. |
| 321 |
func FormatBytesWithLegacySemantics(v bool) Options { |
| 322 |
if v { |
| 323 |
return jsonflags.FormatBytesWithLegacySemantics | 1 |
| 324 |
} else { |
| 325 |
return jsonflags.FormatBytesWithLegacySemantics | 0 |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
// FormatDurationAsNano specifies that a [time.Duration] is |
| 330 |
// formatted as a JSON number representing the number of nanoseconds |
| 331 |
// in contrast to the v2 default of reporting an error. |
| 332 |
// If a duration field has a `format` tag option, |
| 333 |
// then the specified formatting takes precedence. |
| 334 |
// |
| 335 |
// This affects either marshaling or unmarshaling. |
| 336 |
// The v1 default is true. |
| 337 |
func FormatDurationAsNano(v bool) Options { |
| 338 |
// TODO(https://go.dev/issue/71631): Update documentation with v2 behavior. |
| 339 |
if v { |
| 340 |
return jsonflags.FormatDurationAsNano | 1 |
| 341 |
} else { |
| 342 |
return jsonflags.FormatDurationAsNano | 0 |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
// MatchCaseSensitiveDelimiter specifies that underscores and dashes are |
| 347 |
// not to be ignored when performing case-insensitive name matching which |
| 348 |
// occurs under [jsonv2.MatchCaseInsensitiveNames] or the `case:ignore` tag option. |
| 349 |
// Thus, case-insensitive name matching is identical to [strings.EqualFold]. |
| 350 |
// Use of this option diminishes the ability of case-insensitive matching |
| 351 |
// to be able to match common case variants (e.g, "foo_bar" with "fooBar"). |
| 352 |
// |
| 353 |
// This affects either marshaling or unmarshaling. |
| 354 |
// The v1 default is true. |
| 355 |
func MatchCaseSensitiveDelimiter(v bool) Options { |
| 356 |
if v { |
| 357 |
return jsonflags.MatchCaseSensitiveDelimiter | 1 |
| 358 |
} else { |
| 359 |
return jsonflags.MatchCaseSensitiveDelimiter | 0 |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
// MergeWithLegacySemantics specifies that unmarshaling into a non-zero |
| 364 |
// Go value follows legacy semantics: |
| 365 |
// |
| 366 |
// - When unmarshaling a JSON null, this preserves the original Go value |
| 367 |
// if the kind is a bool, int, uint, float, string, array, or struct. |
| 368 |
// Otherwise, it zeros the Go value. |
| 369 |
// In contrast, the default v2 behavior is to consistently and always |
| 370 |
// zero the Go value when unmarshaling a JSON null into it. |
| 371 |
// |
| 372 |
// - When unmarshaling a JSON value other than null, this merges into |
| 373 |
// the original Go value for array elements, slice elements, |
| 374 |
// struct fields (but not map values), |
| 375 |
// pointer values, and interface values (only if a non-nil pointer). |
| 376 |
// In contrast, the default v2 behavior is to merge into the Go value |
| 377 |
// for struct fields, map values, pointer values, and interface values. |
| 378 |
// In general, the v2 semantic merges when unmarshaling a JSON object, |
| 379 |
// otherwise it replaces the original value. |
| 380 |
// |
| 381 |
// This only affects unmarshaling and is ignored when marshaling. |
| 382 |
// The v1 default is true. |
| 383 |
func MergeWithLegacySemantics(v bool) Options { |
| 384 |
if v { |
| 385 |
return jsonflags.MergeWithLegacySemantics | 1 |
| 386 |
} else { |
| 387 |
return jsonflags.MergeWithLegacySemantics | 0 |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
// OmitEmptyWithLegacySemantics specifies that the `omitempty` tag option |
| 392 |
// follows a definition of empty where a field is omitted if the Go value is |
| 393 |
// false, 0, a nil pointer, a nil interface value, |
| 394 |
// or any empty array, slice, map, or string. |
| 395 |
// This overrides the v2 semantic where a field is empty if the value |
| 396 |
// marshals as a JSON null or an empty JSON string, object, or array. |
| 397 |
// |
| 398 |
// The v1 and v2 definitions of `omitempty` are practically the same for |
| 399 |
// Go strings, slices, arrays, and maps. Usages of `omitempty` on |
| 400 |
// Go bools, ints, uints floats, pointers, and interfaces should migrate to use |
| 401 |
// the `omitzero` tag option, which omits a field if it is the zero Go value. |
| 402 |
// |
| 403 |
// This only affects marshaling and is ignored when unmarshaling. |
| 404 |
// The v1 default is true. |
| 405 |
func OmitEmptyWithLegacySemantics(v bool) Options { |
| 406 |
if v { |
| 407 |
return jsonflags.OmitEmptyWithLegacySemantics | 1 |
| 408 |
} else { |
| 409 |
return jsonflags.OmitEmptyWithLegacySemantics | 0 |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
// ParseBytesWithLooseRFC4648 specifies that when parsing |
| 414 |
// binary data encoded as "base32" or "base64", |
| 415 |
// to ignore the presence of '\r' and '\n' characters. |
| 416 |
// In contrast, the v2 default is to report an error in order to be |
| 417 |
// strictly compliant with RFC 4648, section 3.3, |
| 418 |
// which specifies that non-alphabet characters must be rejected. |
| 419 |
// |
| 420 |
// This only affects unmarshaling and is ignored when marshaling. |
| 421 |
// The v1 default is true. |
| 422 |
func ParseBytesWithLooseRFC4648(v bool) Options { |
| 423 |
if v { |
| 424 |
return jsonflags.ParseBytesWithLooseRFC4648 | 1 |
| 425 |
} else { |
| 426 |
return jsonflags.ParseBytesWithLooseRFC4648 | 0 |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
// ParseTimeWithLooseRFC3339 specifies that a [time.Time] |
| 431 |
// parses according to loose adherence to RFC 3339. |
| 432 |
// In particular, it permits historically incorrect representations, |
| 433 |
// allowing for deviations in hour format, sub-second separator, |
| 434 |
// and timezone representation. In contrast, the default v2 behavior |
| 435 |
// is to strictly comply with the grammar specified in RFC 3339. |
| 436 |
// |
| 437 |
// This only affects unmarshaling and is ignored when marshaling. |
| 438 |
// The v1 default is true. |
| 439 |
func ParseTimeWithLooseRFC3339(v bool) Options { |
| 440 |
if v { |
| 441 |
return jsonflags.ParseTimeWithLooseRFC3339 | 1 |
| 442 |
} else { |
| 443 |
return jsonflags.ParseTimeWithLooseRFC3339 | 0 |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
// ReportErrorsWithLegacySemantics specifies that Marshal and Unmarshal |
| 448 |
// should report errors with legacy semantics: |
| 449 |
// |
| 450 |
// - When marshaling or unmarshaling, the returned error values are |
| 451 |
// usually of types such as [SyntaxError], [MarshalerError], |
| 452 |
// [UnsupportedTypeError], [UnsupportedValueError], |
| 453 |
// [InvalidUnmarshalError], or [UnmarshalTypeError]. |
| 454 |
// In contrast, the v2 semantic is to always return errors as either |
| 455 |
// [jsonv2.SemanticError] or [jsontext.SyntacticError]. |
| 456 |
// |
| 457 |
// - When marshaling, if a user-defined marshal method reports an error, |
| 458 |
// it is always wrapped in a [MarshalerError], even if the error itself |
| 459 |
// is already a [MarshalerError], which may lead to multiple redundant |
| 460 |
// layers of wrapping. In contrast, the v2 semantic is to |
| 461 |
// always wrap an error within [jsonv2.SemanticError] |
| 462 |
// unless it is already a semantic error. |
| 463 |
// |
| 464 |
// - When unmarshaling, if a user-defined unmarshal method reports an error, |
| 465 |
// it is never wrapped and reported verbatim. In contrast, the v2 semantic |
| 466 |
// is to always wrap an error within [jsonv2.SemanticError] |
| 467 |
// unless it is already a semantic error. |
| 468 |
// |
| 469 |
// - When marshaling or unmarshaling, if a Go struct contains type errors |
| 470 |
// (e.g., conflicting names or malformed field tags), then such errors |
| 471 |
// are ignored and the Go struct uses a best-effort representation. |
| 472 |
// In contrast, the v2 semantic is to report a runtime error. |
| 473 |
// |
| 474 |
// - When unmarshaling, the syntactic structure of the JSON input |
| 475 |
// is fully validated before performing the semantic unmarshaling |
| 476 |
// of the JSON data into the Go value. Practically speaking, |
| 477 |
// this means that JSON input with syntactic errors do not result |
| 478 |
// in any mutations of the target Go value. In contrast, the v2 semantic |
| 479 |
// is to perform a streaming decode and gradually unmarshal the JSON input |
| 480 |
// into the target Go value, which means that the Go value may be |
| 481 |
// partially mutated when a syntactic error is encountered. |
| 482 |
// |
| 483 |
// - When unmarshaling, a semantic error does not immediately terminate the |
| 484 |
// unmarshal procedure, but rather evaluation continues. |
| 485 |
// When unmarshal returns, only the first semantic error is reported. |
| 486 |
// In contrast, the v2 semantic is to terminate unmarshal the moment |
| 487 |
// an error is encountered. |
| 488 |
// |
| 489 |
// This affects either marshaling or unmarshaling. |
| 490 |
// The v1 default is true. |
| 491 |
func ReportErrorsWithLegacySemantics(v bool) Options { |
| 492 |
if v { |
| 493 |
return jsonflags.ReportErrorsWithLegacySemantics | 1 |
| 494 |
} else { |
| 495 |
return jsonflags.ReportErrorsWithLegacySemantics | 0 |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
// StringifyWithLegacySemantics specifies that the `string` tag option |
| 500 |
// may stringify bools and string values. It only takes effect on fields |
| 501 |
// where the top-level type is a bool, string, numeric kind, or a pointer to |
| 502 |
// such a kind. Specifically, `string` will not stringify bool, string, |
| 503 |
// or numeric kinds within a composite data type |
| 504 |
// (e.g., array, slice, struct, map, or interface). |
| 505 |
// |
| 506 |
// When marshaling, such Go values are serialized as their usual |
| 507 |
// JSON representation, but quoted within a JSON string. |
| 508 |
// When unmarshaling, such Go values must be deserialized from |
| 509 |
// a JSON string containing their usual JSON representation. |
| 510 |
// A JSON null quoted in a JSON string is a valid substitute for JSON null |
| 511 |
// while unmarshaling into a Go value that `string` takes effect on. |
| 512 |
// |
| 513 |
// This affects either marshaling or unmarshaling. |
| 514 |
// The v1 default is true. |
| 515 |
func StringifyWithLegacySemantics(v bool) Options { |
| 516 |
if v { |
| 517 |
return jsonflags.StringifyWithLegacySemantics | 1 |
| 518 |
} else { |
| 519 |
return jsonflags.StringifyWithLegacySemantics | 0 |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
// UnmarshalArrayFromAnyLength specifies that Go arrays can be unmarshaled |
| 524 |
// from input JSON arrays of any length. If the JSON array is too short, |
| 525 |
// then the remaining Go array elements are zeroed. If the JSON array |
| 526 |
// is too long, then the excess JSON array elements are skipped over. |
| 527 |
// |
| 528 |
// This only affects unmarshaling and is ignored when marshaling. |
| 529 |
// The v1 default is true. |
| 530 |
func UnmarshalArrayFromAnyLength(v bool) Options { |
| 531 |
if v { |
| 532 |
return jsonflags.UnmarshalArrayFromAnyLength | 1 |
| 533 |
} else { |
| 534 |
return jsonflags.UnmarshalArrayFromAnyLength | 0 |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
// unmarshalAnyWithRawNumber specifies that unmarshaling a JSON number into |
| 539 |
// an empty Go interface should use the Number type instead of a float64. |
| 540 |
func unmarshalAnyWithRawNumber(v bool) Options { |
| 541 |
if v { |
| 542 |
return jsonflags.UnmarshalAnyWithRawNumber | 1 |
| 543 |
} else { |
| 544 |
return jsonflags.UnmarshalAnyWithRawNumber | 0 |
| 545 |
} |
| 546 |
} |