Package json
Overview ▸
Index ▸
Variables
ErrUnknownName indicates that a JSON object member could not be unmarshaled because the name is not known to the target Go struct. This error is directly wrapped within a SemanticError when produced.
The name of an unknown JSON object member can be extracted as:
err := ...
serr, ok := errors.AsType[*json.SemanticError](err)
if ok && serr.Err == json.ErrUnknownName {
ptr := serr.JSONPointer // JSON pointer to unknown name
name := ptr.LastToken() // unknown name itself
...
}
This error is only returned if RejectUnknownMembers is true.
var ErrUnknownName = errors.New("unknown object member name")
func GetOption
func GetOption[T any](opts Options, setter func(T) Options) (T, bool)
GetOption returns the value stored in opts with the provided setter, reporting whether the value is present. If not present, the returned value is the zero value for type T.
Example usage:
v, ok := json.GetOption(opts, json.Deterministic)
Options are most commonly introspected to alter the JSON representation of MarshalerTo.MarshalJSONTo and UnmarshalerFrom.UnmarshalJSONFrom methods, and MarshalToFunc and UnmarshalFromFunc functions. In such cases, the presence bit should generally be ignored.
func Marshal 1.27
func Marshal(in any, opts ...Options) (out []byte, err error)
Marshal serializes a Go value as a []byte according to the provided marshal and encode options (while ignoring unmarshal or decode options). It does not terminate the output with a newline.
Type-specific marshal functions and methods take precedence over the default representation of a value. Functions or methods that operate on *T are only called when encoding a value of type T (by taking its address) or a non-nil value of *T. Marshal ensures that a value is always addressable (by copying the value if necessary) so that these functions and methods can be consistently called. For performance, it is recommended that Marshal be passed a non-nil pointer to the value.
The input value is encoded as JSON according to the following rules:
If any type-specific functions in a WithMarshalers option match the value type, then those functions are called to encode the value. If all applicable functions return errors.ErrUnsupported, then the value is encoded according to subsequent rules.
If the value type implements MarshalerTo, then the MarshalJSONTo method is called to encode the value. If the method returns errors.ErrUnsupported, then the input is encoded according to subsequent rules.
If the value type implements Marshaler, then the MarshalJSON method is called to encode the value.
If the value type implements encoding.TextAppender, then the AppendText method is called to encode the value and subsequently encode its result as a JSON string.
If the value type implements encoding.TextMarshaler, then the MarshalText method is called to encode the value and subsequently encode its result as a JSON string.
Otherwise, the value is encoded according to the value's type as described in detail below.
Most Go types have a default JSON representation as follows:
A Go boolean is encoded as a JSON boolean (e.g., true or false).
A Go string is encoded as a JSON string.
A Go []byte or [N]byte is encoded as a JSON string containing a binary value using Base 64 Encoding per RFC 4648, section 4.
A Go integer is encoded as a JSON number without fractions or exponents. If StringifyNumbers is specified or encoding a JSON object name, then the JSON number is encoded within a JSON string.
A Go float is encoded as a JSON number. If StringifyNumbers is specified or encoding a JSON object name, then the JSON number is encoded within a JSON string. Encoding a NaN or ±Inf results in a SemanticError.
A Go map is encoded as a JSON object, where each Go map key and value is recursively encoded as a name and value pair in the JSON object. The Go map key must encode as a JSON string, otherwise this results in a SemanticError. The Go map is traversed in a non-deterministic order. For deterministic encoding, consider using the Deterministic option. By default, a nil map is encoded as an empty JSON object, unless the FormatNilMapAsNull option is specified.
A Go struct is encoded as a JSON object. See the “JSON Representation of Go structs” section in the package-level documentation for more details.
A Go slice is encoded as a JSON array, where each Go slice element is recursively JSON-encoded as the elements of the JSON array. By default, a nil slice is encoded as an empty JSON array, unless the FormatNilSliceAsNull option is specified.
A Go array is encoded as a JSON array, where each Go array element is recursively JSON-encoded as the elements of the JSON array. The JSON array length is always identical to the Go array length.
A Go pointer is encoded as a JSON null if nil, otherwise it is the recursively JSON-encoded representation of the underlying value.
A Go interface is encoded as a JSON null if nil, otherwise it is the recursively JSON-encoded representation of the underlying value.
A Go time.Time is encoded as a JSON string containing the timestamp formatted in RFC 3339 with nanosecond precision.
A Go time.Duration currently has no default representation and results in a SemanticError, unless the encoding/json.FormatDurationAsNano option is specified, in which case it is encoded as a JSON number without fractions or exponents, representing the duration in nanoseconds.
All other Go types (e.g., complex numbers, channels, and functions) have no default representation and result in a SemanticError.
JSON cannot represent cyclic data structures and Marshal does not handle them.
▸ Example (Multiline)
func MarshalEncode 1.27
func MarshalEncode(out *jsontext.Encoder, in any, opts ...Options) (err error)
MarshalEncode serializes a Go value into an jsontext.Encoder according to the provided marshal or encode options (while ignoring unmarshal or decode options). The options provided take precedence over options already applied on the jsontext.Encoder and only apply for the duration of the marshal call.
See Marshal for details about the conversion of a Go value into JSON.
func MarshalWrite 1.27
func MarshalWrite(out io.Writer, in any, opts ...Options) (err error)
MarshalWrite serializes a Go value into an io.Writer according to the provided marshal and encode options (while ignoring unmarshal or decode options). It does not terminate the output with a newline. See Marshal for details about the conversion of a Go value into JSON.
func Unmarshal 1.27
func Unmarshal(in []byte, out any, opts ...Options) (err error)
Unmarshal decodes a []byte input into a Go value according to the provided unmarshal and decode options (while ignoring marshal or encode options). The input must be a single JSON value with optional whitespace interspersed. The output must be a non-nil pointer.
Type-specific unmarshal functions and methods take precedence over the default representation of a value. Functions or methods that operate on *T are only called when decoding a value of type T (by taking its address) or a non-nil value of *T. Unmarshal ensures that a value is always addressable (by copying the value if necessary) so that these functions and methods can be consistently called. If a value must be shallow copied to call a pointer-receiver Unmarshaler, UnmarshalerFrom, or encoding.TextUnmarshaler method, then any mutations performed by the method are shallow copied back into the destination value.
The input is decoded into the output according to the following rules:
If any type-specific functions in a WithUnmarshalers option match the value type, then those functions are called to decode the JSON value. If all applicable functions return errors.ErrUnsupported, then the input is decoded according to subsequent rules.
If the value type implements UnmarshalerFrom, then the UnmarshalJSONFrom method is called to decode the JSON value. If the method returns errors.ErrUnsupported, then the input is decoded according to subsequent rules.
If the value type implements Unmarshaler, then the UnmarshalJSON method is called to decode the JSON value.
If the value type implements encoding.TextUnmarshaler, then the input is decoded as a JSON string and the UnmarshalText method is called with the decoded string value. This fails with a SemanticError if the input is not a JSON string.
Otherwise, the JSON value is decoded according to the value's type as described in detail below.
Most Go types have a default JSON representation. A JSON null may be decoded into every supported Go value where it is equivalent to storing the zero value of the Go value. If the input JSON kind is not handled by the current Go value type, then this fails with a SemanticError. Unless otherwise specified, the decoded value replaces any pre-existing value.
The representation of each type is as follows:
A Go boolean is decoded from a JSON boolean (e.g., true or false).
A Go string is decoded from a JSON string.
A Go []byte or [N]byte is decoded from a JSON string containing a binary value using Base 64 Encoding per RFC 4648, section 4. When decoding into a non-nil []byte, the slice length is reset to zero and the decoded input is appended to it. When decoding into a [N]byte, the input must decode to exactly N bytes, otherwise it fails with a SemanticError.
A Go integer is decoded from a JSON number. It must be decoded from a JSON string containing a JSON number if StringifyNumbers is specified or decoding a JSON object name. It fails with a SemanticError if the JSON number has a fractional or exponent component. It also fails if it overflows the representation of the Go integer type.
A Go float is decoded from a JSON number. It must be decoded from a JSON string containing a JSON number if StringifyNumbers is specified or decoding a JSON object name. It fails if it overflows the representation of the Go float type. Since JSON lacks a native representation for a NaN or ±Inf, such values cannot be the result of decoding.
A Go map is decoded from a JSON object, where each JSON object name and value pair is recursively decoded as the Go map key and value. Maps are not cleared. If the Go map is nil, then a new map is allocated to decode into. If the decoded key matches an existing Go map entry, the entry value is reused by decoding the JSON object value into it.
A Go struct is decoded from a JSON object. See the “JSON Representation of Go structs” section in the package-level documentation for more details.
A Go slice is decoded from a JSON array, where each JSON element is recursively decoded and appended to the Go slice. Before appending into a Go slice, a new slice is allocated if it is nil, otherwise the slice length is reset to zero.
A Go array is decoded from a JSON array, where each JSON array element is recursively decoded as each corresponding Go array element. Each Go array element is zeroed before decoding into it. It fails with a SemanticError if the JSON array does not contain the exact same number of elements as the Go array.
A Go pointer is decoded based on the JSON kind and underlying Go type. If the input is a JSON null, then this stores a nil pointer. Otherwise, it allocates a new underlying value if the pointer is nil, and recursively JSON decodes into the underlying value.
A Go interface is decoded based on the JSON kind and underlying Go type. If the input is a JSON null, then this stores a nil interface value. Otherwise, a nil interface value of an empty interface type is initialized with a zero Go bool, string, float64, map[string]any, or []any if the input is a JSON boolean, string, number, object, or array, respectively. If the interface value is still nil, then this fails with a SemanticError since decoding could not determine an appropriate Go type to decode into. For example, unmarshaling into a nil io.Reader fails since there is no concrete type to populate the interface value with. Otherwise an underlying value exists and it recursively decodes the JSON input into it.
A Go time.Time is decoded from a JSON string containing the time formatted in RFC 3339 with nanosecond precision.
A Go time.Duration currently has no default representation and results in a SemanticError, unless the encoding/json.FormatDurationAsNano option is specified, in which case it is decoded as a JSON number without fractions or exponents, representing the duration in nanoseconds.
All other Go types (e.g., complex numbers, channels, and functions) have no default representation and result in a SemanticError.
In general, unmarshaling follows merge semantics (similar to RFC 7396) where the decoded Go value replaces the destination value for any JSON kind other than an object. For JSON objects, the input object is merged into the destination value where matching object members recursively apply merge semantics.
func UnmarshalDecode 1.27
func UnmarshalDecode(in *jsontext.Decoder, out any, opts ...Options) (err error)
UnmarshalDecode deserializes a Go value from a jsontext.Decoder according to the provided unmarshal or decode options (while ignoring marshal or encode options). The options provided take precedence over options already applied on the jsontext.Decoder and only apply for the duration of the unmarshal call.
The input may be a stream of zero or more JSON values. UnmarshalDecode unmarshals only the next JSON value in the stream. If there are no more top-level JSON values, it reports io.EOF. The output must be a non-nil pointer. See Unmarshal for details about the conversion of JSON into a Go value.
▸ Example (Stream)
func UnmarshalRead 1.27
func UnmarshalRead(in io.Reader, out any, opts ...Options) (err error)
UnmarshalRead deserializes a Go value from an io.Reader according to the provided unmarshal and decode options (while ignoring marshal or encode options). The input must be a single JSON value with optional whitespace interspersed. It consumes the entirety of io.Reader until io.EOF is encountered, without reporting an error for EOF. The output must be a non-nil pointer. See Unmarshal for details about the conversion of JSON into a Go value.
type Marshaler 1.27
Marshaler is implemented by types that can marshal themselves. It is recommended that types implement MarshalerTo unless the implementation is trying to avoid directly depending on the "jsontext" package.
Implementations should return a buffer that is safe for the caller to retain and potentially mutate.
Implementations must not return errors.ErrUnsupported.
If the returned error is a SemanticError, then unpopulated fields of the error may be populated by json with additional context. Errors of other types are wrapped within a SemanticError.
Implementations should assume Deterministic is true and return deterministic output.
type Marshaler interface {
MarshalJSON() ([]byte, error)
}
type MarshalerTo 1.27
MarshalerTo is implemented by types that can marshal themselves. It is recommended that types implement MarshalerTo instead of Marshaler since it is both more performant and more flexible. If a type implements both Marshaler and MarshalerTo, then MarshalerTo takes precedence. In such a case, both implementations should aim to have equivalent behavior for the default marshal options.
The implementation must write only one JSON value to the Encoder. Alternatively, it may return errors.ErrUnsupported without mutating the Encoder. The "json" package calling the method will use the next available JSON representation for the receiver type, as described in Marshal. Implementations must not retain the pointer to jsontext.Encoder.
If the returned error is a SemanticError, then unpopulated fields of the error may be populated by json with additional context. Errors of other types are wrapped within a SemanticError, except for IO errors.
The MarshalJSONTo method should not be called directly as it may return sentinel errors that need special handling. Users should instead call MarshalEncode, which handles such cases.
Implementations should inspect the marshal options from jsontext.Encoder.Options and adjust behavior to respect the options as necessary.
The following options may be relevant to MarshalerTo implementations:
- Deterministic: if the implementation may produce non-deterministic output - StringifyNumbers: if the type is represented as a JSON number
Several options, such as FormatNilSliceAsNull, apply only to native Go types. Thus, these options are typically not directly relevant to MarshalerTo implementations. However, types representing a composite type should marshal contained types using MarshalEncode to ensure these options apply to the contained types. Similarly, WithMarshalers may influence marshaling of any contained type within a composite type.
All other options are automatically handled outside of the MarshalerTo implementation, and thus are not relevant to implementations.
type MarshalerTo interface {
MarshalJSONTo(*jsontext.Encoder) error
}
▸ Example
type Marshalers 1.27
Marshalers is a list of functions that may override the marshal behavior of specific types. Populate WithMarshalers to use it with Marshal, MarshalWrite, or MarshalEncode. A nil *Marshalers is equivalent to an empty list. There are no exported fields or methods on Marshalers.
type Marshalers = typedMarshalers
func JoinMarshalers 1.27
func JoinMarshalers(ms ...*Marshalers) *Marshalers
JoinMarshalers constructs a flattened list of marshal functions. If multiple functions in the list are applicable for a value of a given type, then those earlier in the list take precedence over those that come later. If a function returns errors.ErrUnsupported, then the next applicable function is called, otherwise the default marshaling behavior is used.
For example:
m1 := JoinMarshalers(f1, f2) m2 := JoinMarshalers(f0, m1, f3) // equivalent to m3 m3 := JoinMarshalers(f0, f1, f2, f3) // equivalent to m2
func MarshalFunc
func MarshalFunc[T any](fn func(T) ([]byte, error)) *Marshalers
MarshalFunc constructs a type-specific marshaler that specifies how to marshal values of type T. T can be any type except a named pointer. The function is always provided with a non-nil pointer value if T is an interface or pointer type.
Implementations must follow the requirements of Marshaler.
Implementations must not retain the value of T.
func MarshalToFunc
func MarshalToFunc[T any](fn func(*jsontext.Encoder, T) error) *Marshalers
MarshalToFunc constructs a type-specific marshaler that specifies how to marshal values of type T. T can be any type except a named pointer. The function is always provided with a non-nil pointer value if T is an interface or pointer type.
Implementations must follow the requirements of MarshalerTo.
Implementations must not retain the pointer to jsontext.Encoder or the value of T.
type Options 1.27
Options configure Marshal, MarshalWrite, MarshalEncode, Unmarshal, UnmarshalRead, and UnmarshalDecode with specific features. Each function takes in a variadic list of options, where properties set in later options override the value of previously set properties.
The Options type is identical to encoding/json.Options and encoding/json/jsontext.Options. Options from the other packages can be used interchangeably with functionality in this package.
An Options value represents either a single option or a set of options. It can be thought of as a Go map of option properties (even though the underlying implementation avoids Go maps for performance).
The constructors (e.g., Deterministic) return a value for a single option:
opt := Deterministic(true)
which is analogous to creating a single entry map:
opt := Options{"Deterministic": true}
JoinOptions composes multiple options values together:
out := JoinOptions(opts...)
which is analogous to making a new map and copying the options over:
out := make(Options)
for _, m := range opts {
for k, v := range m {
out[k] = v
}
}
GetOption looks up the value of an options parameter:
v, ok := GetOption(opts, Deterministic)
which is analogous to a Go map lookup:
v, ok := Options["Deterministic"]
There is a single Options type, which is used with both marshal and unmarshal. Some options affect both operations, while others only affect one operation:
- StringifyNumbers affects marshaling and unmarshaling
- Deterministic affects marshaling only
- FormatNilSliceAsNull affects marshaling only
- FormatNilMapAsNull affects marshaling only
- OmitZeroStructFields affects marshaling only
- MatchCaseInsensitiveNames affects marshaling and unmarshaling
- RejectUnknownMembers affects unmarshaling only
- WithMarshalers affects marshaling only
- WithUnmarshalers affects unmarshaling only
Options that do not affect a particular operation are ignored.
type Options = jsonopts.Options
func DefaultOptionsV2 1.27
func DefaultOptionsV2() Options
DefaultOptionsV2 is the full set of all options that define v2 semantics. It is equivalent to the set of options in encoding/json.DefaultOptionsV1 all being set to false. All other options are not present.
func Deterministic 1.27
func Deterministic(v bool) Options
Deterministic specifies that marshaling the same input value will always serialize as the same output bytes.
For example, Go maps are marshaled sorted by key.
For native Go types, Determinism is guaranteed across different instances of identical binaries, but not across different builds of a program (such as different source or toolchain version, different GOOS/GOARCH, different build flags).
A Go type with a custom marshaler should also respect the Deterministic option and serialize deterministically if it is true.
This only affects marshaling and is ignored when unmarshaling.
func FormatNilMapAsNull 1.27
func FormatNilMapAsNull(v bool) Options
FormatNilMapAsNull specifies that a nil Go map should marshal as a JSON null instead of the default representation as an empty JSON object.
This only affects marshaling and is ignored when unmarshaling.
func FormatNilSliceAsNull 1.27
func FormatNilSliceAsNull(v bool) Options
FormatNilSliceAsNull specifies that a nil Go slice should marshal as a JSON null instead of the default representation as an empty JSON array (or an empty JSON string in the case of ~[]byte).
This only affects marshaling and is ignored when unmarshaling.
func JoinOptions 1.27
func JoinOptions(srcs ...Options) Options
JoinOptions coalesces the provided list of options into a single Options. Properties set in later options override the value of previously set properties.
func MatchCaseInsensitiveNames 1.27
func MatchCaseInsensitiveNames(v bool) Options
MatchCaseInsensitiveNames specifies that JSON object members are matched against Go struct fields using a case-insensitive match of the name. If a name matches multiple fields, the field whose name matches exactly is chosen. If there is none, an error is reported. Go struct fields explicitly marked with `case:strict` or `case:ignore` always use case-sensitive (or case-insensitive) name matching, regardless of the value of this option.
This affects either marshaling or unmarshaling.
Matching names case-insensitively also affects duplicate name detection (assuming jsontext.AllowDuplicateNames is false) since variations of the same name may match the same Go struct field. For example, when unmarshaling, the names "foo" and "Foo" may both match the same Go struct field and therefore be considered a duplicate name. When marshaling, normally it is impossible for any two Go struct fields to serialize in a way where they unmarshal into the same Go struct field since they all have unique exact names. However, it is possible for an embedded fallback to contain a name that also matches the name for a Go struct field, resulting in a duplicate name error.
func OmitZeroStructFields 1.27
func OmitZeroStructFields(v bool) Options
OmitZeroStructFields specifies that zero-valued fields of Go struct should be omitted from the marshaled output. A value is considered zero if its type has an "IsZero() bool" method that returns true, or if it lacks such a method and the value is a Go zero value. This option is equivalent to specifying the `omitzero` tag option on every field in a Go struct.
This only affects marshaling and is ignored when unmarshaling.
func RejectUnknownMembers 1.27
func RejectUnknownMembers(v bool) Options
RejectUnknownMembers specifies that unknown members should be rejected when unmarshaling a JSON object.
This only affects unmarshaling and is ignored when marshaling.
func StringifyNumbers 1.27
func StringifyNumbers(v bool) Options
StringifyNumbers specifies that types that would normally be encoded as a JSON number instead be encoded as a JSON string containing the equivalent JSON number value. When unmarshaling, the value is parsed from a JSON string containing the JSON number without any surrounding whitespace.
Specifying the `string` tag option on a Go struct field applies this option to the top-level JSON value for that field. When applied via the `string` tag option, StringifyNumbers option does not recursively apply to nested JSON numbers within a JSON object or array.
Like all options, explicitly specifying this option in a call to Marshal, Unmarshal, etc, will apply recursively.
A Go type with custom marshal/unmarshal that represents a JSON number should respect the StringifyNumbers option and if specified serialize as a JSON number within a JSON string. Custom marshal/unmarshal should handle nested JSON objects using MarshalEncode/UnmarshalDecode, which will automatically apply the non-recursive `string` tag option behavior.
According to RFC 8259, section 6, a JSON implementation may choose to limit the representation of a JSON number to an IEEE 754 binary64 value. This may cause decoders to lose precision for int64 and uint64 types. Quoting JSON numbers as a JSON string preserves the exact precision.
This affects either marshaling or unmarshaling.
func WithMarshalers 1.27
func WithMarshalers(v *Marshalers) Options
WithMarshalers specifies a list of type-specific marshalers to use, which can be used to override the default marshal behavior for values of particular types.
This only affects marshaling and is ignored when unmarshaling.
▸ Example (Errors)
func WithUnmarshalers 1.27
func WithUnmarshalers(v *Unmarshalers) Options
WithUnmarshalers specifies a list of type-specific unmarshalers to use, which can be used to override the default unmarshal behavior for values of particular types.
This only affects unmarshaling and is ignored when marshaling.
▸ Example (RawNumber)
▸ Example (RecordOffsets)
type SemanticError 1.27
SemanticError describes an error determining the meaning of JSON data as Go data, or vice versa.
If a Marshaler, MarshalerTo, Unmarshaler, or UnmarshalerFrom method returns a SemanticError when called by the json package, then the ByteOffset, JSONPointer, and GoType fields are automatically populated by the calling context if they are the zero value.
The contents of this error as produced by this package may change over time.
type SemanticError struct {
// ByteOffset indicates that an error occurred at or after this byte offset.
ByteOffset int64
// JSONPointer indicates that an error occurred within this JSON value
// as indicated using the JSON Pointer notation (see RFC 6901).
JSONPointer jsontext.Pointer
// JSONKind is the JSON kind that could not be handled.
JSONKind jsontext.Kind // may be zero if unknown
// JSONValue is the JSON number or string that could not be unmarshaled.
// It is not populated during marshaling.
JSONValue jsontext.Value // may be nil if irrelevant or unknown
// GoType is the Go type that could not be handled.
GoType reflect.Type // may be nil if unknown
// Err is the underlying error.
Err error // may be nil
// contains filtered or unexported fields
}
func (*SemanticError) Error 1.27
func (e *SemanticError) Error() string
func (*SemanticError) Unwrap 1.27
func (e *SemanticError) Unwrap() error
type Unmarshaler 1.27
Unmarshaler is implemented by types that can unmarshal themselves. It is recommended that types implement UnmarshalerFrom unless the implementation is trying to avoid a direct dependency on the "jsontext" package.
The input can be assumed to be a valid encoding of a JSON value if called from unmarshal functionality in this package. It is recommended that UnmarshalJSON implement merge semantics when unmarshaling into a pre-populated value, as described in Unmarshal.
Implementations must not retain or mutate the input []byte.
Implementations must not return errors.ErrUnsupported.
If the returned error is a SemanticError, then unpopulated fields of the error may be populated by json with additional context. Errors of other types are wrapped within a SemanticError.
type Unmarshaler interface {
UnmarshalJSON([]byte) error
}
type UnmarshalerFrom 1.27
UnmarshalerFrom is implemented by types that can unmarshal themselves. It is recommended that types implement UnmarshalerFrom instead of Unmarshaler since this is both more performant and more flexible. If a type implements both Unmarshaler and UnmarshalerFrom, then UnmarshalerFrom takes precedence. In such a case, both implementations should aim to have equivalent behavior for the default unmarshal options.
The implementation must read only one JSON value from the Decoder. It is recommended that UnmarshalJSONFrom implement merge semantics when unmarshaling into a pre-populated value, as described in Unmarshal. Alternatively, it may return errors.ErrUnsupported without mutating the Decoder. The "json" package calling the method will use the next available JSON representation for the receiver type. Implementations must not retain the pointer to jsontext.Decoder.
If the returned error is a SemanticError, then unpopulated fields of the error may be populated by json with additional context. Errors of other types are wrapped within a SemanticError, except for [jsontext.SyntacticError]s and IO errors.
The UnmarshalJSONFrom method should not be called directly as it may return sentinel errors that need special handling. Users should instead call UnmarshalDecode, which handles such cases.
Implementations should inspect the unmarshal options from jsontext.Decoder.Options and adjust behavior to respect the options as necessary.
The following options may be relevant to UnmarshalerFrom implementations:
- StringifyNumbers: if the type is represented as a JSON number
Several options, such as FormatNilSliceAsNull, apply only to native Go types. Thus, these options are typically not directly relevant to UnmarshalerFrom implementations. However, types representing a composite type should unmarshal contained types using UnmarshalDecode to ensure these options apply to the contained types. Similarly, WithUnmarshalers may influence unmarshaling of any contained type within a composite type.
All other options are automatically handled outside of the UnmarshalerFrom implementation, and thus are not relevant to implementations.
type UnmarshalerFrom interface {
UnmarshalJSONFrom(*jsontext.Decoder) error
}
▸ Example
type Unmarshalers 1.27
Unmarshalers is a list of functions that may override the unmarshal behavior of specific types. Populate WithUnmarshalers to use it with Unmarshal, UnmarshalRead, or UnmarshalDecode. A nil *Unmarshalers is equivalent to an empty list. There are no exported fields or methods on Unmarshalers.
type Unmarshalers = typedUnmarshalers
func JoinUnmarshalers 1.27
func JoinUnmarshalers(us ...*Unmarshalers) *Unmarshalers
JoinUnmarshalers constructs a flattened list of unmarshal functions. If multiple functions in the list are applicable for a value of a given type, then those earlier in the list take precedence over those that come later. If a function returns errors.ErrUnsupported, then the next applicable function is called, otherwise the default unmarshaling behavior is used.
For example:
u1 := JoinUnmarshalers(f1, f2) u2 := JoinUnmarshalers(f0, u1, f3) // equivalent to u3 u3 := JoinUnmarshalers(f0, f1, f2, f3) // equivalent to u2
func UnmarshalFromFunc
func UnmarshalFromFunc[T any](fn func(*jsontext.Decoder, T) error) *Unmarshalers
UnmarshalFromFunc constructs a type-specific unmarshaler that specifies how to unmarshal values of type T. T must be an unnamed pointer or an interface type. The function is always provided with a non-nil pointer value.
Implementations must follow the requirements of UnmarshalerFrom.
Implementations must not retain the pointer to jsontext.Decoder or the value of T.
func UnmarshalFunc
func UnmarshalFunc[T any](fn func([]byte, T) error) *Unmarshalers
UnmarshalFunc constructs a type-specific unmarshaler that specifies how to unmarshal values of type T. T must be an unnamed pointer or an interface type. The function is always provided with a non-nil pointer value.
Implementations must follow the requirements of Unmarshaler.
Implementations must not retain the value of T.