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  					(export.Encoder(enc).Tokens.Last.NeedObjectName()) && t.Kind() == reflect.String) {
   183  				// Do not call MarshalText on unaddressable values and map keys of string kind.
   184  				return prevMarshal(enc, va, mo)
   185  			}
   186  			marshaler, _ := reflect.TypeAssert[encoding.TextMarshaler](va.Addr())
   187  			if err := export.Encoder(enc).AppendRaw('"', false, func(b []byte) ([]byte, error) {
   188  				b2, err := marshaler.MarshalText()
   189  				return append(b, b2...), err
   190  			}); err != nil {
   191  				err = wrapErrUnsupported(err, "MarshalText method")
   192  				if mo.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
   193  					return internal.NewMarshalerError(va.Addr().Interface(), err, "MarshalText") // unlike unmarshal, always wrapped
   194  				}
   195  				if !isSemanticError(err) && !export.IsIOError(err) {
   196  					err = newMarshalErrorBefore(enc, t, err)
   197  				}
   198  				return err
   199  			}
   200  			return nil
   201  		}
   202  	}
   203  
   204  	if needAddr, ok := implements(t, textAppenderType); ok {
   205  		fncs.nonDefault = true
   206  		prevMarshal := fncs.marshal
   207  		fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) (err error) {
   208  			if mo.Flags.Get(jsonflags.CallMethodsWithLegacySemantics) &&
   209  				((needAddr && va.forcedAddr) ||
   210  					(export.Encoder(enc).Tokens.Last.NeedObjectName()) && t.Kind() == reflect.String) {
   211  				// Do not call AppendText on unaddressable values and map keys of string kind.
   212  				return prevMarshal(enc, va, mo)
   213  			}
   214  			appender, _ := reflect.TypeAssert[encoding.TextAppender](va.Addr())
   215  			if err := export.Encoder(enc).AppendRaw('"', false, appender.AppendText); err != nil {
   216  				err = wrapErrUnsupported(err, "AppendText method")
   217  				if mo.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
   218  					return internal.NewMarshalerError(va.Addr().Interface(), err, "AppendText") // unlike unmarshal, always wrapped
   219  				}
   220  				if !isSemanticError(err) && !export.IsIOError(err) {
   221  					err = newMarshalErrorBefore(enc, t, err)
   222  				}
   223  				return err
   224  			}
   225  			return nil
   226  		}
   227  	}
   228  
   229  	if needAddr, ok := implements(t, jsonMarshalerType); ok {
   230  		fncs.nonDefault = true
   231  		prevMarshal := fncs.marshal
   232  		fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
   233  			if mo.Flags.Get(jsonflags.CallMethodsWithLegacySemantics) &&
   234  				((needAddr && va.forcedAddr) || export.Encoder(enc).Tokens.Last.NeedObjectName()) {
   235  				// Do not call MarshalJSON on unaddressable values and map keys.
   236  				return prevMarshal(enc, va, mo)
   237  			}
   238  			marshaler, _ := reflect.TypeAssert[Marshaler](va.Addr())
   239  			val, err := marshaler.MarshalJSON()
   240  			if err != nil {
   241  				err = wrapErrUnsupported(err, "MarshalJSON method")
   242  				if mo.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
   243  					return internal.NewMarshalerError(va.Addr().Interface(), err, "MarshalJSON") // unlike unmarshal, always wrapped
   244  				}
   245  				err = newMarshalErrorBefore(enc, t, err)
   246  				return collapseSemanticErrors(err)
   247  			}
   248  			if err := enc.WriteValue(val); err != nil {
   249  				if mo.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
   250  					return internal.NewMarshalerError(va.Addr().Interface(), err, "MarshalJSON") // unlike unmarshal, always wrapped
   251  				}
   252  				if isSyntacticError(err) {
   253  					err = newMarshalErrorBefore(enc, t, err)
   254  				}
   255  				return err
   256  			}
   257  			return nil
   258  		}
   259  	}
   260  
   261  	if needAddr, ok := implements(t, jsonMarshalerToType); ok {
   262  		fncs.nonDefault = true
   263  		prevMarshal := fncs.marshal
   264  		fncs.marshal = func(enc *jsontext.Encoder, va addressableValue, mo *jsonopts.Struct) error {
   265  			if mo.Flags.Get(jsonflags.CallMethodsWithLegacySemantics) &&
   266  				((needAddr && va.forcedAddr) || export.Encoder(enc).Tokens.Last.NeedObjectName()) {
   267  				// Do not call MarshalJSONTo on unaddressable values and map keys.
   268  				return prevMarshal(enc, va, mo)
   269  			}
   270  			xe := export.Encoder(enc)
   271  			prevDepth, prevLength := xe.Tokens.DepthLength()
   272  			xe.Flags.Set(jsonflags.WithinArshalCall | 1)
   273  			marshaler, _ := reflect.TypeAssert[MarshalerTo](va.Addr())
   274  			err := marshaler.MarshalJSONTo(enc)
   275  			xe.Flags.Set(jsonflags.WithinArshalCall | 0)
   276  			currDepth, currLength := xe.Tokens.DepthLength()
   277  			if (prevDepth != currDepth || prevLength+1 != currLength) && err == nil {
   278  				err = errNonSingularValue
   279  			}
   280  			if err != nil {
   281  				if errors.Is(err, errors.ErrUnsupported) {
   282  					if prevDepth == currDepth && prevLength == currLength {
   283  						return prevMarshal(enc, va, mo)
   284  					}
   285  					err = errUnsupportedMutation
   286  				}
   287  				if mo.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
   288  					return internal.NewMarshalerError(va.Addr().Interface(), err, "MarshalJSONTo") // unlike unmarshal, always wrapped
   289  				}
   290  				if !export.IsIOError(err) {
   291  					err = newSemanticErrorWithPosition(enc, t, prevDepth, prevLength, err)
   292  				}
   293  				return err
   294  			}
   295  			return nil
   296  		}
   297  	}
   298  
   299  	if _, ok := implements(t, textUnmarshalerType); ok {
   300  		fncs.nonDefault = true
   301  		fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
   302  			xd := export.Decoder(dec)
   303  			var flags jsonwire.ValueFlags
   304  			val, err := xd.ReadValue(&flags)
   305  			if err != nil {
   306  				return err // must be a syntactic or I/O error
   307  			}
   308  			if val.Kind() == 'n' {
   309  				if !uo.Flags.Get(jsonflags.MergeWithLegacySemantics) {
   310  					va.SetZero()
   311  				}
   312  				return nil
   313  			}
   314  			if val.Kind() != '"' {
   315  				return newUnmarshalErrorAfter(dec, t, errNonStringValue)
   316  			}
   317  			s := jsonwire.UnquoteMayCopy(val, flags.IsVerbatim())
   318  			unmarshaler, _ := reflect.TypeAssert[encoding.TextUnmarshaler](va.Addr())
   319  			if err := unmarshaler.UnmarshalText(s); err != nil {
   320  				err = wrapErrUnsupported(err, "UnmarshalText method")
   321  				if uo.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
   322  					return err // unlike marshal, never wrapped
   323  				}
   324  				if !isSemanticError(err) && !isSyntacticError(err) && !export.IsIOError(err) {
   325  					err = newUnmarshalErrorAfter(dec, t, err)
   326  				}
   327  				return err
   328  			}
   329  			return nil
   330  		}
   331  	}
   332  
   333  	if _, ok := implements(t, jsonUnmarshalerType); ok {
   334  		fncs.nonDefault = true
   335  		prevUnmarshal := fncs.unmarshal
   336  		fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
   337  			if uo.Flags.Get(jsonflags.CallMethodsWithLegacySemantics) &&
   338  				export.Decoder(dec).Tokens.Last.NeedObjectName() {
   339  				// Do not call UnmarshalJSON on map keys.
   340  				return prevUnmarshal(dec, va, uo)
   341  			}
   342  			val, err := dec.ReadValue()
   343  			if err != nil {
   344  				return err // must be a syntactic or I/O error
   345  			}
   346  			unmarshaler, _ := reflect.TypeAssert[Unmarshaler](va.Addr())
   347  			if err := unmarshaler.UnmarshalJSON(val); err != nil {
   348  				err = wrapErrUnsupported(err, "UnmarshalJSON method")
   349  				if uo.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
   350  					return err // unlike marshal, never wrapped
   351  				}
   352  				err = newUnmarshalErrorAfter(dec, t, err)
   353  				return collapseSemanticErrors(err)
   354  			}
   355  			return nil
   356  		}
   357  	}
   358  
   359  	if _, ok := implements(t, jsonUnmarshalerFromType); ok {
   360  		fncs.nonDefault = true
   361  		prevUnmarshal := fncs.unmarshal
   362  		fncs.unmarshal = func(dec *jsontext.Decoder, va addressableValue, uo *jsonopts.Struct) error {
   363  			if uo.Flags.Get(jsonflags.CallMethodsWithLegacySemantics) &&
   364  				export.Decoder(dec).Tokens.Last.NeedObjectName() {
   365  				// Do not call UnmarshalJSONFrom on map keys.
   366  				return prevUnmarshal(dec, va, uo)
   367  			}
   368  			xd := export.Decoder(dec)
   369  			prevDepth, prevLength := xd.Tokens.DepthLength()
   370  			if prevDepth == 1 && xd.AtEOF() {
   371  				return io.EOF // check EOF early to avoid fn reporting an EOF
   372  			}
   373  			xd.Flags.Set(jsonflags.WithinArshalCall | 1)
   374  			unmarshaler, _ := reflect.TypeAssert[UnmarshalerFrom](va.Addr())
   375  			err := unmarshaler.UnmarshalJSONFrom(dec)
   376  			xd.Flags.Set(jsonflags.WithinArshalCall | 0)
   377  			currDepth, currLength := xd.Tokens.DepthLength()
   378  			if (prevDepth != currDepth || prevLength+1 != currLength) && err == nil {
   379  				err = errNonSingularValue
   380  			}
   381  			if err != nil {
   382  				if errors.Is(err, errors.ErrUnsupported) {
   383  					if prevDepth == currDepth && prevLength == currLength {
   384  						return prevUnmarshal(dec, va, uo)
   385  					}
   386  					err = errUnsupportedMutation
   387  				}
   388  				if uo.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
   389  					if err2 := xd.SkipUntil(prevDepth, prevLength+1); err2 != nil {
   390  						return err2
   391  					}
   392  					return err // unlike marshal, never wrapped
   393  				}
   394  				if !isSyntacticError(err) && !export.IsIOError(err) {
   395  					err = newSemanticErrorWithPosition(dec, t, prevDepth, prevLength, err)
   396  				}
   397  				return err
   398  			}
   399  			return nil
   400  		}
   401  	}
   402  
   403  	return fncs
   404  }
   405  
   406  // implementsAny is like t.Implements(ifaceType) for a list of interfaces,
   407  // but checks whether either t or reflect.PointerTo(t) implements the interface.
   408  func implementsAny(t reflect.Type, ifaceTypes ...reflect.Type) bool {
   409  	for _, ifaceType := range ifaceTypes {
   410  		if _, ok := implements(t, ifaceType); ok {
   411  			return true
   412  		}
   413  	}
   414  	return false
   415  }
   416  
   417  // implements is like t.Implements(ifaceType) but checks whether
   418  // either t or reflect.PointerTo(t) implements the interface.
   419  // It also reports whether the value needs to be addressed
   420  // in order to satisfy the interface.
   421  func implements(t, ifaceType reflect.Type) (needAddr, ok bool) {
   422  	switch {
   423  	case t.Implements(ifaceType):
   424  		return false, true
   425  	case reflect.PointerTo(t).Implements(ifaceType):
   426  		return true, true
   427  	default:
   428  		return false, false
   429  	}
   430  }
   431  

View as plain text