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 [OmitEmptyWithLegacyDefinition] 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 [FormatBytesWithLegacySemantics] 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  //     is represented as a JSON string containing the formatted duration
   122  //     (e.g., "1h2m3.456s") according to [time.Duration.String].
   123  //     The [FormatTimeWithLegacySemantics] 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  import (
   176  	"encoding"
   177  
   178  	"encoding/json/internal/jsonflags"
   179  	"encoding/json/internal/jsonopts"
   180  	"encoding/json/jsontext"
   181  	jsonv2 "encoding/json/v2"
   182  )
   183  
   184  // Reference encoding, jsonv2, and jsontext packages to assist pkgsite
   185  // in being able to hotlink references to those packages.
   186  var (
   187  	_ encoding.TextMarshaler
   188  	_ encoding.TextUnmarshaler
   189  	_ jsonv2.Options
   190  	_ jsontext.Options
   191  )
   192  
   193  // Options are a set of options to configure the v2 "json" package
   194  // to operate with v1 semantics for particular features.
   195  // Values of this type can be passed to v2 functions like
   196  // [jsonv2.Marshal] or [jsonv2.Unmarshal].
   197  // Instead of referencing this type, use [jsonv2.Options].
   198  //
   199  // See the "Migrating to v2" section for guidance on how to migrate usage
   200  // of "json" from using v1 to using v2 instead.
   201  type Options = jsonopts.Options
   202  
   203  // DefaultOptionsV1 is the full set of all options that define v1 semantics.
   204  // It is equivalent to the following boolean options being set to true:
   205  //
   206  //   - [CallMethodsWithLegacySemantics]
   207  //   - [EscapeInvalidUTF8]
   208  //   - [FormatBytesWithLegacySemantics]
   209  //   - [FormatTimeWithLegacySemantics]
   210  //   - [MatchCaseSensitiveDelimiter]
   211  //   - [MergeWithLegacySemantics]
   212  //   - [OmitEmptyWithLegacyDefinition]
   213  //   - [ReportErrorsWithLegacySemantics]
   214  //   - [StringifyWithLegacySemantics]
   215  //   - [UnmarshalArrayFromAnyLength]
   216  //   - [jsonv2.Deterministic]
   217  //   - [jsonv2.FormatNilMapAsNull]
   218  //   - [jsonv2.FormatNilSliceAsNull]
   219  //   - [jsonv2.MatchCaseInsensitiveNames]
   220  //   - [jsontext.AllowDuplicateNames]
   221  //   - [jsontext.AllowInvalidUTF8]
   222  //   - [jsontext.EscapeForHTML]
   223  //   - [jsontext.EscapeForJS]
   224  //   - [jsontext.PreserveRawString]
   225  //
   226  // All other boolean options are set to false.
   227  // All non-boolean options are set to the zero value,
   228  // except for [jsontext.WithIndent], which defaults to "\t".
   229  //
   230  // The [Marshal] and [Unmarshal] functions in this package are
   231  // semantically identical to calling the v2 equivalents with this option:
   232  //
   233  //	jsonv2.Marshal(v, jsonv1.DefaultOptionsV1())
   234  //	jsonv2.Unmarshal(b, v, jsonv1.DefaultOptionsV1())
   235  func DefaultOptionsV1() Options {
   236  	return &jsonopts.DefaultOptionsV1
   237  }
   238  
   239  // CallMethodsWithLegacySemantics specifies that calling of type-provided
   240  // marshal and unmarshal methods follow legacy semantics:
   241  //
   242  //   - When marshaling, a marshal method declared on a pointer receiver
   243  //     is only called if the Go value is addressable.
   244  //     Values obtained from an interface or map element are not addressable.
   245  //     Values obtained from a pointer or slice element are addressable.
   246  //     Values obtained from an array element or struct field inherit
   247  //     the addressability of the parent. In contrast, the v2 semantic
   248  //     is to always call marshal methods regardless of addressability.
   249  //
   250  //   - When marshaling or unmarshaling, the [Marshaler] or [Unmarshaler]
   251  //     methods are ignored for map keys. However, [encoding.TextMarshaler]
   252  //     or [encoding.TextUnmarshaler] are still callable.
   253  //     In contrast, the v2 semantic is to serialize map keys
   254  //     like any other value (with regard to calling methods),
   255  //     which may include calling [Marshaler] or [Unmarshaler] methods,
   256  //     where it is the implementation's responsibility to represent the
   257  //     Go value as a JSON string (as required for JSON object names).
   258  //
   259  //   - When marshaling, if a map key value implements a marshal method
   260  //     and is a nil pointer, then it is serialized as an empty JSON string.
   261  //     In contrast, the v2 semantic is to report an error.
   262  //
   263  //   - When marshaling, if an interface type implements a marshal method
   264  //     and the interface value is a nil pointer to a concrete type,
   265  //     then the marshal method is always called.
   266  //     In contrast, the v2 semantic is to never directly call methods
   267  //     on interface values and to instead defer evaluation based upon
   268  //     the underlying concrete value. Similar to non-interface values,
   269  //     marshal methods are not called on nil pointers and
   270  //     are instead serialized as a JSON null.
   271  //
   272  // This affects either marshaling or unmarshaling.
   273  // The v1 default is true.
   274  func CallMethodsWithLegacySemantics(v bool) Options {
   275  	if v {
   276  		return jsonflags.CallMethodsWithLegacySemantics | 1
   277  	} else {
   278  		return jsonflags.CallMethodsWithLegacySemantics | 0
   279  	}
   280  }
   281  
   282  // EscapeInvalidUTF8 specifies that when encoding a [jsontext.String]
   283  // with bytes of invalid UTF-8, such bytes are escaped as
   284  // a hexadecimal Unicode codepoint (i.e., \ufffd).
   285  // In contrast, the v2 default is to use the minimal representation,
   286  // which is to encode invalid UTF-8 as the Unicode replacement rune itself
   287  // (without any form of escaping).
   288  //
   289  // This only affects encoding and is ignored when decoding.
   290  // The v1 default is true.
   291  func EscapeInvalidUTF8(v bool) Options {
   292  	if v {
   293  		return jsonflags.EscapeInvalidUTF8 | 1
   294  	} else {
   295  		return jsonflags.EscapeInvalidUTF8 | 0
   296  	}
   297  }
   298  
   299  // FormatBytesWithLegacySemantics specifies that handling of
   300  // []~byte and [N]~byte types follow legacy semantics:
   301  //
   302  //   - A Go [N]~byte is always treated as as a normal Go array
   303  //     in contrast to the v2 default of treating [N]byte as
   304  //     using some form of binary data encoding (RFC 4648).
   305  //
   306  //   - A Go []~byte is to be treated as using some form of
   307  //     binary data encoding (RFC 4648) in contrast to the v2 default
   308  //     of only treating []byte as such. In particular, v2 does not
   309  //     treat slices of named byte types as representing binary data.
   310  //
   311  //   - When marshaling, if a named byte implements a marshal method,
   312  //     then the slice is serialized as a JSON array of elements,
   313  //     each of which call the marshal method.
   314  //
   315  //   - When unmarshaling, if the input is a JSON array,
   316  //     then unmarshal into the []~byte as if it were a normal Go slice.
   317  //     In contrast, the v2 default is to report an error unmarshaling
   318  //     a JSON array when expecting some form of binary data encoding.
   319  //
   320  //   - When unmarshaling, '\r' and '\n' characters are ignored
   321  //     within the encoded "base32" and "base64" data.
   322  //     In contrast, the v2 default is to report an error in order to be
   323  //     strictly compliant with RFC 4648, section 3.3,
   324  //     which specifies that non-alphabet characters must be rejected.
   325  //
   326  // This affects either marshaling or unmarshaling.
   327  // The v1 default is true.
   328  func FormatBytesWithLegacySemantics(v bool) Options {
   329  	if v {
   330  		return jsonflags.FormatBytesWithLegacySemantics | 1
   331  	} else {
   332  		return jsonflags.FormatBytesWithLegacySemantics | 0
   333  	}
   334  }
   335  
   336  // FormatTimeWithLegacySemantics specifies that [time] types are formatted
   337  // with legacy semantics:
   338  //
   339  //   - When marshaling or unmarshaling, a [time.Duration] is formatted as
   340  //     a JSON number representing the number of nanoseconds.
   341  //     In contrast, the default v2 behavior uses a JSON string
   342  //     with the duration formatted with [time.Duration.String].
   343  //     If a duration field has a `format` tag option,
   344  //     then the specified formatting takes precedence.
   345  //
   346  //   - When unmarshaling, a [time.Time] follows loose adherence to RFC 3339.
   347  //     In particular, it permits historically incorrect representations,
   348  //     allowing for deviations in hour format, sub-second separator,
   349  //     and timezone representation. In contrast, the default v2 behavior
   350  //     is to strictly comply with the grammar specified in RFC 3339.
   351  //
   352  // This affects either marshaling or unmarshaling.
   353  // The v1 default is true.
   354  func FormatTimeWithLegacySemantics(v bool) Options {
   355  	if v {
   356  		return jsonflags.FormatTimeWithLegacySemantics | 1
   357  	} else {
   358  		return jsonflags.FormatTimeWithLegacySemantics | 0
   359  	}
   360  }
   361  
   362  // MatchCaseSensitiveDelimiter specifies that underscores and dashes are
   363  // not to be ignored when performing case-insensitive name matching which
   364  // occurs under [jsonv2.MatchCaseInsensitiveNames] or the `case:ignore` tag option.
   365  // Thus, case-insensitive name matching is identical to [strings.EqualFold].
   366  // Use of this option diminishes the ability of case-insensitive matching
   367  // to be able to match common case variants (e.g, "foo_bar" with "fooBar").
   368  //
   369  // This affects either marshaling or unmarshaling.
   370  // The v1 default is true.
   371  func MatchCaseSensitiveDelimiter(v bool) Options {
   372  	if v {
   373  		return jsonflags.MatchCaseSensitiveDelimiter | 1
   374  	} else {
   375  		return jsonflags.MatchCaseSensitiveDelimiter | 0
   376  	}
   377  }
   378  
   379  // MergeWithLegacySemantics specifies that unmarshaling into a non-zero
   380  // Go value follows legacy semantics:
   381  //
   382  //   - When unmarshaling a JSON null, this preserves the original Go value
   383  //     if the kind is a bool, int, uint, float, string, array, or struct.
   384  //     Otherwise, it zeros the Go value.
   385  //     In contrast, the default v2 behavior is to consistently and always
   386  //     zero the Go value when unmarshaling a JSON null into it.
   387  //
   388  //   - When unmarshaling a JSON value other than null, this merges into
   389  //     the original Go value for array elements, slice elements,
   390  //     struct fields (but not map values),
   391  //     pointer values, and interface values (only if a non-nil pointer).
   392  //     In contrast, the default v2 behavior is to merge into the Go value
   393  //     for struct fields, map values, pointer values, and interface values.
   394  //     In general, the v2 semantic merges when unmarshaling a JSON object,
   395  //     otherwise it replaces the original value.
   396  //
   397  // This only affects unmarshaling and is ignored when marshaling.
   398  // The v1 default is true.
   399  func MergeWithLegacySemantics(v bool) Options {
   400  	if v {
   401  		return jsonflags.MergeWithLegacySemantics | 1
   402  	} else {
   403  		return jsonflags.MergeWithLegacySemantics | 0
   404  	}
   405  }
   406  
   407  // OmitEmptyWithLegacyDefinition specifies that the `omitempty` tag option
   408  // follows a definition of empty where a field is omitted if the Go value is
   409  // false, 0, a nil pointer, a nil interface value,
   410  // or any empty array, slice, map, or string.
   411  // This overrides the v2 semantic where a field is empty if the value
   412  // marshals as a JSON null or an empty JSON string, object, or array.
   413  //
   414  // The v1 and v2 definitions of `omitempty` are practically the same for
   415  // Go strings, slices, arrays, and maps. Usages of `omitempty` on
   416  // Go bools, ints, uints floats, pointers, and interfaces should migrate to use
   417  // the `omitzero` tag option, which omits a field if it is the zero Go value.
   418  //
   419  // This only affects marshaling and is ignored when unmarshaling.
   420  // The v1 default is true.
   421  func OmitEmptyWithLegacyDefinition(v bool) Options {
   422  	if v {
   423  		return jsonflags.OmitEmptyWithLegacyDefinition | 1
   424  	} else {
   425  		return jsonflags.OmitEmptyWithLegacyDefinition | 0
   426  	}
   427  }
   428  
   429  // ReportErrorsWithLegacySemantics specifies that Marshal and Unmarshal
   430  // should report errors with legacy semantics:
   431  //
   432  //   - When marshaling or unmarshaling, the returned error values are
   433  //     usually of types such as [SyntaxError], [MarshalerError],
   434  //     [UnsupportedTypeError], [UnsupportedValueError],
   435  //     [InvalidUnmarshalError], or [UnmarshalTypeError].
   436  //     In contrast, the v2 semantic is to always return errors as either
   437  //     [jsonv2.SemanticError] or [jsontext.SyntacticError].
   438  //
   439  //   - When marshaling, if a user-defined marshal method reports an error,
   440  //     it is always wrapped in a [MarshalerError], even if the error itself
   441  //     is already a [MarshalerError], which may lead to multiple redundant
   442  //     layers of wrapping. In contrast, the v2 semantic is to
   443  //     always wrap an error within [jsonv2.SemanticError]
   444  //     unless it is already a semantic error.
   445  //
   446  //   - When unmarshaling, if a user-defined unmarshal method reports an error,
   447  //     it is never wrapped and reported verbatim. In contrast, the v2 semantic
   448  //     is to always wrap an error within [jsonv2.SemanticError]
   449  //     unless it is already a semantic error.
   450  //
   451  //   - When marshaling or unmarshaling, if a Go struct contains type errors
   452  //     (e.g., conflicting names or malformed field tags), then such errors
   453  //     are ignored and the Go struct uses a best-effort representation.
   454  //     In contrast, the v2 semantic is to report a runtime error.
   455  //
   456  //   - When unmarshaling, the syntactic structure of the JSON input
   457  //     is fully validated before performing the semantic unmarshaling
   458  //     of the JSON data into the Go value. Practically speaking,
   459  //     this means that JSON input with syntactic errors do not result
   460  //     in any mutations of the target Go value. In contrast, the v2 semantic
   461  //     is to perform a streaming decode and gradually unmarshal the JSON input
   462  //     into the target Go value, which means that the Go value may be
   463  //     partially mutated when a syntactic error is encountered.
   464  //
   465  //   - When unmarshaling, a semantic error does not immediately terminate the
   466  //     unmarshal procedure, but rather evaluation continues.
   467  //     When unmarshal returns, only the first semantic error is reported.
   468  //     In contrast, the v2 semantic is to terminate unmarshal the moment
   469  //     an error is encountered.
   470  //
   471  // This affects either marshaling or unmarshaling.
   472  // The v1 default is true.
   473  func ReportErrorsWithLegacySemantics(v bool) Options {
   474  	if v {
   475  		return jsonflags.ReportErrorsWithLegacySemantics | 1
   476  	} else {
   477  		return jsonflags.ReportErrorsWithLegacySemantics | 0
   478  	}
   479  }
   480  
   481  // StringifyWithLegacySemantics specifies that the `string` tag option
   482  // may stringify bools and string values. It only takes effect on fields
   483  // where the top-level type is a bool, string, numeric kind, or a pointer to
   484  // such a kind. Specifically, `string` will not stringify bool, string,
   485  // or numeric kinds within a composite data type
   486  // (e.g., array, slice, struct, map, or interface).
   487  //
   488  // When marshaling, such Go values are serialized as their usual
   489  // JSON representation, but quoted within a JSON string.
   490  // When unmarshaling, such Go values must be deserialized from
   491  // a JSON string containing their usual JSON representation.
   492  // A JSON null quoted in a JSON string is a valid substitute for JSON null
   493  // while unmarshaling into a Go value that `string` takes effect on.
   494  //
   495  // This affects either marshaling or unmarshaling.
   496  // The v1 default is true.
   497  func StringifyWithLegacySemantics(v bool) Options {
   498  	if v {
   499  		return jsonflags.StringifyWithLegacySemantics | 1
   500  	} else {
   501  		return jsonflags.StringifyWithLegacySemantics | 0
   502  	}
   503  }
   504  
   505  // UnmarshalArrayFromAnyLength specifies that Go arrays can be unmarshaled
   506  // from input JSON arrays of any length. If the JSON array is too short,
   507  // then the remaining Go array elements are zeroed. If the JSON array
   508  // is too long, then the excess JSON array elements are skipped over.
   509  //
   510  // This only affects unmarshaling and is ignored when marshaling.
   511  // The v1 default is true.
   512  func UnmarshalArrayFromAnyLength(v bool) Options {
   513  	if v {
   514  		return jsonflags.UnmarshalArrayFromAnyLength | 1
   515  	} else {
   516  		return jsonflags.UnmarshalArrayFromAnyLength | 0
   517  	}
   518  }
   519  
   520  // unmarshalAnyWithRawNumber specifies that unmarshaling a JSON number into
   521  // an empty Go interface should use the Number type instead of a float64.
   522  func unmarshalAnyWithRawNumber(v bool) Options {
   523  	if v {
   524  		return jsonflags.UnmarshalAnyWithRawNumber | 1
   525  	} else {
   526  		return jsonflags.UnmarshalAnyWithRawNumber | 0
   527  	}
   528  }
   529  

View as plain text