Source file src/encoding/json/v2/arshal_methods.go

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build goexperiment.jsonv2
     6  
     7  package json
     8  
     9  import (
    10  	"encoding"
    11  	"errors"
    12  	"io"
    13  	"reflect"
    14  
    15  	"encoding/json/internal"
    16  	"encoding/json/internal/jsonflags"
    17  	"encoding/json/internal/jsonopts"
    18  	"encoding/json/internal/jsonwire"
    19  	"encoding/json/jsontext"
    20  )
    21  
    22  var errNonStringValue = errors.New("JSON value must be string type")
    23  
    24  // Interfaces for custom serialization.
    25  var (
    26  	jsonMarshalerType       = reflect.TypeFor[Marshaler]()
    27  	jsonMarshalerToType     = reflect.TypeFor[MarshalerTo]()
    28  	jsonUnmarshalerType     = reflect.TypeFor[Unmarshaler]()
    29  	jsonUnmarshalerFromType = reflect.TypeFor[UnmarshalerFrom]()
    30  	textAppenderType        = reflect.TypeFor[encoding.TextAppender]()
    31  	textMarshalerType       = reflect.TypeFor[encoding.TextMarshaler]()
    32  	textUnmarshalerType     = reflect.TypeFor[encoding.TextUnmarshaler]()
    33  
    34  	allMarshalerTypes   = []reflect.Type{jsonMarshalerToType, jsonMarshalerType, textAppenderType, textMarshalerType}
    35  	allUnmarshalerTypes = []reflect.Type{jsonUnmarshalerFromType, jsonUnmarshalerType, textUnmarshalerType}
    36  	allMethodTypes      = append(allMarshalerTypes, allUnmarshalerTypes...)
    37  )
    38  
    39  // Marshaler is implemented by types that can marshal themselves.
    40  // It is recommended that types implement [MarshalerTo] unless the implementation
    41  // is trying to avoid directly depending on the "jsontext" package.
    42  //
    43  // Implementations should return a buffer that is safe
    44  // for the caller to retain and potentially mutate.
    45  //
    46  // Implementations must not return [errors.ErrUnsupported].
    47  //
    48  // If the returned error is a [SemanticError], then unpopulated fields
    49  // of the error may be populated by [json] with additional context.
    50  // Errors of other types are wrapped within a [SemanticError].
    51  //
    52  // Implementations should assume [Deterministic] is true and return
    53  // deterministic output.
    54  type Marshaler interface {
    55  	MarshalJSON() ([]byte, error)
    56  }
    57  
    58  // MarshalerTo is implemented by types that can marshal themselves.
    59  // It is recommended that types implement MarshalerTo instead of [Marshaler]
    60  // since it is both more performant and more flexible.
    61  // If a type implements both Marshaler and MarshalerTo,
    62  // then MarshalerTo takes precedence. In such a case, both implementations
    63  // should aim to have equivalent behavior for the default marshal options.
    64  //
    65  // The implementation must write only one JSON value to the Encoder.
    66  // Alternatively, it may return [errors.ErrUnsupported] without mutating
    67  // the Encoder. The "json" package calling the method will
    68  // use the next available JSON representation for the receiver type,
    69  // as described in [Marshal].
    70  // Implementations must not retain the pointer to [jsontext.Encoder].
    71  //
    72  // If the returned error is a [SemanticError], then unpopulated fields
    73  // of the error may be populated by [json] with additional context.
    74  // Errors of other types are wrapped within a [SemanticError],
    75  // except for IO errors.
    76  //
    77  // The MarshalJSONTo method should not be called directly as it may
    78  // return sentinel errors that need special handling.
    79  // Users should instead call [MarshalEncode], which handles such cases.
    80  //
    81  // Implementations should inspect the marshal options from
    82  // [jsontext.Encoder.Options] and adjust behavior to respect the options as
    83  // necessary.
    84  //
    85  // The following options may be relevant to MarshalerTo implementations:
    86  //
    87  // - [Deterministic]: if the implementation may produce non-deterministic output
    88  // - [StringifyNumbers]: if the type is represented as a JSON number
    89  //
    90  // Several options, such as [FormatNilSliceAsNull], apply only to native Go
    91  // types. Thus, these options are typically not directly relevant to
    92  // MarshalerTo implementations. However, types representing a composite type
    93  // should marshal contained types using [MarshalEncode] to ensure these options
    94  // apply to the contained types. Similarly, [WithMarshalers] may influence
    95  // marshaling of any contained type within a composite type.
    96  //
    97  // All other options are automatically handled outside of the MarshalerTo
    98  // implementation, and thus are not relevant to implementations.
    99  type MarshalerTo interface {
   100  	MarshalJSONTo(*jsontext.Encoder) error
   101  }
   102  
   103  // Unmarshaler is implemented by types that can unmarshal themselves.
   104  // It is recommended that types implement [UnmarshalerFrom] unless the implementation
   105  // is trying to avoid a direct dependency on the "jsontext" package.
   106  //
   107  // The input can be assumed to be a valid encoding of a JSON value
   108  // if called from unmarshal functionality in this package.
   109  // It is recommended that UnmarshalJSON implement merge semantics
   110  // when unmarshaling into a pre-populated value, as described in [Unmarshal].
   111  //
   112  // Implementations must not retain or mutate the input []byte.
   113  //
   114  // Implementations must not return [errors.ErrUnsupported].
   115  //
   116  // If the returned error is a [SemanticError], then unpopulated fields
   117  // of the error may be populated by [json] with additional context.
   118  // Errors of other types are wrapped within a [SemanticError].
   119  type Unmarshaler interface {
   120  	UnmarshalJSON([]byte) error
   121  }
   122  
   123  // UnmarshalerFrom is implemented by types that can unmarshal themselves.
   124  // It is recommended that types implement UnmarshalerFrom instead of [Unmarshaler]
   125  // since this is both more performant and more flexible.
   126  // If a type implements both Unmarshaler and UnmarshalerFrom,
   127  // then UnmarshalerFrom takes precedence. In such a case, both implementations
   128  // should aim to have equivalent behavior for the default unmarshal options.
   129  //
   130  // The implementation must read only one JSON value from the Decoder.
   131  // It is recommended that UnmarshalJSONFrom implement merge semantics when
   132  // unmarshaling into a pre-populated value, as described in [Unmarshal].
   133  // Alternatively, it may return [errors.ErrUnsupported] without mutating
   134  // the Decoder. The "json" package calling the method will
   135  // use the next available JSON representation for the receiver type.
   136  // Implementations must not retain the pointer to [jsontext.Decoder].
   137  //
   138  // If the returned error is a [SemanticError], then unpopulated fields
   139  // of the error may be populated by [json] with additional context.
   140  // Errors of other types are wrapped within a [SemanticError],
   141  // except for [jsontext.SyntacticError]s and IO errors.
   142  //
   143  // The UnmarshalJSONFrom method should not be called directly as it may
   144  // return sentinel errors that need special handling.
   145  // Users should instead call [UnmarshalDecode], which handles such cases.
   146  //
   147  // Implementations should inspect the unmarshal options from
   148  // [jsontext.Decoder.Options] and adjust behavior to respect the options as
   149  // necessary.
   150  //
   151  // The following options may be relevant to UnmarshalerFrom implementations:
   152  //
   153  // - [StringifyNumbers]: if the type is represented as a JSON number
   154  //
   155  // Several options, such as [FormatNilSliceAsNull], apply only to native Go
   156  // types. Thus, these options are typically not directly relevant to
   157  // UnmarshalerFrom implementations. However, types representing a composite
   158  // type should unmarshal contained types using [UnmarshalDecode] to ensure
   159  // these options apply to the contained types. Similarly, [WithUnmarshalers]
   160  // may influence unmarshaling of any contained type within a composite type.
   161  //
   162  // All other options are automatically handled outside of the UnmarshalerFrom
   163  // implementation, and thus are not relevant to implementations.
   164  type UnmarshalerFrom interface {
   165  	UnmarshalJSONFrom(*jsontext.Decoder) error
   166  }
   167  
   168  func makeMethodArshaler(fncs *arshaler, t reflect.Type) *arshaler {
   169  	// Avoid injecting method arshaler on the pointer or interface version
   170  	// to avoid ever calling the method on a nil pointer or interface receiver.
   171  	// Let it be injected on the value receiver (which is always addressable).
   172  	if t.Kind() == reflect.Pointer || t.Kind() == reflect.Interface {
   173  		return fncs
   174  	}
   175  
   176  	if needAddr, ok := implements(t, textMarshalerType); ok {
   177  		fncs.nonDefault = true
   178  		prevMarshal := fncs.marshal
   179  		fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
   180  			if mo.Flags.Get(jsonflags.CallMethodsWithLegacySemantics) &&
   181  				(needAddr && va.forcedAddr) {
   182  				return prevMarshal(enc, va, mo)
   183  			}
   184  			marshaler, _ := reflect.TypeAssert[encoding.TextMarshaler](va.Addr())
   185  			if err := export.Encoder(enc).AppendRaw('"', false, func(b []byte) ([]byte, error) {
   186  				b2, err := marshaler.MarshalText()
   187  				return append(b, b2...), err
   188  			}); err != nil {
   189  				err = wrapErrUnsupported(err, "MarshalText method")
   190  				if mo.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
   191  					return internal.NewMarshalerError(va.Addr().Interface(), err, "MarshalText") // unlike unmarshal, always wrapped
   192  				}
   193  				if !isSemanticError(err) && !export.IsIOError(err) {
   194  					err = newMarshalErrorBefore(enc, t, err)
   195  				}
   196  				return err
   197  			}
   198  			return nil
   199  		}
   200  	}
   201  
   202  	if needAddr, ok := implements(t, textAppenderType); ok {
   203  		fncs.nonDefault = true
   204  		prevMarshal := fncs.marshal
   205  		fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) (err error) {
   206  			if mo.Flags.Get(jsonflags.CallMethodsWithLegacySemantics) &&
   207  				(needAddr && va.forcedAddr) {
   208  				return prevMarshal(enc, va, mo)
   209  			}
   210  			appender, _ := reflect.TypeAssert[encoding.TextAppender](va.Addr())
   211  			if err := export.Encoder(enc).AppendRaw('"', false, appender.AppendText); err != nil {
   212  				err = wrapErrUnsupported(err, "AppendText method")
   213  				if mo.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
   214  					return internal.NewMarshalerError(va.Addr().Interface(), err, "AppendText") // unlike unmarshal, always wrapped
   215  				}
   216  				if !isSemanticError(err) && !export.IsIOError(err) {
   217  					err = newMarshalErrorBefore(enc, t, err)
   218  				}
   219  				return err
   220  			}
   221  			return nil
   222  		}
   223  	}
   224  
   225  	if needAddr, ok := implements(t, jsonMarshalerType); ok {
   226  		fncs.nonDefault = true
   227  		prevMarshal := fncs.marshal
   228  		fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
   229  			if mo.Flags.Get(jsonflags.CallMethodsWithLegacySemantics) &&
   230  				((needAddr && va.forcedAddr) || export.Encoder(enc).Tokens.Last.NeedObjectName()) {
   231  				return prevMarshal(enc, va, mo)
   232  			}
   233  			marshaler, _ := reflect.TypeAssert[Marshaler](va.Addr())
   234  			val, err := marshaler.MarshalJSON()
   235  			if err != nil {
   236  				err = wrapErrUnsupported(err, "MarshalJSON method")
   237  				if mo.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
   238  					return internal.NewMarshalerError(va.Addr().Interface(), err, "MarshalJSON") // unlike unmarshal, always wrapped
   239  				}
   240  				err = newMarshalErrorBefore(enc, t, err)
   241  				return collapseSemanticErrors(err)
   242  			}
   243  			if err := enc.WriteValue(val); err != nil {
   244  				if mo.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
   245  					return internal.NewMarshalerError(va.Addr().Interface(), err, "MarshalJSON") // unlike unmarshal, always wrapped
   246  				}
   247  				if isSyntacticError(err) {
   248  					err = newMarshalErrorBefore(enc, t, err)
   249  				}
   250  				return err
   251  			}
   252  			return nil
   253  		}
   254  	}
   255  
   256  	if needAddr, ok := implements(t, jsonMarshalerToType); ok {
   257  		fncs.nonDefault = true
   258  		prevMarshal := fncs.marshal
   259  		fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
   260  			if mo.Flags.Get(jsonflags.CallMethodsWithLegacySemantics) &&
   261  				((needAddr && va.forcedAddr) || export.Encoder(enc).Tokens.Last.NeedObjectName()) {
   262  				return prevMarshal(enc, va, mo)
   263  			}
   264  			xe := export.Encoder(enc)
   265  			prevDepth, prevLength := xe.Tokens.DepthLength()
   266  			xe.Flags.Set(jsonflags.WithinArshalCall | 1)
   267  			marshaler, _ := reflect.TypeAssert[MarshalerTo](va.Addr())
   268  			err := marshaler.MarshalJSONTo(enc)
   269  			xe.Flags.Set(jsonflags.WithinArshalCall | 0)
   270  			currDepth, currLength := xe.Tokens.DepthLength()
   271  			if (prevDepth != currDepth || prevLength+1 != currLength) && err == nil {
   272  				err = errNonSingularValue
   273  			}
   274  			if err != nil {
   275  				if errors.Is(err, errors.ErrUnsupported) {
   276  					if prevDepth == currDepth && prevLength == currLength {
   277  						return prevMarshal(enc, va, mo)
   278  					}
   279  					err = errUnsupportedMutation
   280  				}
   281  				if mo.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
   282  					return internal.NewMarshalerError(va.Addr().Interface(), err, "MarshalJSONTo") // unlike unmarshal, always wrapped
   283  				}
   284  				if !export.IsIOError(err) {
   285  					err = newSemanticErrorWithPosition(enc, t, prevDepth, prevLength, err)
   286  				}
   287  				return err
   288  			}
   289  			return nil
   290  		}
   291  	}
   292  
   293  	if _, ok := implements(t, textUnmarshalerType); ok {
   294  		fncs.nonDefault = true
   295  		fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
   296  			xd := export.Decoder(dec)
   297  			var flags jsonwire.ValueFlags
   298  			val, err := xd.ReadValue(&flags)
   299  			if err != nil {
   300  				return err // must be a syntactic or I/O error
   301  			}
   302  			if val.Kind() == 'n' {
   303  				if !uo.Flags.Get(jsonflags.MergeWithLegacySemantics) {
   304  					va.SetZero()
   305  				}
   306  				return nil
   307  			}
   308  			if val.Kind() != '"' {
   309  				return newUnmarshalErrorAfter(dec, t, errNonStringValue)
   310  			}
   311  			s := jsonwire.UnquoteMayCopy(val, flags.IsVerbatim())
   312  			unmarshaler, _ := reflect.TypeAssert[encoding.TextUnmarshaler](va.Addr())
   313  			if err := unmarshaler.UnmarshalText(s); err != nil {
   314  				err = wrapErrUnsupported(err, "UnmarshalText method")
   315  				if uo.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
   316  					return err // unlike marshal, never wrapped
   317  				}
   318  				if !isSemanticError(err) && !isSyntacticError(err) && !export.IsIOError(err) {
   319  					err = newUnmarshalErrorAfter(dec, t, err)
   320  				}
   321  				return err
   322  			}
   323  			return nil
   324  		}
   325  	}
   326  
   327  	if _, ok := implements(t, jsonUnmarshalerType); ok {
   328  		fncs.nonDefault = true
   329  		prevUnmarshal := fncs.unmarshal
   330  		fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
   331  			if uo.Flags.Get(jsonflags.CallMethodsWithLegacySemantics) &&
   332  				export.Decoder(dec).Tokens.Last.NeedObjectName() {
   333  				return prevUnmarshal(dec, va, uo)
   334  			}
   335  			val, err := dec.ReadValue()
   336  			if err != nil {
   337  				return err // must be a syntactic or I/O error
   338  			}
   339  			unmarshaler, _ := reflect.TypeAssert[Unmarshaler](va.Addr())
   340  			if err := unmarshaler.UnmarshalJSON(val); err != nil {
   341  				err = wrapErrUnsupported(err, "UnmarshalJSON method")
   342  				if uo.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
   343  					return err // unlike marshal, never wrapped
   344  				}
   345  				err = newUnmarshalErrorAfter(dec, t, err)
   346  				return collapseSemanticErrors(err)
   347  			}
   348  			return nil
   349  		}
   350  	}
   351  
   352  	if _, ok := implements(t, jsonUnmarshalerFromType); ok {
   353  		fncs.nonDefault = true
   354  		prevUnmarshal := fncs.unmarshal
   355  		fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
   356  			if uo.Flags.Get(jsonflags.CallMethodsWithLegacySemantics) &&
   357  				export.Decoder(dec).Tokens.Last.NeedObjectName() {
   358  				return prevUnmarshal(dec, va, uo)
   359  			}
   360  			xd := export.Decoder(dec)
   361  			prevDepth, prevLength := xd.Tokens.DepthLength()
   362  			if prevDepth == 1 && xd.AtEOF() {
   363  				return io.EOF // check EOF early to avoid fn reporting an EOF
   364  			}
   365  			xd.Flags.Set(jsonflags.WithinArshalCall | 1)
   366  			unmarshaler, _ := reflect.TypeAssert[UnmarshalerFrom](va.Addr())
   367  			err := unmarshaler.UnmarshalJSONFrom(dec)
   368  			xd.Flags.Set(jsonflags.WithinArshalCall | 0)
   369  			currDepth, currLength := xd.Tokens.DepthLength()
   370  			if (prevDepth != currDepth || prevLength+1 != currLength) && err == nil {
   371  				err = errNonSingularValue
   372  			}
   373  			if err != nil {
   374  				if errors.Is(err, errors.ErrUnsupported) {
   375  					if prevDepth == currDepth && prevLength == currLength {
   376  						return prevUnmarshal(dec, va, uo)
   377  					}
   378  					err = errUnsupportedMutation
   379  				}
   380  				if uo.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
   381  					if err2 := xd.SkipUntil(prevDepth, prevLength+1); err2 != nil {
   382  						return err2
   383  					}
   384  					return err // unlike marshal, never wrapped
   385  				}
   386  				if !isSyntacticError(err) && !export.IsIOError(err) {
   387  					err = newSemanticErrorWithPosition(dec, t, prevDepth, prevLength, err)
   388  				}
   389  				return err
   390  			}
   391  			return nil
   392  		}
   393  	}
   394  
   395  	return fncs
   396  }
   397  
   398  // implementsAny is like t.Implements(ifaceType) for a list of interfaces,
   399  // but checks whether either t or reflect.PointerTo(t) implements the interface.
   400  func implementsAny(t reflect.Type, ifaceTypes ...reflect.Type) bool {
   401  	for _, ifaceType := range ifaceTypes {
   402  		if _, ok := implements(t, ifaceType); ok {
   403  			return true
   404  		}
   405  	}
   406  	return false
   407  }
   408  
   409  // implements is like t.Implements(ifaceType) but checks whether
   410  // either t or reflect.PointerTo(t) implements the interface.
   411  // It also reports whether the value needs to be addressed
   412  // in order to satisfy the interface.
   413  func implements(t, ifaceType reflect.Type) (needAddr, ok bool) {
   414  	switch {
   415  	case t.Implements(ifaceType):
   416  		return false, true
   417  	case reflect.PointerTo(t).Implements(ifaceType):
   418  		return true, true
   419  	default:
   420  		return false, false
   421  	}
   422  }
   423  

View as plain text