github.com/kamalshkeir/kencoding@v0.0.2-0.20230409043843-44b609a0475a/json/codec.go (about)

     1  package json
     2  
     3  import (
     4  	"encoding"
     5  	"encoding/json"
     6  	"fmt"
     7  	"reflect"
     8  	"sort"
     9  	"strconv"
    10  	"strings"
    11  	"sync/atomic"
    12  	"time"
    13  	"unicode"
    14  	"unsafe"
    15  
    16  	"github.com/kamalshkeir/kasm/keyset"
    17  )
    18  
    19  const (
    20  	// 1000 is the value used by the standard encoding/json package.
    21  	//
    22  	// https://cs.opensource.google/go/go/+/refs/tags/go1.17.3:src/encoding/json/encode.go;drc=refs%2Ftags%2Fgo1.17.3;l=300
    23  	startDetectingCyclesAfter = 1000
    24  )
    25  
    26  type codec struct {
    27  	encode encodeFunc
    28  	decode decodeFunc
    29  }
    30  
    31  type encoder struct {
    32  	flags AppendFlags
    33  	// ptrDepth tracks the depth of pointer cycles, when it reaches the value
    34  	// of startDetectingCyclesAfter, the ptrSeen map is allocated and the
    35  	// encoder starts tracking pointers it has seen as an attempt to detect
    36  	// whether it has entered a pointer cycle and needs to error before the
    37  	// goroutine runs out of stack space.
    38  	ptrDepth uint32
    39  	ptrSeen  map[unsafe.Pointer]struct{}
    40  }
    41  
    42  type decoder struct {
    43  	flags ParseFlags
    44  }
    45  
    46  type encodeFunc func(encoder, []byte, unsafe.Pointer) ([]byte, error)
    47  type decodeFunc func(decoder, []byte, unsafe.Pointer) ([]byte, error)
    48  
    49  type emptyFunc func(unsafe.Pointer) bool
    50  type sortFunc func([]reflect.Value)
    51  
    52  var (
    53  	// Eventually consistent cache mapping go types to dynamically generated
    54  	// codecs.
    55  	//
    56  	// Note: using a uintptr as key instead of reflect.Type shaved ~15ns off of
    57  	// the ~30ns Marhsal/Unmarshal functions which were dominated by the map
    58  	// lookup time for simple types like bool, int, etc..
    59  	cache unsafe.Pointer // map[unsafe.Pointer]codec
    60  )
    61  
    62  func cacheLoad() map[unsafe.Pointer]codec {
    63  	p := atomic.LoadPointer(&cache)
    64  	return *(*map[unsafe.Pointer]codec)(unsafe.Pointer(&p))
    65  }
    66  
    67  func cacheStore(typ reflect.Type, cod codec, oldCodecs map[unsafe.Pointer]codec) {
    68  	newCodecs := make(map[unsafe.Pointer]codec, len(oldCodecs)+1)
    69  	newCodecs[typeid(typ)] = cod
    70  
    71  	for t, c := range oldCodecs {
    72  		newCodecs[t] = c
    73  	}
    74  
    75  	atomic.StorePointer(&cache, *(*unsafe.Pointer)(unsafe.Pointer(&newCodecs)))
    76  }
    77  
    78  func typeid(t reflect.Type) unsafe.Pointer {
    79  	return (*iface)(unsafe.Pointer(&t)).ptr
    80  }
    81  
    82  func constructCachedCodec(t reflect.Type, cache map[unsafe.Pointer]codec) codec {
    83  	c := constructCodec(t, map[reflect.Type]*structType{}, t.Kind() == reflect.Ptr)
    84  
    85  	if inlined(t) {
    86  		c.encode = constructInlineValueEncodeFunc(c.encode)
    87  	}
    88  
    89  	cacheStore(t, c, cache)
    90  	return c
    91  }
    92  
    93  func constructCodec(t reflect.Type, seen map[reflect.Type]*structType, canAddr bool) (c codec) {
    94  	switch t {
    95  	case nullType, nil:
    96  		c = codec{encode: encoder.encodeNull, decode: decoder.decodeNull}
    97  
    98  	case numberType:
    99  		c = codec{encode: encoder.encodeNumber, decode: decoder.decodeNumber}
   100  
   101  	case bytesType:
   102  		c = codec{encode: encoder.encodeBytes, decode: decoder.decodeBytes}
   103  
   104  	case durationType:
   105  		c = codec{encode: encoder.encodeDuration, decode: decoder.decodeDuration}
   106  
   107  	case timeType:
   108  		c = codec{encode: encoder.encodeTime, decode: decoder.decodeTime}
   109  
   110  	case interfaceType:
   111  		c = codec{encode: encoder.encodeInterface, decode: decoder.decodeInterface}
   112  
   113  	case rawMessageType:
   114  		c = codec{encode: encoder.encodeRawMessage, decode: decoder.decodeRawMessage}
   115  
   116  	case numberPtrType:
   117  		c = constructPointerCodec(numberPtrType, nil)
   118  
   119  	case durationPtrType:
   120  		c = constructPointerCodec(durationPtrType, nil)
   121  
   122  	case timePtrType:
   123  		c = constructPointerCodec(timePtrType, nil)
   124  
   125  	case rawMessagePtrType:
   126  		c = constructPointerCodec(rawMessagePtrType, nil)
   127  	}
   128  
   129  	if c.encode != nil {
   130  		return
   131  	}
   132  
   133  	switch t.Kind() {
   134  	case reflect.Bool:
   135  		c = codec{encode: encoder.encodeBool, decode: decoder.decodeBool}
   136  
   137  	case reflect.Int:
   138  		c = codec{encode: encoder.encodeInt, decode: decoder.decodeInt}
   139  
   140  	case reflect.Int8:
   141  		c = codec{encode: encoder.encodeInt8, decode: decoder.decodeInt8}
   142  
   143  	case reflect.Int16:
   144  		c = codec{encode: encoder.encodeInt16, decode: decoder.decodeInt16}
   145  
   146  	case reflect.Int32:
   147  		c = codec{encode: encoder.encodeInt32, decode: decoder.decodeInt32}
   148  
   149  	case reflect.Int64:
   150  		c = codec{encode: encoder.encodeInt64, decode: decoder.decodeInt64}
   151  
   152  	case reflect.Uint:
   153  		c = codec{encode: encoder.encodeUint, decode: decoder.decodeUint}
   154  
   155  	case reflect.Uintptr:
   156  		c = codec{encode: encoder.encodeUintptr, decode: decoder.decodeUintptr}
   157  
   158  	case reflect.Uint8:
   159  		c = codec{encode: encoder.encodeUint8, decode: decoder.decodeUint8}
   160  
   161  	case reflect.Uint16:
   162  		c = codec{encode: encoder.encodeUint16, decode: decoder.decodeUint16}
   163  
   164  	case reflect.Uint32:
   165  		c = codec{encode: encoder.encodeUint32, decode: decoder.decodeUint32}
   166  
   167  	case reflect.Uint64:
   168  		c = codec{encode: encoder.encodeUint64, decode: decoder.decodeUint64}
   169  
   170  	case reflect.Float32:
   171  		c = codec{encode: encoder.encodeFloat32, decode: decoder.decodeFloat32}
   172  
   173  	case reflect.Float64:
   174  		c = codec{encode: encoder.encodeFloat64, decode: decoder.decodeFloat64}
   175  
   176  	case reflect.String:
   177  		c = codec{encode: encoder.encodeString, decode: decoder.decodeString}
   178  
   179  	case reflect.Interface:
   180  		c = constructInterfaceCodec(t)
   181  
   182  	case reflect.Array:
   183  		c = constructArrayCodec(t, seen, canAddr)
   184  
   185  	case reflect.Slice:
   186  		c = constructSliceCodec(t, seen)
   187  
   188  	case reflect.Map:
   189  		c = constructMapCodec(t, seen)
   190  
   191  	case reflect.Struct:
   192  		c = constructStructCodec(t, seen, canAddr)
   193  
   194  	case reflect.Ptr:
   195  		c = constructPointerCodec(t, seen)
   196  
   197  	default:
   198  		c = constructUnsupportedTypeCodec(t)
   199  	}
   200  
   201  	p := reflect.PtrTo(t)
   202  
   203  	if canAddr {
   204  		switch {
   205  		case p.Implements(jsonMarshalerType):
   206  			c.encode = constructJSONMarshalerEncodeFunc(t, true)
   207  		case p.Implements(textMarshalerType):
   208  			c.encode = constructTextMarshalerEncodeFunc(t, true)
   209  		}
   210  	}
   211  
   212  	switch {
   213  	case t.Implements(jsonMarshalerType):
   214  		c.encode = constructJSONMarshalerEncodeFunc(t, false)
   215  	case t.Implements(textMarshalerType):
   216  		c.encode = constructTextMarshalerEncodeFunc(t, false)
   217  	}
   218  
   219  	switch {
   220  	case p.Implements(jsonUnmarshalerType):
   221  		c.decode = constructJSONUnmarshalerDecodeFunc(t, true)
   222  	case p.Implements(textUnmarshalerType):
   223  		c.decode = constructTextUnmarshalerDecodeFunc(t, true)
   224  	}
   225  
   226  	return
   227  }
   228  
   229  func constructStringCodec(t reflect.Type, seen map[reflect.Type]*structType, canAddr bool) codec {
   230  	c := constructCodec(t, seen, canAddr)
   231  	return codec{
   232  		encode: constructStringEncodeFunc(c.encode),
   233  		decode: constructStringDecodeFunc(c.decode),
   234  	}
   235  }
   236  
   237  func constructStringEncodeFunc(encode encodeFunc) encodeFunc {
   238  	return func(e encoder, b []byte, p unsafe.Pointer) ([]byte, error) {
   239  		return e.encodeToString(b, p, encode)
   240  	}
   241  }
   242  
   243  func constructStringDecodeFunc(decode decodeFunc) decodeFunc {
   244  	return func(d decoder, b []byte, p unsafe.Pointer) ([]byte, error) {
   245  		return d.decodeFromString(b, p, decode)
   246  	}
   247  }
   248  
   249  func constructStringToIntDecodeFunc(t reflect.Type, decode decodeFunc) decodeFunc {
   250  	return func(d decoder, b []byte, p unsafe.Pointer) ([]byte, error) {
   251  		return d.decodeFromStringToInt(b, p, t, decode)
   252  	}
   253  }
   254  
   255  func constructArrayCodec(t reflect.Type, seen map[reflect.Type]*structType, canAddr bool) codec {
   256  	e := t.Elem()
   257  	c := constructCodec(e, seen, canAddr)
   258  	s := alignedSize(e)
   259  	return codec{
   260  		encode: constructArrayEncodeFunc(s, t, c.encode),
   261  		decode: constructArrayDecodeFunc(s, t, c.decode),
   262  	}
   263  }
   264  
   265  func constructArrayEncodeFunc(size uintptr, t reflect.Type, encode encodeFunc) encodeFunc {
   266  	n := t.Len()
   267  	return func(e encoder, b []byte, p unsafe.Pointer) ([]byte, error) {
   268  		return e.encodeArray(b, p, n, size, t, encode)
   269  	}
   270  }
   271  
   272  func constructArrayDecodeFunc(size uintptr, t reflect.Type, decode decodeFunc) decodeFunc {
   273  	n := t.Len()
   274  	return func(d decoder, b []byte, p unsafe.Pointer) ([]byte, error) {
   275  		return d.decodeArray(b, p, n, size, t, decode)
   276  	}
   277  }
   278  
   279  func constructSliceCodec(t reflect.Type, seen map[reflect.Type]*structType) codec {
   280  	e := t.Elem()
   281  	s := alignedSize(e)
   282  
   283  	if e.Kind() == reflect.Uint8 {
   284  		// Go 1.7+ behavior: slices of byte types (and aliases) may override the
   285  		// default encoding and decoding behaviors by implementing marshaler and
   286  		// unmarshaler interfaces.
   287  		p := reflect.PtrTo(e)
   288  		c := codec{}
   289  
   290  		switch {
   291  		case e.Implements(jsonMarshalerType):
   292  			c.encode = constructJSONMarshalerEncodeFunc(e, false)
   293  		case e.Implements(textMarshalerType):
   294  			c.encode = constructTextMarshalerEncodeFunc(e, false)
   295  		case p.Implements(jsonMarshalerType):
   296  			c.encode = constructJSONMarshalerEncodeFunc(e, true)
   297  		case p.Implements(textMarshalerType):
   298  			c.encode = constructTextMarshalerEncodeFunc(e, true)
   299  		}
   300  
   301  		switch {
   302  		case e.Implements(jsonUnmarshalerType):
   303  			c.decode = constructJSONUnmarshalerDecodeFunc(e, false)
   304  		case e.Implements(textUnmarshalerType):
   305  			c.decode = constructTextUnmarshalerDecodeFunc(e, false)
   306  		case p.Implements(jsonUnmarshalerType):
   307  			c.decode = constructJSONUnmarshalerDecodeFunc(e, true)
   308  		case p.Implements(textUnmarshalerType):
   309  			c.decode = constructTextUnmarshalerDecodeFunc(e, true)
   310  		}
   311  
   312  		if c.encode != nil {
   313  			c.encode = constructSliceEncodeFunc(s, t, c.encode)
   314  		} else {
   315  			c.encode = encoder.encodeBytes
   316  		}
   317  
   318  		if c.decode != nil {
   319  			c.decode = constructSliceDecodeFunc(s, t, c.decode)
   320  		} else {
   321  			c.decode = decoder.decodeBytes
   322  		}
   323  
   324  		return c
   325  	}
   326  
   327  	c := constructCodec(e, seen, true)
   328  	return codec{
   329  		encode: constructSliceEncodeFunc(s, t, c.encode),
   330  		decode: constructSliceDecodeFunc(s, t, c.decode),
   331  	}
   332  }
   333  
   334  func constructSliceEncodeFunc(size uintptr, t reflect.Type, encode encodeFunc) encodeFunc {
   335  	return func(e encoder, b []byte, p unsafe.Pointer) ([]byte, error) {
   336  		return e.encodeSlice(b, p, size, t, encode)
   337  	}
   338  }
   339  
   340  func constructSliceDecodeFunc(size uintptr, t reflect.Type, decode decodeFunc) decodeFunc {
   341  	return func(d decoder, b []byte, p unsafe.Pointer) ([]byte, error) {
   342  		return d.decodeSlice(b, p, size, t, decode)
   343  	}
   344  }
   345  
   346  func constructMapCodec(t reflect.Type, seen map[reflect.Type]*structType) codec {
   347  	var sortKeys sortFunc
   348  	k := t.Key()
   349  	v := t.Elem()
   350  
   351  	// Faster implementations for some common cases.
   352  	switch {
   353  	case k == stringType && v == interfaceType:
   354  		return codec{
   355  			encode: encoder.encodeMapStringInterface,
   356  			decode: decoder.decodeMapStringInterface,
   357  		}
   358  
   359  	case k == stringType && v == rawMessageType:
   360  		return codec{
   361  			encode: encoder.encodeMapStringRawMessage,
   362  			decode: decoder.decodeMapStringRawMessage,
   363  		}
   364  
   365  	case k == stringType && v == stringType:
   366  		return codec{
   367  			encode: encoder.encodeMapStringString,
   368  			decode: decoder.decodeMapStringString,
   369  		}
   370  
   371  	case k == stringType && v == stringsType:
   372  		return codec{
   373  			encode: encoder.encodeMapStringStringSlice,
   374  			decode: decoder.decodeMapStringStringSlice,
   375  		}
   376  
   377  	case k == stringType && v == boolType:
   378  		return codec{
   379  			encode: encoder.encodeMapStringBool,
   380  			decode: decoder.decodeMapStringBool,
   381  		}
   382  	}
   383  
   384  	kc := codec{}
   385  	vc := constructCodec(v, seen, false)
   386  
   387  	if k.Implements(textMarshalerType) || reflect.PtrTo(k).Implements(textUnmarshalerType) {
   388  		kc.encode = constructTextMarshalerEncodeFunc(k, false)
   389  		kc.decode = constructTextUnmarshalerDecodeFunc(k, true)
   390  
   391  		sortKeys = func(keys []reflect.Value) {
   392  			sort.Slice(keys, func(i, j int) bool {
   393  				// This is a performance abomination but the use case is rare
   394  				// enough that it shouldn't be a problem in practice.
   395  				k1, _ := keys[i].Interface().(encoding.TextMarshaler).MarshalText()
   396  				k2, _ := keys[j].Interface().(encoding.TextMarshaler).MarshalText()
   397  				return string(k1) < string(k2)
   398  			})
   399  		}
   400  	} else {
   401  		switch k.Kind() {
   402  		case reflect.String:
   403  			kc.encode = encoder.encodeString
   404  			kc.decode = decoder.decodeString
   405  
   406  			sortKeys = func(keys []reflect.Value) {
   407  				sort.Slice(keys, func(i, j int) bool { return keys[i].String() < keys[j].String() })
   408  			}
   409  
   410  		case reflect.Int,
   411  			reflect.Int8,
   412  			reflect.Int16,
   413  			reflect.Int32,
   414  			reflect.Int64:
   415  			kc = constructStringCodec(k, seen, false)
   416  
   417  			sortKeys = func(keys []reflect.Value) {
   418  				sort.Slice(keys, func(i, j int) bool { return intStringsAreSorted(keys[i].Int(), keys[j].Int()) })
   419  			}
   420  
   421  		case reflect.Uint,
   422  			reflect.Uintptr,
   423  			reflect.Uint8,
   424  			reflect.Uint16,
   425  			reflect.Uint32,
   426  			reflect.Uint64:
   427  			kc = constructStringCodec(k, seen, false)
   428  
   429  			sortKeys = func(keys []reflect.Value) {
   430  				sort.Slice(keys, func(i, j int) bool { return uintStringsAreSorted(keys[i].Uint(), keys[j].Uint()) })
   431  			}
   432  
   433  		default:
   434  			return constructUnsupportedTypeCodec(t)
   435  		}
   436  	}
   437  
   438  	if inlined(v) {
   439  		vc.encode = constructInlineValueEncodeFunc(vc.encode)
   440  	}
   441  
   442  	return codec{
   443  		encode: constructMapEncodeFunc(t, kc.encode, vc.encode, sortKeys),
   444  		decode: constructMapDecodeFunc(t, kc.decode, vc.decode),
   445  	}
   446  }
   447  
   448  func constructMapEncodeFunc(t reflect.Type, encodeKey, encodeValue encodeFunc, sortKeys sortFunc) encodeFunc {
   449  	return func(e encoder, b []byte, p unsafe.Pointer) ([]byte, error) {
   450  		return e.encodeMap(b, p, t, encodeKey, encodeValue, sortKeys)
   451  	}
   452  }
   453  
   454  func constructMapDecodeFunc(t reflect.Type, decodeKey, decodeValue decodeFunc) decodeFunc {
   455  	kt := t.Key()
   456  	vt := t.Elem()
   457  	kz := reflect.Zero(kt)
   458  	vz := reflect.Zero(vt)
   459  	return func(d decoder, b []byte, p unsafe.Pointer) ([]byte, error) {
   460  		return d.decodeMap(b, p, t, kt, vt, kz, vz, decodeKey, decodeValue)
   461  	}
   462  }
   463  
   464  func constructStructCodec(t reflect.Type, seen map[reflect.Type]*structType, canAddr bool) codec {
   465  	st := constructStructType(t, seen, canAddr)
   466  	return codec{
   467  		encode: constructStructEncodeFunc(st),
   468  		decode: constructStructDecodeFunc(st),
   469  	}
   470  }
   471  
   472  func constructStructType(t reflect.Type, seen map[reflect.Type]*structType, canAddr bool) *structType {
   473  	// Used for preventing infinite recursion on types that have pointers to
   474  	// themselves.
   475  	st := seen[t]
   476  
   477  	if st == nil {
   478  		st = &structType{
   479  			fields:      make([]structField, 0, t.NumField()),
   480  			fieldsIndex: make(map[string]*structField),
   481  			ficaseIndex: make(map[string]*structField),
   482  			typ:         t,
   483  		}
   484  
   485  		seen[t] = st
   486  		st.fields = appendStructFields(st.fields, t, 0, seen, canAddr)
   487  
   488  		for i := range st.fields {
   489  			f := &st.fields[i]
   490  			s := strings.ToLower(f.name)
   491  			st.fieldsIndex[f.name] = f
   492  			// When there is ambiguity because multiple fields have the same
   493  			// case-insensitive representation, the first field must win.
   494  			if _, exists := st.ficaseIndex[s]; !exists {
   495  				st.ficaseIndex[s] = f
   496  			}
   497  		}
   498  
   499  		if len(st.fields) <= 32 {
   500  			keys := make([][]byte, len(st.fields))
   501  			for i, f := range st.fields {
   502  				keys[i] = []byte(f.name)
   503  			}
   504  			st.keyset = keyset.New(keys)
   505  		}
   506  	}
   507  
   508  	return st
   509  }
   510  
   511  func constructStructEncodeFunc(st *structType) encodeFunc {
   512  	return func(e encoder, b []byte, p unsafe.Pointer) ([]byte, error) {
   513  		return e.encodeStruct(b, p, st)
   514  	}
   515  }
   516  
   517  func constructStructDecodeFunc(st *structType) decodeFunc {
   518  	return func(d decoder, b []byte, p unsafe.Pointer) ([]byte, error) {
   519  		return d.decodeStruct(b, p, st)
   520  	}
   521  }
   522  
   523  func constructEmbeddedStructPointerCodec(t reflect.Type, unexported bool, offset uintptr, field codec) codec {
   524  	return codec{
   525  		encode: constructEmbeddedStructPointerEncodeFunc(t, unexported, offset, field.encode),
   526  		decode: constructEmbeddedStructPointerDecodeFunc(t, unexported, offset, field.decode),
   527  	}
   528  }
   529  
   530  func constructEmbeddedStructPointerEncodeFunc(t reflect.Type, unexported bool, offset uintptr, encode encodeFunc) encodeFunc {
   531  	return func(e encoder, b []byte, p unsafe.Pointer) ([]byte, error) {
   532  		return e.encodeEmbeddedStructPointer(b, p, t, unexported, offset, encode)
   533  	}
   534  }
   535  
   536  func constructEmbeddedStructPointerDecodeFunc(t reflect.Type, unexported bool, offset uintptr, decode decodeFunc) decodeFunc {
   537  	return func(d decoder, b []byte, p unsafe.Pointer) ([]byte, error) {
   538  		return d.decodeEmbeddedStructPointer(b, p, t, unexported, offset, decode)
   539  	}
   540  }
   541  
   542  func appendStructFields(fields []structField, t reflect.Type, offset uintptr, seen map[reflect.Type]*structType, canAddr bool) []structField {
   543  	type embeddedField struct {
   544  		index      int
   545  		offset     uintptr
   546  		pointer    bool
   547  		unexported bool
   548  		subtype    *structType
   549  		subfield   *structField
   550  	}
   551  
   552  	names := make(map[string]struct{})
   553  	embedded := make([]embeddedField, 0, 10)
   554  
   555  	for i, n := 0, t.NumField(); i < n; i++ {
   556  		f := t.Field(i)
   557  
   558  		var (
   559  			name       = f.Name
   560  			anonymous  = f.Anonymous
   561  			tag        = false
   562  			omitempty  = false
   563  			stringify  = false
   564  			unexported = len(f.PkgPath) != 0
   565  		)
   566  
   567  		if unexported && !anonymous { // unexported
   568  			continue
   569  		}
   570  
   571  		if parts := strings.Split(f.Tag.Get("json"), ","); len(parts) != 0 {
   572  			if len(parts[0]) != 0 {
   573  				name, tag = parts[0], true
   574  			}
   575  
   576  			if name == "-" && len(parts) == 1 { // ignored
   577  				continue
   578  			}
   579  
   580  			if !isValidTag(name) {
   581  				name = f.Name
   582  			}
   583  
   584  			for _, tag := range parts[1:] {
   585  				switch tag {
   586  				case "omitempty":
   587  					omitempty = true
   588  				case "string":
   589  					stringify = true
   590  				}
   591  			}
   592  		}
   593  
   594  		if anonymous && !tag { // embedded
   595  			typ := f.Type
   596  			ptr := f.Type.Kind() == reflect.Ptr
   597  
   598  			if ptr {
   599  				typ = f.Type.Elem()
   600  			}
   601  
   602  			if typ.Kind() == reflect.Struct {
   603  				// When the embedded fields is inlined the fields can be looked
   604  				// up by offset from the address of the wrapping object, so we
   605  				// simply add the embedded struct fields to the list of fields
   606  				// of the current struct type.
   607  				subtype := constructStructType(typ, seen, canAddr)
   608  
   609  				for j := range subtype.fields {
   610  					embedded = append(embedded, embeddedField{
   611  						index:      i<<32 | j,
   612  						offset:     offset + f.Offset,
   613  						pointer:    ptr,
   614  						unexported: unexported,
   615  						subtype:    subtype,
   616  						subfield:   &subtype.fields[j],
   617  					})
   618  				}
   619  
   620  				continue
   621  			}
   622  
   623  			if unexported { // ignore unexported non-struct types
   624  				continue
   625  			}
   626  		}
   627  
   628  		codec := constructCodec(f.Type, seen, canAddr)
   629  
   630  		if stringify {
   631  			// https://golang.org/pkg/encoding/json/#Marshal
   632  			//
   633  			// The "string" option signals that a field is stored as JSON inside
   634  			// a JSON-encoded string. It applies only to fields of string,
   635  			// floating point, integer, or boolean types. This extra level of
   636  			// encoding is sometimes used when communicating with JavaScript
   637  			// programs:
   638  			typ := f.Type
   639  
   640  			if typ.Kind() == reflect.Ptr {
   641  				typ = typ.Elem()
   642  			}
   643  
   644  			switch typ.Kind() {
   645  			case reflect.Int,
   646  				reflect.Int8,
   647  				reflect.Int16,
   648  				reflect.Int32,
   649  				reflect.Int64,
   650  				reflect.Uint,
   651  				reflect.Uintptr,
   652  				reflect.Uint8,
   653  				reflect.Uint16,
   654  				reflect.Uint32,
   655  				reflect.Uint64:
   656  				codec.encode = constructStringEncodeFunc(codec.encode)
   657  				codec.decode = constructStringToIntDecodeFunc(typ, codec.decode)
   658  			case reflect.Bool,
   659  				reflect.Float32,
   660  				reflect.Float64,
   661  				reflect.String:
   662  				codec.encode = constructStringEncodeFunc(codec.encode)
   663  				codec.decode = constructStringDecodeFunc(codec.decode)
   664  			}
   665  		}
   666  
   667  		fields = append(fields, structField{
   668  			codec:     codec,
   669  			offset:    offset + f.Offset,
   670  			empty:     emptyFuncOf(f.Type),
   671  			tag:       tag,
   672  			omitempty: omitempty,
   673  			name:      name,
   674  			index:     i << 32,
   675  			typ:       f.Type,
   676  			zero:      reflect.Zero(f.Type),
   677  		})
   678  
   679  		names[name] = struct{}{}
   680  	}
   681  
   682  	// Only unambiguous embedded fields must be serialized.
   683  	ambiguousNames := make(map[string]int)
   684  	ambiguousTags := make(map[string]int)
   685  
   686  	// Embedded types can never override a field that was already present at
   687  	// the top-level.
   688  	for name := range names {
   689  		ambiguousNames[name]++
   690  		ambiguousTags[name]++
   691  	}
   692  
   693  	for _, embfield := range embedded {
   694  		ambiguousNames[embfield.subfield.name]++
   695  		if embfield.subfield.tag {
   696  			ambiguousTags[embfield.subfield.name]++
   697  		}
   698  	}
   699  
   700  	for _, embfield := range embedded {
   701  		subfield := *embfield.subfield
   702  
   703  		if ambiguousNames[subfield.name] > 1 && !(subfield.tag && ambiguousTags[subfield.name] == 1) {
   704  			continue // ambiguous embedded field
   705  		}
   706  
   707  		if embfield.pointer {
   708  			subfield.codec = constructEmbeddedStructPointerCodec(embfield.subtype.typ, embfield.unexported, subfield.offset, subfield.codec)
   709  			subfield.offset = embfield.offset
   710  		} else {
   711  			subfield.offset += embfield.offset
   712  		}
   713  
   714  		// To prevent dominant flags more than one level below the embedded one.
   715  		subfield.tag = false
   716  
   717  		// To ensure the order of the fields in the output is the same is in the
   718  		// struct type.
   719  		subfield.index = embfield.index
   720  
   721  		fields = append(fields, subfield)
   722  	}
   723  
   724  	for i := range fields {
   725  		name := fields[i].name
   726  		fields[i].json = encodeKeyFragment(name, 0)
   727  		fields[i].html = encodeKeyFragment(name, EscapeHTML)
   728  	}
   729  
   730  	sort.Slice(fields, func(i, j int) bool { return fields[i].index < fields[j].index })
   731  	return fields
   732  }
   733  
   734  func encodeKeyFragment(s string, flags AppendFlags) string {
   735  	b := make([]byte, 1, len(s)+4)
   736  	b[0] = ','
   737  	e := encoder{flags: flags}
   738  	b, _ = e.encodeString(b, unsafe.Pointer(&s))
   739  	b = append(b, ':')
   740  	return *(*string)(unsafe.Pointer(&b))
   741  }
   742  
   743  func constructPointerCodec(t reflect.Type, seen map[reflect.Type]*structType) codec {
   744  	e := t.Elem()
   745  	c := constructCodec(e, seen, true)
   746  	return codec{
   747  		encode: constructPointerEncodeFunc(e, c.encode),
   748  		decode: constructPointerDecodeFunc(e, c.decode),
   749  	}
   750  }
   751  
   752  func constructPointerEncodeFunc(t reflect.Type, encode encodeFunc) encodeFunc {
   753  	return func(e encoder, b []byte, p unsafe.Pointer) ([]byte, error) {
   754  		return e.encodePointer(b, p, t, encode)
   755  	}
   756  }
   757  
   758  func constructPointerDecodeFunc(t reflect.Type, decode decodeFunc) decodeFunc {
   759  	return func(d decoder, b []byte, p unsafe.Pointer) ([]byte, error) {
   760  		return d.decodePointer(b, p, t, decode)
   761  	}
   762  }
   763  
   764  func constructInterfaceCodec(t reflect.Type) codec {
   765  	return codec{
   766  		encode: constructMaybeEmptyInterfaceEncoderFunc(t),
   767  		decode: constructMaybeEmptyInterfaceDecoderFunc(t),
   768  	}
   769  }
   770  
   771  func constructMaybeEmptyInterfaceEncoderFunc(t reflect.Type) encodeFunc {
   772  	return func(e encoder, b []byte, p unsafe.Pointer) ([]byte, error) {
   773  		return e.encodeMaybeEmptyInterface(b, p, t)
   774  	}
   775  }
   776  
   777  func constructMaybeEmptyInterfaceDecoderFunc(t reflect.Type) decodeFunc {
   778  	return func(d decoder, b []byte, p unsafe.Pointer) ([]byte, error) {
   779  		return d.decodeMaybeEmptyInterface(b, p, t)
   780  	}
   781  }
   782  
   783  func constructUnsupportedTypeCodec(t reflect.Type) codec {
   784  	return codec{
   785  		encode: constructUnsupportedTypeEncodeFunc(t),
   786  		decode: constructUnsupportedTypeDecodeFunc(t),
   787  	}
   788  }
   789  
   790  func constructUnsupportedTypeEncodeFunc(t reflect.Type) encodeFunc {
   791  	return func(e encoder, b []byte, p unsafe.Pointer) ([]byte, error) {
   792  		return e.encodeUnsupportedTypeError(b, p, t)
   793  	}
   794  }
   795  
   796  func constructUnsupportedTypeDecodeFunc(t reflect.Type) decodeFunc {
   797  	return func(d decoder, b []byte, p unsafe.Pointer) ([]byte, error) {
   798  		return d.decodeUnmarshalTypeError(b, p, t)
   799  	}
   800  }
   801  
   802  func constructJSONMarshalerEncodeFunc(t reflect.Type, pointer bool) encodeFunc {
   803  	return func(e encoder, b []byte, p unsafe.Pointer) ([]byte, error) {
   804  		return e.encodeJSONMarshaler(b, p, t, pointer)
   805  	}
   806  }
   807  
   808  func constructJSONUnmarshalerDecodeFunc(t reflect.Type, pointer bool) decodeFunc {
   809  	return func(d decoder, b []byte, p unsafe.Pointer) ([]byte, error) {
   810  		return d.decodeJSONUnmarshaler(b, p, t, pointer)
   811  	}
   812  }
   813  
   814  func constructTextMarshalerEncodeFunc(t reflect.Type, pointer bool) encodeFunc {
   815  	return func(e encoder, b []byte, p unsafe.Pointer) ([]byte, error) {
   816  		return e.encodeTextMarshaler(b, p, t, pointer)
   817  	}
   818  }
   819  
   820  func constructTextUnmarshalerDecodeFunc(t reflect.Type, pointer bool) decodeFunc {
   821  	return func(d decoder, b []byte, p unsafe.Pointer) ([]byte, error) {
   822  		return d.decodeTextUnmarshaler(b, p, t, pointer)
   823  	}
   824  }
   825  
   826  func constructInlineValueEncodeFunc(encode encodeFunc) encodeFunc {
   827  	return func(e encoder, b []byte, p unsafe.Pointer) ([]byte, error) {
   828  		return encode(e, b, noescape(unsafe.Pointer(&p)))
   829  	}
   830  }
   831  
   832  // noescape hides a pointer from escape analysis.  noescape is
   833  // the identity function but escape analysis doesn't think the
   834  // output depends on the input. noescape is inlined and currently
   835  // compiles down to zero instructions.
   836  // USE CAREFULLY!
   837  // This was copied from the runtime; see issues 23382 and 7921.
   838  //
   839  //go:nosplit
   840  func noescape(p unsafe.Pointer) unsafe.Pointer {
   841  	x := uintptr(p)
   842  	return unsafe.Pointer(x ^ 0)
   843  }
   844  
   845  func alignedSize(t reflect.Type) uintptr {
   846  	a := t.Align()
   847  	s := t.Size()
   848  	return align(uintptr(a), uintptr(s))
   849  }
   850  
   851  func align(align, size uintptr) uintptr {
   852  	if align != 0 && (size%align) != 0 {
   853  		size = ((size / align) + 1) * align
   854  	}
   855  	return size
   856  }
   857  
   858  func inlined(t reflect.Type) bool {
   859  	switch t.Kind() {
   860  	case reflect.Ptr:
   861  		return true
   862  	case reflect.Map:
   863  		return true
   864  	case reflect.Struct:
   865  		return t.NumField() == 1 && inlined(t.Field(0).Type)
   866  	default:
   867  		return false
   868  	}
   869  }
   870  
   871  func isValidTag(s string) bool {
   872  	if s == "" {
   873  		return false
   874  	}
   875  	for _, c := range s {
   876  		switch {
   877  		case strings.ContainsRune("!#$%&()*+-./:;<=>?@[]^_{|}~ ", c):
   878  			// Backslash and quote chars are reserved, but
   879  			// otherwise any punctuation chars are allowed
   880  			// in a tag name.
   881  		default:
   882  			if !unicode.IsLetter(c) && !unicode.IsDigit(c) {
   883  				return false
   884  			}
   885  		}
   886  	}
   887  	return true
   888  }
   889  
   890  func emptyFuncOf(t reflect.Type) emptyFunc {
   891  	switch t {
   892  	case bytesType, rawMessageType:
   893  		return func(p unsafe.Pointer) bool { return (*slice)(p).len == 0 }
   894  	}
   895  
   896  	switch t.Kind() {
   897  	case reflect.Array:
   898  		if t.Len() == 0 {
   899  			return func(unsafe.Pointer) bool { return true }
   900  		}
   901  
   902  	case reflect.Map:
   903  		return func(p unsafe.Pointer) bool { return reflect.NewAt(t, p).Elem().Len() == 0 }
   904  
   905  	case reflect.Slice:
   906  		return func(p unsafe.Pointer) bool { return (*slice)(p).len == 0 }
   907  
   908  	case reflect.String:
   909  		return func(p unsafe.Pointer) bool { return len(*(*string)(p)) == 0 }
   910  
   911  	case reflect.Bool:
   912  		return func(p unsafe.Pointer) bool { return !*(*bool)(p) }
   913  
   914  	case reflect.Int, reflect.Uint:
   915  		return func(p unsafe.Pointer) bool { return *(*uint)(p) == 0 }
   916  
   917  	case reflect.Uintptr:
   918  		return func(p unsafe.Pointer) bool { return *(*uintptr)(p) == 0 }
   919  
   920  	case reflect.Int8, reflect.Uint8:
   921  		return func(p unsafe.Pointer) bool { return *(*uint8)(p) == 0 }
   922  
   923  	case reflect.Int16, reflect.Uint16:
   924  		return func(p unsafe.Pointer) bool { return *(*uint16)(p) == 0 }
   925  
   926  	case reflect.Int32, reflect.Uint32:
   927  		return func(p unsafe.Pointer) bool { return *(*uint32)(p) == 0 }
   928  
   929  	case reflect.Int64, reflect.Uint64:
   930  		return func(p unsafe.Pointer) bool { return *(*uint64)(p) == 0 }
   931  
   932  	case reflect.Float32:
   933  		return func(p unsafe.Pointer) bool { return *(*float32)(p) == 0 }
   934  
   935  	case reflect.Float64:
   936  		return func(p unsafe.Pointer) bool { return *(*float64)(p) == 0 }
   937  
   938  	case reflect.Ptr:
   939  		return func(p unsafe.Pointer) bool { return *(*unsafe.Pointer)(p) == nil }
   940  
   941  	case reflect.Interface:
   942  		return func(p unsafe.Pointer) bool { return (*iface)(p).ptr == nil }
   943  	}
   944  
   945  	return func(unsafe.Pointer) bool { return false }
   946  }
   947  
   948  type iface struct {
   949  	typ unsafe.Pointer
   950  	ptr unsafe.Pointer
   951  }
   952  
   953  type slice struct {
   954  	data unsafe.Pointer
   955  	len  int
   956  	cap  int
   957  }
   958  
   959  type structType struct {
   960  	fields      []structField
   961  	fieldsIndex map[string]*structField
   962  	ficaseIndex map[string]*structField
   963  	keyset      []byte
   964  	typ         reflect.Type
   965  	inlined     bool
   966  }
   967  
   968  type structField struct {
   969  	codec     codec
   970  	offset    uintptr
   971  	empty     emptyFunc
   972  	tag       bool
   973  	omitempty bool
   974  	json      string
   975  	html      string
   976  	name      string
   977  	typ       reflect.Type
   978  	zero      reflect.Value
   979  	index     int
   980  }
   981  
   982  func unmarshalTypeError(b []byte, t reflect.Type) error {
   983  	return &UnmarshalTypeError{Value: strconv.Quote(prefix(b)), Type: t}
   984  }
   985  
   986  func unmarshalOverflow(b []byte, t reflect.Type) error {
   987  	return &UnmarshalTypeError{Value: "number " + prefix(b) + " overflows", Type: t}
   988  }
   989  
   990  func unexpectedEOF(b []byte) error {
   991  	return syntaxError(b, "unexpected end of JSON input")
   992  }
   993  
   994  var syntaxErrorMsgOffset = ^uintptr(0)
   995  
   996  func init() {
   997  	t := reflect.TypeOf(SyntaxError{})
   998  	for i, n := 0, t.NumField(); i < n; i++ {
   999  		if f := t.Field(i); f.Type.Kind() == reflect.String {
  1000  			syntaxErrorMsgOffset = f.Offset
  1001  		}
  1002  	}
  1003  }
  1004  
  1005  func syntaxError(b []byte, msg string, args ...interface{}) error {
  1006  	e := new(SyntaxError)
  1007  	i := syntaxErrorMsgOffset
  1008  	if i != ^uintptr(0) {
  1009  		s := "json: " + fmt.Sprintf(msg, args...) + ": " + prefix(b)
  1010  		p := unsafe.Pointer(e)
  1011  		// Hack to set the unexported `msg` field.
  1012  		*(*string)(unsafe.Pointer(uintptr(p) + i)) = s
  1013  	}
  1014  	return e
  1015  }
  1016  
  1017  func objectKeyError(b []byte, err error) ([]byte, error) {
  1018  	if len(b) == 0 {
  1019  		return nil, unexpectedEOF(b)
  1020  	}
  1021  	switch err.(type) {
  1022  	case *UnmarshalTypeError:
  1023  		err = syntaxError(b, "invalid character '%c' looking for beginning of object key", b[0])
  1024  	}
  1025  	return b, err
  1026  }
  1027  
  1028  func prefix(b []byte) string {
  1029  	if len(b) < 32 {
  1030  		return string(b)
  1031  	}
  1032  	return string(b[:32]) + "..."
  1033  }
  1034  
  1035  func intStringsAreSorted(i0, i1 int64) bool {
  1036  	var b0, b1 [32]byte
  1037  	return string(strconv.AppendInt(b0[:0], i0, 10)) < string(strconv.AppendInt(b1[:0], i1, 10))
  1038  }
  1039  
  1040  func uintStringsAreSorted(u0, u1 uint64) bool {
  1041  	var b0, b1 [32]byte
  1042  	return string(strconv.AppendUint(b0[:0], u0, 10)) < string(strconv.AppendUint(b1[:0], u1, 10))
  1043  }
  1044  
  1045  func stringToBytes(s string) []byte {
  1046  	return *(*[]byte)(unsafe.Pointer(&sliceHeader{
  1047  		Data: *(*unsafe.Pointer)(unsafe.Pointer(&s)),
  1048  		Len:  len(s),
  1049  		Cap:  len(s),
  1050  	}))
  1051  }
  1052  
  1053  type sliceHeader struct {
  1054  	Data unsafe.Pointer
  1055  	Len  int
  1056  	Cap  int
  1057  }
  1058  
  1059  var (
  1060  	nullType = reflect.TypeOf(nil)
  1061  	boolType = reflect.TypeOf(false)
  1062  
  1063  	intType   = reflect.TypeOf(int(0))
  1064  	int8Type  = reflect.TypeOf(int8(0))
  1065  	int16Type = reflect.TypeOf(int16(0))
  1066  	int32Type = reflect.TypeOf(int32(0))
  1067  	int64Type = reflect.TypeOf(int64(0))
  1068  
  1069  	uintType    = reflect.TypeOf(uint(0))
  1070  	uint8Type   = reflect.TypeOf(uint8(0))
  1071  	uint16Type  = reflect.TypeOf(uint16(0))
  1072  	uint32Type  = reflect.TypeOf(uint32(0))
  1073  	uint64Type  = reflect.TypeOf(uint64(0))
  1074  	uintptrType = reflect.TypeOf(uintptr(0))
  1075  
  1076  	float32Type = reflect.TypeOf(float32(0))
  1077  	float64Type = reflect.TypeOf(float64(0))
  1078  
  1079  	numberType     = reflect.TypeOf(json.Number(""))
  1080  	stringType     = reflect.TypeOf("")
  1081  	stringsType    = reflect.TypeOf([]string(nil))
  1082  	bytesType      = reflect.TypeOf(([]byte)(nil))
  1083  	durationType   = reflect.TypeOf(time.Duration(0))
  1084  	timeType       = reflect.TypeOf(time.Time{})
  1085  	rawMessageType = reflect.TypeOf(RawMessage(nil))
  1086  
  1087  	numberPtrType     = reflect.PtrTo(numberType)
  1088  	durationPtrType   = reflect.PtrTo(durationType)
  1089  	timePtrType       = reflect.PtrTo(timeType)
  1090  	rawMessagePtrType = reflect.PtrTo(rawMessageType)
  1091  
  1092  	sliceInterfaceType       = reflect.TypeOf(([]interface{})(nil))
  1093  	sliceStringType          = reflect.TypeOf(([]interface{})(nil))
  1094  	mapStringInterfaceType   = reflect.TypeOf((map[string]interface{})(nil))
  1095  	mapStringRawMessageType  = reflect.TypeOf((map[string]RawMessage)(nil))
  1096  	mapStringStringType      = reflect.TypeOf((map[string]string)(nil))
  1097  	mapStringStringSliceType = reflect.TypeOf((map[string][]string)(nil))
  1098  	mapStringBoolType        = reflect.TypeOf((map[string]bool)(nil))
  1099  
  1100  	interfaceType       = reflect.TypeOf((*interface{})(nil)).Elem()
  1101  	jsonMarshalerType   = reflect.TypeOf((*Marshaler)(nil)).Elem()
  1102  	jsonUnmarshalerType = reflect.TypeOf((*Unmarshaler)(nil)).Elem()
  1103  	textMarshalerType   = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
  1104  	textUnmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
  1105  )
  1106  
  1107  // =============================================================================
  1108  // Copyright 2009 The Go Authors. All rights reserved.
  1109  // Use of this source code is governed by a BSD-style
  1110  // license that can be found in the LICENSE file.
  1111  
  1112  // appendDuration appends a human-readable representation of d to b.
  1113  //
  1114  // The function copies the implementation of time.Duration.String but prevents
  1115  // Go from making a dynamic memory allocation on the returned value.
  1116  func appendDuration(b []byte, d time.Duration) []byte {
  1117  	// Largest time is 2540400h10m10.000000000s
  1118  	var buf [32]byte
  1119  	w := len(buf)
  1120  
  1121  	u := uint64(d)
  1122  	neg := d < 0
  1123  	if neg {
  1124  		u = -u
  1125  	}
  1126  
  1127  	if u < uint64(time.Second) {
  1128  		// Special case: if duration is smaller than a second,
  1129  		// use smaller units, like 1.2ms
  1130  		var prec int
  1131  		w--
  1132  		buf[w] = 's'
  1133  		w--
  1134  		switch {
  1135  		case u == 0:
  1136  			return append(b, '0', 's')
  1137  		case u < uint64(time.Microsecond):
  1138  			// print nanoseconds
  1139  			prec = 0
  1140  			buf[w] = 'n'
  1141  		case u < uint64(time.Millisecond):
  1142  			// print microseconds
  1143  			prec = 3
  1144  			// U+00B5 'µ' micro sign == 0xC2 0xB5
  1145  			w-- // Need room for two bytes.
  1146  			copy(buf[w:], "µ")
  1147  		default:
  1148  			// print milliseconds
  1149  			prec = 6
  1150  			buf[w] = 'm'
  1151  		}
  1152  		w, u = fmtFrac(buf[:w], u, prec)
  1153  		w = fmtInt(buf[:w], u)
  1154  	} else {
  1155  		w--
  1156  		buf[w] = 's'
  1157  
  1158  		w, u = fmtFrac(buf[:w], u, 9)
  1159  
  1160  		// u is now integer seconds
  1161  		w = fmtInt(buf[:w], u%60)
  1162  		u /= 60
  1163  
  1164  		// u is now integer minutes
  1165  		if u > 0 {
  1166  			w--
  1167  			buf[w] = 'm'
  1168  			w = fmtInt(buf[:w], u%60)
  1169  			u /= 60
  1170  
  1171  			// u is now integer hours
  1172  			// Stop at hours because days can be different lengths.
  1173  			if u > 0 {
  1174  				w--
  1175  				buf[w] = 'h'
  1176  				w = fmtInt(buf[:w], u)
  1177  			}
  1178  		}
  1179  	}
  1180  
  1181  	if neg {
  1182  		w--
  1183  		buf[w] = '-'
  1184  	}
  1185  
  1186  	return append(b, buf[w:]...)
  1187  }
  1188  
  1189  // fmtFrac formats the fraction of v/10**prec (e.g., ".12345") into the
  1190  // tail of buf, omitting trailing zeros.  it omits the decimal
  1191  // point too when the fraction is 0.  It returns the index where the
  1192  // output bytes begin and the value v/10**prec.
  1193  func fmtFrac(buf []byte, v uint64, prec int) (nw int, nv uint64) {
  1194  	// Omit trailing zeros up to and including decimal point.
  1195  	w := len(buf)
  1196  	print := false
  1197  	for i := 0; i < prec; i++ {
  1198  		digit := v % 10
  1199  		print = print || digit != 0
  1200  		if print {
  1201  			w--
  1202  			buf[w] = byte(digit) + '0'
  1203  		}
  1204  		v /= 10
  1205  	}
  1206  	if print {
  1207  		w--
  1208  		buf[w] = '.'
  1209  	}
  1210  	return w, v
  1211  }
  1212  
  1213  // fmtInt formats v into the tail of buf.
  1214  // It returns the index where the output begins.
  1215  func fmtInt(buf []byte, v uint64) int {
  1216  	w := len(buf)
  1217  	if v == 0 {
  1218  		w--
  1219  		buf[w] = '0'
  1220  	} else {
  1221  		for v > 0 {
  1222  			w--
  1223  			buf[w] = byte(v%10) + '0'
  1224  			v /= 10
  1225  		}
  1226  	}
  1227  	return w
  1228  }
  1229  
  1230  // =============================================================================