Source file src/encoding/json/v2_options.go

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

View as plain text