encode.go

1// Copyright 2010 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// Package json implements encoding and decoding of JSON as defined in
6// RFC 7159. The mapping between JSON and Go values is described
7// in the documentation for the Marshal and Unmarshal functions.
8//
9// See "JSON and Go" for an introduction to this package:
10// https://golang.org/doc/articles/json_and_go.html
11package json
12
13import (
14	"bytes"
15	"encoding"
16	"encoding/base64"
17	"fmt"
18	"math"
19	"reflect"
20	"sort"
21	"strconv"
22	"strings"
23	"sync"
24	"unicode"
25	"unicode/utf8"
26)
27
28// Marshal returns the JSON encoding of v.
29//
30// Marshal traverses the value v recursively.
31// If an encountered value implements the Marshaler interface
32// and is not a nil pointer, Marshal calls its MarshalJSON method
33// to produce JSON. If no MarshalJSON method is present but the
34// value implements encoding.TextMarshaler instead, Marshal calls
35// its MarshalText method and encodes the result as a JSON string.
36// The nil pointer exception is not strictly necessary
37// but mimics a similar, necessary exception in the behavior of
38// UnmarshalJSON.
39//
40// Otherwise, Marshal uses the following type-dependent default encodings:
41//
42// Boolean values encode as JSON booleans.
43//
44// Floating point, integer, and Number values encode as JSON numbers.
45//
46// String values encode as JSON strings coerced to valid UTF-8,
47// replacing invalid bytes with the Unicode replacement rune.
48// So that the JSON will be safe to embed inside HTML <script> tags,
49// the string is encoded using HTMLEscape,
50// which replaces "<", ">", "&", U+2028, and U+2029 are escaped
51// to "\u003c","\u003e", "\u0026", "\u2028", and "\u2029".
52// This replacement can be disabled when using an Encoder,
53// by calling SetEscapeHTML(false).
54//
55// Array and slice values encode as JSON arrays, except that
56// []byte encodes as a base64-encoded string, and a nil slice
57// encodes as the null JSON value.
58//
59// Struct values encode as JSON objects.
60// Each exported struct field becomes a member of the object, using the
61// field name as the object key, unless the field is omitted for one of the
62// reasons given below.
63//
64// The encoding of each struct field can be customized by the format string
65// stored under the "json" key in the struct field's tag.
66// The format string gives the name of the field, possibly followed by a
67// comma-separated list of options. The name may be empty in order to
68// specify options without overriding the default field name.
69//
70// The "omitempty" option specifies that the field should be omitted
71// from the encoding if the field has an empty value, defined as
72// false, 0, a nil pointer, a nil interface value, and any empty array,
73// slice, map, or string.
74//
75// As a special case, if the field tag is "-", the field is always omitted.
76// Note that a field with name "-" can still be generated using the tag "-,".
77//
78// Examples of struct field tags and their meanings:
79//
80//	// Field appears in JSON as key "myName".
81//	Field int `json:"myName"`
82//
83//	// Field appears in JSON as key "myName" and
84//	// the field is omitted from the object if its value is empty,
85//	// as defined above.
86//	Field int `json:"myName,omitempty"`
87//
88//	// Field appears in JSON as key "Field" (the default), but
89//	// the field is skipped if empty.
90//	// Note the leading comma.
91//	Field int `json:",omitempty"`
92//
93//	// Field is ignored by this package.
94//	Field int `json:"-"`
95//
96//	// Field appears in JSON as key "-".
97//	Field int `json:"-,"`
98//
99// The "string" option signals that a field is stored as JSON inside a
100// JSON-encoded string. It applies only to fields of string, floating point,
101// integer, or boolean types. This extra level of encoding is sometimes used
102// when communicating with JavaScript programs:
103//
104//	Int64String int64 `json:",string"`
105//
106// The key name will be used if it's a non-empty string consisting of
107// only Unicode letters, digits, and ASCII punctuation except quotation
108// marks, backslash, and comma.
109//
110// Anonymous struct fields are usually marshaled as if their inner exported fields
111// were fields in the outer struct, subject to the usual Go visibility rules amended
112// as described in the next paragraph.
113// An anonymous struct field with a name given in its JSON tag is treated as
114// having that name, rather than being anonymous.
115// An anonymous struct field of interface type is treated the same as having
116// that type as its name, rather than being anonymous.
117//
118// The Go visibility rules for struct fields are amended for JSON when
119// deciding which field to marshal or unmarshal. If there are
120// multiple fields at the same level, and that level is the least
121// nested (and would therefore be the nesting level selected by the
122// usual Go rules), the following extra rules apply:
123//
124// 1) Of those fields, if any are JSON-tagged, only tagged fields are considered,
125// even if there are multiple untagged fields that would otherwise conflict.
126//
127// 2) If there is exactly one field (tagged or not according to the first rule), that is selected.
128//
129// 3) Otherwise there are multiple fields, and all are ignored; no error occurs.
130//
131// Handling of anonymous struct fields is new in Go 1.1.
132// Prior to Go 1.1, anonymous struct fields were ignored. To force ignoring of
133// an anonymous struct field in both current and earlier versions, give the field
134// a JSON tag of "-".
135//
136// Map values encode as JSON objects. The map's key type must either be a
137// string, an integer type, or implement encoding.TextMarshaler. The map keys
138// are sorted and used as JSON object keys by applying the following rules,
139// subject to the UTF-8 coercion described for string values above:
140//   - keys of any string type are used directly
141//   - encoding.TextMarshalers are marshaled
142//   - integer keys are converted to strings
143//
144// Pointer values encode as the value pointed to.
145// A nil pointer encodes as the null JSON value.
146//
147// Interface values encode as the value contained in the interface.
148// A nil interface value encodes as the null JSON value.
149//
150// Channel, complex, and function values cannot be encoded in JSON.
151// Attempting to encode such a value causes Marshal to return
152// an UnsupportedTypeError.
153//
154// JSON cannot represent cyclic data structures and Marshal does not
155// handle them. Passing cyclic structures to Marshal will result in
156// an error.
157func Marshal(v any) ([]byte, error) {
158	e := newEncodeState()
159
160	err := e.marshal(v, encOpts{escapeHTML: true})
161	if err != nil {
162		return nil, err
163	}
164	buf := append([]byte(nil), e.Bytes()...)
165
166	encodeStatePool.Put(e)
167
168	return buf, nil
169}
170
171// MarshalIndent is like Marshal but applies Indent to format the output.
172// Each JSON element in the output will begin on a new line beginning with prefix
173// followed by one or more copies of indent according to the indentation nesting.
174func MarshalIndent(v any, prefix, indent string) ([]byte, error) {
175	b, err := Marshal(v)
176	if err != nil {
177		return nil, err
178	}
179	var buf bytes.Buffer
180	err = Indent(&buf, b, prefix, indent)
181	if err != nil {
182		return nil, err
183	}
184	return buf.Bytes(), nil
185}
186
187// HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029
188// characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029
189// so that the JSON will be safe to embed inside HTML <script> tags.
190// For historical reasons, web browsers don't honor standard HTML
191// escaping within <script> tags, so an alternative JSON encoding must
192// be used.
193func HTMLEscape(dst *bytes.Buffer, src []byte) {
194	// The characters can only appear in string literals,
195	// so just scan the string one byte at a time.
196	start := 0
197	for i, c := range src {
198		if c == '<' || c == '>' || c == '&' {
199			if start < i {
200				dst.Write(src[start:i])
201			}
202			dst.WriteString(`\u00`)
203			dst.WriteByte(hex[c>>4])
204			dst.WriteByte(hex[c&0xF])
205			start = i + 1
206		}
207		// Convert U+2028 and U+2029 (E2 80 A8 and E2 80 A9).
208		if c == 0xE2 && i+2 < len(src) && src[i+1] == 0x80 && src[i+2]&^1 == 0xA8 {
209			if start < i {
210				dst.Write(src[start:i])
211			}
212			dst.WriteString(`\u202`)
213			dst.WriteByte(hex[src[i+2]&0xF])
214			start = i + 3
215		}
216	}
217	if start < len(src) {
218		dst.Write(src[start:])
219	}
220}
221
222// Marshaler is the interface implemented by types that
223// can marshal themselves into valid JSON.
224type Marshaler interface {
225	MarshalJSON() ([]byte, error)
226}
227
228// An UnsupportedTypeError is returned by Marshal when attempting
229// to encode an unsupported value type.
230type UnsupportedTypeError struct {
231	Type reflect.Type
232}
233
234func (e *UnsupportedTypeError) Error() string {
235	return "json: unsupported type: " + e.Type.String()
236}
237
238// An UnsupportedValueError is returned by Marshal when attempting
239// to encode an unsupported value.
240type UnsupportedValueError struct {
241	Value reflect.Value
242	Str   string
243}
244
245func (e *UnsupportedValueError) Error() string {
246	return "json: unsupported value: " + e.Str
247}
248
249// Before Go 1.2, an InvalidUTF8Error was returned by Marshal when
250// attempting to encode a string value with invalid UTF-8 sequences.
251// As of Go 1.2, Marshal instead coerces the string to valid UTF-8 by
252// replacing invalid bytes with the Unicode replacement rune U+FFFD.
253//
254// Deprecated: No longer used; kept for compatibility.
255type InvalidUTF8Error struct {
256	S string // the whole string value that caused the error
257}
258
259func (e *InvalidUTF8Error) Error() string {
260	return "json: invalid UTF-8 in string: " + strconv.Quote(e.S)
261}
262
263// A MarshalerError represents an error from calling a MarshalJSON or MarshalText method.
264type MarshalerError struct {
265	Type       reflect.Type
266	Err        error
267	sourceFunc string
268}
269
270func (e *MarshalerError) Error() string {
271	srcFunc := e.sourceFunc
272	if srcFunc == "" {
273		srcFunc = "MarshalJSON"
274	}
275	return "json: error calling " + srcFunc +
276		" for type " + e.Type.String() +
277		": " + e.Err.Error()
278}
279
280// Unwrap returns the underlying error.
281func (e *MarshalerError) Unwrap() error { return e.Err }
282
283var hex = "0123456789abcdef"
284
285// An encodeState encodes JSON into a bytes.Buffer.
286type encodeState struct {
287	bytes.Buffer // accumulated output
288	scratch      [64]byte
289
290	// Keep track of what pointers we've seen in the current recursive call
291	// path, to avoid cycles that could lead to a stack overflow. Only do
292	// the relatively expensive map operations if ptrLevel is larger than
293	// startDetectingCyclesAfter, so that we skip the work if we're within a
294	// reasonable amount of nested pointers deep.
295	ptrLevel uint
296	ptrSeen  map[any]struct{}
297}
298
299const startDetectingCyclesAfter = 1000
300
301var encodeStatePool sync.Pool
302
303func newEncodeState() *encodeState {
304	if v := encodeStatePool.Get(); v != nil {
305		e := v.(*encodeState)
306		e.Reset()
307		if len(e.ptrSeen) > 0 {
308			panic("ptrEncoder.encode should have emptied ptrSeen via defers")
309		}
310		e.ptrLevel = 0
311		return e
312	}
313	return &encodeState{ptrSeen: make(map[any]struct{})}
314}
315
316// jsonError is an error wrapper type for internal use only.
317// Panics with errors are wrapped in jsonError so that the top-level recover
318// can distinguish intentional panics from this package.
319type jsonError struct{ error }
320
321func (e *encodeState) marshal(v any, opts encOpts) (err error) {
322	defer func() {
323		if r := recover(); r != nil {
324			if je, ok := r.(jsonError); ok {
325				err = je.error
326			} else {
327				panic(r)
328			}
329		}
330	}()
331	e.reflectValue(reflect.ValueOf(v), opts)
332	return nil
333}
334
335// error aborts the encoding by panicking with err wrapped in jsonError.
336func (e *encodeState) error(err error) {
337	panic(jsonError{err})
338}
339
340func isEmptyValue(v reflect.Value) bool {
341	switch v.Kind() {
342	case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
343		return v.Len() == 0
344	case reflect.Bool:
345		return !v.Bool()
346	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
347		return v.Int() == 0
348	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
349		return v.Uint() == 0
350	case reflect.Float32, reflect.Float64:
351		return v.Float() == 0
352	case reflect.Interface, reflect.Pointer:
353		return v.IsNil()
354	}
355	return false
356}
357
358func (e *encodeState) reflectValue(v reflect.Value, opts encOpts) {
359	valueEncoder(v)(e, v, opts)
360}
361
362type encOpts struct {
363	// quoted causes primitive fields to be encoded inside JSON strings.
364	quoted bool
365	// escapeHTML causes '<', '>', and '&' to be escaped in JSON strings.
366	escapeHTML bool
367}
368
369type encoderFunc func(e *encodeState, v reflect.Value, opts encOpts)
370
371var encoderCache sync.Map // map[reflect.Type]encoderFunc
372
373func valueEncoder(v reflect.Value) encoderFunc {
374	if !v.IsValid() {
375		return invalidValueEncoder
376	}
377	return typeEncoder(v.Type())
378}
379
380func typeEncoder(t reflect.Type) encoderFunc {
381	if fi, ok := encoderCache.Load(t); ok {
382		return fi.(encoderFunc)
383	}
384
385	// To deal with recursive types, populate the map with an
386	// indirect func before we build it. This type waits on the
387	// real func (f) to be ready and then calls it. This indirect
388	// func is only used for recursive types.
389	var (
390		wg sync.WaitGroup
391		f  encoderFunc
392	)
393	wg.Add(1)
394	fi, loaded := encoderCache.LoadOrStore(t, encoderFunc(func(e *encodeState, v reflect.Value, opts encOpts) {
395		wg.Wait()
396		f(e, v, opts)
397	}))
398	if loaded {
399		return fi.(encoderFunc)
400	}
401
402	// Compute the real encoder and replace the indirect func with it.
403	f = newTypeEncoder(t, true)
404	wg.Done()
405	encoderCache.Store(t, f)
406	return f
407}
408
409var (
410	marshalerType     = reflect.TypeOf((*Marshaler)(nil)).Elem()
411	textMarshalerType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
412)
413
414// newTypeEncoder constructs an encoderFunc for a type.
415// The returned encoder only checks CanAddr when allowAddr is true.
416func newTypeEncoder(t reflect.Type, allowAddr bool) encoderFunc {
417	// If we have a non-pointer value whose type implements
418	// Marshaler with a value receiver, then we're better off taking
419	// the address of the value - otherwise we end up with an
420	// allocation as we cast the value to an interface.
421	if t.Kind() != reflect.Pointer && allowAddr && reflect.PointerTo(t).Implements(marshalerType) {
422		return newCondAddrEncoder(addrMarshalerEncoder, newTypeEncoder(t, false))
423	}
424	if t.Implements(marshalerType) {
425		return marshalerEncoder
426	}
427	if t.Kind() != reflect.Pointer && allowAddr && reflect.PointerTo(t).Implements(textMarshalerType) {
428		return newCondAddrEncoder(addrTextMarshalerEncoder, newTypeEncoder(t, false))
429	}
430	if t.Implements(textMarshalerType) {
431		return textMarshalerEncoder
432	}
433
434	switch t.Kind() {
435	case reflect.Bool:
436		return boolEncoder
437	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
438		return intEncoder
439	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
440		return uintEncoder
441	case reflect.Float32:
442		return float32Encoder
443	case reflect.Float64:
444		return float64Encoder
445	case reflect.String:
446		return stringEncoder
447	case reflect.Interface:
448		return interfaceEncoder
449	case reflect.Struct:
450		return newStructEncoder(t)
451	case reflect.Map:
452		return newMapEncoder(t)
453	case reflect.Slice:
454		return newSliceEncoder(t)
455	case reflect.Array:
456		return newArrayEncoder(t)
457	case reflect.Pointer:
458		return newPtrEncoder(t)
459	default:
460		return unsupportedTypeEncoder
461	}
462}
463
464func invalidValueEncoder(e *encodeState, v reflect.Value, _ encOpts) {
465	e.WriteString("null")
466}
467
468func marshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
469	if v.Kind() == reflect.Pointer && v.IsNil() {
470		e.WriteString("null")
471		return
472	}
473	m, ok := v.Interface().(Marshaler)
474	if !ok {
475		e.WriteString("null")
476		return
477	}
478	b, err := m.MarshalJSON()
479	if err == nil {
480		// copy JSON into buffer, checking validity.
481		err = compact(&e.Buffer, b, opts.escapeHTML)
482	}
483	if err != nil {
484		e.error(&MarshalerError{v.Type(), err, "MarshalJSON"})
485	}
486}
487
488func addrMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
489	va := v.Addr()
490	if va.IsNil() {
491		e.WriteString("null")
492		return
493	}
494	m := va.Interface().(Marshaler)
495	b, err := m.MarshalJSON()
496	if err == nil {
497		// copy JSON into buffer, checking validity.
498		err = compact(&e.Buffer, b, opts.escapeHTML)
499	}
500	if err != nil {
501		e.error(&MarshalerError{v.Type(), err, "MarshalJSON"})
502	}
503}
504
505func textMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
506	if v.Kind() == reflect.Pointer && v.IsNil() {
507		e.WriteString("null")
508		return
509	}
510	m, ok := v.Interface().(encoding.TextMarshaler)
511	if !ok {
512		e.WriteString("null")
513		return
514	}
515	b, err := m.MarshalText()
516	if err != nil {
517		e.error(&MarshalerError{v.Type(), err, "MarshalText"})
518	}
519	e.stringBytes(b, opts.escapeHTML)
520}
521
522func addrTextMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
523	va := v.Addr()
524	if va.IsNil() {
525		e.WriteString("null")
526		return
527	}
528	m := va.Interface().(encoding.TextMarshaler)
529	b, err := m.MarshalText()
530	if err != nil {
531		e.error(&MarshalerError{v.Type(), err, "MarshalText"})
532	}
533	e.stringBytes(b, opts.escapeHTML)
534}
535
536func boolEncoder(e *encodeState, v reflect.Value, opts encOpts) {
537	if opts.quoted {
538		e.WriteByte('"')
539	}
540	if v.Bool() {
541		e.WriteString("true")
542	} else {
543		e.WriteString("false")
544	}
545	if opts.quoted {
546		e.WriteByte('"')
547	}
548}
549
550func intEncoder(e *encodeState, v reflect.Value, opts encOpts) {
551	b := strconv.AppendInt(e.scratch[:0], v.Int(), 10)
552	if opts.quoted {
553		e.WriteByte('"')
554	}
555	e.Write(b)
556	if opts.quoted {
557		e.WriteByte('"')
558	}
559}
560
561func uintEncoder(e *encodeState, v reflect.Value, opts encOpts) {
562	b := strconv.AppendUint(e.scratch[:0], v.Uint(), 10)
563	if opts.quoted {
564		e.WriteByte('"')
565	}
566	e.Write(b)
567	if opts.quoted {
568		e.WriteByte('"')
569	}
570}
571
572type floatEncoder int // number of bits
573
574func (bits floatEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
575	f := v.Float()
576	if math.IsInf(f, 0) || math.IsNaN(f) {
577		e.error(&UnsupportedValueError{v, strconv.FormatFloat(f, 'g', -1, int(bits))})
578	}
579
580	// Convert as if by ES6 number to string conversion.
581	// This matches most other JSON generators.
582	// See golang.org/issue/6384 and golang.org/issue/14135.
583	// Like fmt %g, but the exponent cutoffs are different
584	// and exponents themselves are not padded to two digits.
585	b := e.scratch[:0]
586	abs := math.Abs(f)
587	fmt := byte('f')
588	// Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right.
589	if abs != 0 {
590		if bits == 64 && (abs < 1e-6 || abs >= 1e21) || bits == 32 && (float32(abs) < 1e-6 || float32(abs) >= 1e21) {
591			fmt = 'e'
592		}
593	}
594	b = strconv.AppendFloat(b, f, fmt, -1, int(bits))
595	if fmt == 'e' {
596		// clean up e-09 to e-9
597		n := len(b)
598		if n >= 4 && b[n-4] == 'e' && b[n-3] == '-' && b[n-2] == '0' {
599			b[n-2] = b[n-1]
600			b = b[:n-1]
601		}
602	}
603
604	if opts.quoted {
605		e.WriteByte('"')
606	}
607	e.Write(b)
608	if opts.quoted {
609		e.WriteByte('"')
610	}
611}
612
613var (
614	float32Encoder = (floatEncoder(32)).encode
615	float64Encoder = (floatEncoder(64)).encode
616)
617
618func stringEncoder(e *encodeState, v reflect.Value, opts encOpts) {
619	if v.Type() == numberType {
620		numStr := v.String()
621		// In Go1.5 the empty string encodes to "0", while this is not a valid number literal
622		// we keep compatibility so check validity after this.
623		if numStr == "" {
624			numStr = "0" // Number's zero-val
625		}
626		if !isValidNumber(numStr) {
627			e.error(fmt.Errorf("json: invalid number literal %q", numStr))
628		}
629		if opts.quoted {
630			e.WriteByte('"')
631		}
632		e.WriteString(numStr)
633		if opts.quoted {
634			e.WriteByte('"')
635		}
636		return
637	}
638	if opts.quoted {
639		e2 := newEncodeState()
640		// Since we encode the string twice, we only need to escape HTML
641		// the first time.
642		e2.string(v.String(), opts.escapeHTML)
643		e.stringBytes(e2.Bytes(), false)
644		encodeStatePool.Put(e2)
645	} else {
646		e.string(v.String(), opts.escapeHTML)
647	}
648}
649
650// isValidNumber reports whether s is a valid JSON number literal.
651func isValidNumber(s string) bool {
652	// This function implements the JSON numbers grammar.
653	// See https://tools.ietf.org/html/rfc7159#section-6
654	// and https://www.json.org/img/number.png
655
656	if s == "" {
657		return false
658	}
659
660	// Optional -
661	if s[0] == '-' {
662		s = s[1:]
663		if s == "" {
664			return false
665		}
666	}
667
668	// Digits
669	switch {
670	default:
671		return false
672
673	case s[0] == '0':
674		s = s[1:]
675
676	case '1' <= s[0] && s[0] <= '9':
677		s = s[1:]
678		for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
679			s = s[1:]
680		}
681	}
682
683	// . followed by 1 or more digits.
684	if len(s) >= 2 && s[0] == '.' && '0' <= s[1] && s[1] <= '9' {
685		s = s[2:]
686		for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
687			s = s[1:]
688		}
689	}
690
691	// e or E followed by an optional - or + and
692	// 1 or more digits.
693	if len(s) >= 2 && (s[0] == 'e' || s[0] == 'E') {
694		s = s[1:]
695		if s[0] == '+' || s[0] == '-' {
696			s = s[1:]
697			if s == "" {
698				return false
699			}
700		}
701		for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
702			s = s[1:]
703		}
704	}
705
706	// Make sure we are at the end.
707	return s == ""
708}
709
710func interfaceEncoder(e *encodeState, v reflect.Value, opts encOpts) {
711	if v.IsNil() {
712		e.WriteString("null")
713		return
714	}
715	e.reflectValue(v.Elem(), opts)
716}
717
718func unsupportedTypeEncoder(e *encodeState, v reflect.Value, _ encOpts) {
719	e.error(&UnsupportedTypeError{v.Type()})
720}
721
722type structEncoder struct {
723	fields structFields
724}
725
726type structFields struct {
727	list      []field
728	nameIndex map[string]int
729}
730
731func (se structEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
732	next := byte('{')
733FieldLoop:
734	for i := range se.fields.list {
735		f := &se.fields.list[i]
736
737		// Find the nested struct field by following f.index.
738		fv := v
739		for _, i := range f.index {
740			if fv.Kind() == reflect.Pointer {
741				if fv.IsNil() {
742					continue FieldLoop
743				}
744				fv = fv.Elem()
745			}
746			fv = fv.Field(i)
747		}
748
749		if f.omitEmpty && isEmptyValue(fv) {
750			continue
751		}
752		e.WriteByte(next)
753		next = ','
754		if opts.escapeHTML {
755			e.WriteString(f.nameEscHTML)
756		} else {
757			e.WriteString(f.nameNonEsc)
758		}
759		opts.quoted = f.quoted
760		f.encoder(e, fv, opts)
761	}
762	if next == '{' {
763		e.WriteString("{}")
764	} else {
765		e.WriteByte('}')
766	}
767}
768
769func newStructEncoder(t reflect.Type) encoderFunc {
770	se := structEncoder{fields: cachedTypeFields(t)}
771	return se.encode
772}
773
774type mapEncoder struct {
775	elemEnc encoderFunc
776}
777
778func (me mapEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
779	if v.IsNil() {
780		e.WriteString("null")
781		return
782	}
783	if e.ptrLevel++; e.ptrLevel > startDetectingCyclesAfter {
784		// We're a large number of nested ptrEncoder.encode calls deep;
785		// start checking if we've run into a pointer cycle.
786		ptr := v.UnsafePointer()
787		if _, ok := e.ptrSeen[ptr]; ok {
788			e.error(&UnsupportedValueError{v, fmt.Sprintf("encountered a cycle via %s", v.Type())})
789		}
790		e.ptrSeen[ptr] = struct{}{}
791		defer delete(e.ptrSeen, ptr)
792	}
793	e.WriteByte('{')
794
795	// Extract and sort the keys.
796	sv := make([]reflectWithString, v.Len())
797	mi := v.MapRange()
798	for i := 0; mi.Next(); i++ {
799		sv[i].k = mi.Key()
800		sv[i].v = mi.Value()
801		if err := sv[i].resolve(); err != nil {
802			e.error(fmt.Errorf("json: encoding error for type %q: %q", v.Type().String(), err.Error()))
803		}
804	}
805	sort.Slice(sv, func(i, j int) bool { return sv[i].ks < sv[j].ks })
806
807	for i, kv := range sv {
808		if i > 0 {
809			e.WriteByte(',')
810		}
811		e.string(kv.ks, opts.escapeHTML)
812		e.WriteByte(':')
813		me.elemEnc(e, kv.v, opts)
814	}
815	e.WriteByte('}')
816	e.ptrLevel--
817}
818
819func newMapEncoder(t reflect.Type) encoderFunc {
820	switch t.Key().Kind() {
821	case reflect.String,
822		reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
823		reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
824	default:
825		if !t.Key().Implements(textMarshalerType) {
826			return unsupportedTypeEncoder
827		}
828	}
829	me := mapEncoder{typeEncoder(t.Elem())}
830	return me.encode
831}
832
833func encodeByteSlice(e *encodeState, v reflect.Value, _ encOpts) {
834	if v.IsNil() {
835		e.WriteString("null")
836		return
837	}
838	s := v.Bytes()
839	e.WriteByte('"')
840	encodedLen := base64.StdEncoding.EncodedLen(len(s))
841	if encodedLen <= len(e.scratch) {
842		// If the encoded bytes fit in e.scratch, avoid an extra
843		// allocation and use the cheaper Encoding.Encode.
844		dst := e.scratch[:encodedLen]
845		base64.StdEncoding.Encode(dst, s)
846		e.Write(dst)
847	} else if encodedLen <= 1024 {
848		// The encoded bytes are short enough to allocate for, and
849		// Encoding.Encode is still cheaper.
850		dst := make([]byte, encodedLen)
851		base64.StdEncoding.Encode(dst, s)
852		e.Write(dst)
853	} else {
854		// The encoded bytes are too long to cheaply allocate, and
855		// Encoding.Encode is no longer noticeably cheaper.
856		enc := base64.NewEncoder(base64.StdEncoding, e)
857		enc.Write(s)
858		enc.Close()
859	}
860	e.WriteByte('"')
861}
862
863// sliceEncoder just wraps an arrayEncoder, checking to make sure the value isn't nil.
864type sliceEncoder struct {
865	arrayEnc encoderFunc
866}
867
868func (se sliceEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
869	if v.IsNil() {
870		e.WriteString("null")
871		return
872	}
873	if e.ptrLevel++; e.ptrLevel > startDetectingCyclesAfter {
874		// We're a large number of nested ptrEncoder.encode calls deep;
875		// start checking if we've run into a pointer cycle.
876		// Here we use a struct to memorize the pointer to the first element of the slice
877		// and its length.
878		ptr := struct {
879			ptr interface{} // always an unsafe.Pointer, but avoids a dependency on package unsafe
880			len int
881		}{v.UnsafePointer(), v.Len()}
882		if _, ok := e.ptrSeen[ptr]; ok {
883			e.error(&UnsupportedValueError{v, fmt.Sprintf("encountered a cycle via %s", v.Type())})
884		}
885		e.ptrSeen[ptr] = struct{}{}
886		defer delete(e.ptrSeen, ptr)
887	}
888	se.arrayEnc(e, v, opts)
889	e.ptrLevel--
890}
891
892func newSliceEncoder(t reflect.Type) encoderFunc {
893	// Byte slices get special treatment; arrays don't.
894	if t.Elem().Kind() == reflect.Uint8 {
895		p := reflect.PointerTo(t.Elem())
896		if !p.Implements(marshalerType) && !p.Implements(textMarshalerType) {
897			return encodeByteSlice
898		}
899	}
900	enc := sliceEncoder{newArrayEncoder(t)}
901	return enc.encode
902}
903
904type arrayEncoder struct {
905	elemEnc encoderFunc
906}
907
908func (ae arrayEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
909	e.WriteByte('[')
910	n := v.Len()
911	for i := 0; i < n; i++ {
912		if i > 0 {
913			e.WriteByte(',')
914		}
915		ae.elemEnc(e, v.Index(i), opts)
916	}
917	e.WriteByte(']')
918}
919
920func newArrayEncoder(t reflect.Type) encoderFunc {
921	enc := arrayEncoder{typeEncoder(t.Elem())}
922	return enc.encode
923}
924
925type ptrEncoder struct {
926	elemEnc encoderFunc
927}
928
929func (pe ptrEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
930	if v.IsNil() {
931		e.WriteString("null")
932		return
933	}
934	if e.ptrLevel++; e.ptrLevel > startDetectingCyclesAfter {
935		// We're a large number of nested ptrEncoder.encode calls deep;
936		// start checking if we've run into a pointer cycle.
937		ptr := v.Interface()
938		if _, ok := e.ptrSeen[ptr]; ok {
939			e.error(&UnsupportedValueError{v, fmt.Sprintf("encountered a cycle via %s", v.Type())})
940		}
941		e.ptrSeen[ptr] = struct{}{}
942		defer delete(e.ptrSeen, ptr)
943	}
944	pe.elemEnc(e, v.Elem(), opts)
945	e.ptrLevel--
946}
947
948func newPtrEncoder(t reflect.Type) encoderFunc {
949	enc := ptrEncoder{typeEncoder(t.Elem())}
950	return enc.encode
951}
952
953type condAddrEncoder struct {
954	canAddrEnc, elseEnc encoderFunc
955}
956
957func (ce condAddrEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
958	if v.CanAddr() {
959		ce.canAddrEnc(e, v, opts)
960	} else {
961		ce.elseEnc(e, v, opts)
962	}
963}
964
965// newCondAddrEncoder returns an encoder that checks whether its value
966// CanAddr and delegates to canAddrEnc if so, else to elseEnc.
967func newCondAddrEncoder(canAddrEnc, elseEnc encoderFunc) encoderFunc {
968	enc := condAddrEncoder{canAddrEnc: canAddrEnc, elseEnc: elseEnc}
969	return enc.encode
970}
971
972func isValidTag(s string) bool {
973	if s == "" {
974		return false
975	}
976	for _, c := range s {
977		switch {
978		case strings.ContainsRune("!#$%&()*+-./:;<=>?@[]^_{|}~ ", c):
979			// Backslash and quote chars are reserved, but
980			// otherwise any punctuation chars are allowed
981			// in a tag name.
982		case !unicode.IsLetter(c) && !unicode.IsDigit(c):
983			return false
984		}
985	}
986	return true
987}
988
989func typeByIndex(t reflect.Type, index []int) reflect.Type {
990	for _, i := range index {
991		if t.Kind() == reflect.Pointer {
992			t = t.Elem()
993		}
994		t = t.Field(i).Type
995	}
996	return t
997}
998
999type reflectWithString struct {
1000	k  reflect.Value
1001	v  reflect.Value
1002	ks string
1003}
1004
1005func (w *reflectWithString) resolve() error {
1006	if w.k.Kind() == reflect.String {
1007		w.ks = w.k.String()
1008		return nil
1009	}
1010	if tm, ok := w.k.Interface().(encoding.TextMarshaler); ok {
1011		if w.k.Kind() == reflect.Pointer && w.k.IsNil() {
1012			return nil
1013		}
1014		buf, err := tm.MarshalText()
1015		w.ks = string(buf)
1016		return err
1017	}
1018	switch w.k.Kind() {
1019	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
1020		w.ks = strconv.FormatInt(w.k.Int(), 10)
1021		return nil
1022	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
1023		w.ks = strconv.FormatUint(w.k.Uint(), 10)
1024		return nil
1025	}
1026	panic("unexpected map key type")
1027}
1028
1029// NOTE: keep in sync with stringBytes below.
1030func (e *encodeState) string(s string, escapeHTML bool) {
1031	e.WriteByte('"')
1032	start := 0
1033	for i := 0; i < len(s); {
1034		if b := s[i]; b < utf8.RuneSelf {
1035			if htmlSafeSet[b] || (!escapeHTML && safeSet[b]) {
1036				i++
1037				continue
1038			}
1039			if start < i {
1040				e.WriteString(s[start:i])
1041			}
1042			e.WriteByte('\\')
1043			switch b {
1044			case '\\', '"':
1045				e.WriteByte(b)
1046			case '\n':
1047				e.WriteByte('n')
1048			case '\r':
1049				e.WriteByte('r')
1050			case '\t':
1051				e.WriteByte('t')
1052			default:
1053				// This encodes bytes < 0x20 except for \t, \n and \r.
1054				// If escapeHTML is set, it also escapes <, >, and &
1055				// because they can lead to security holes when
1056				// user-controlled strings are rendered into JSON
1057				// and served to some browsers.
1058				e.WriteString(`u00`)
1059				e.WriteByte(hex[b>>4])
1060				e.WriteByte(hex[b&0xF])
1061			}
1062			i++
1063			start = i
1064			continue
1065		}
1066		c, size := utf8.DecodeRuneInString(s[i:])
1067		if c == utf8.RuneError && size == 1 {
1068			if start < i {
1069				e.WriteString(s[start:i])
1070			}
1071			e.WriteString(`\ufffd`)
1072			i += size
1073			start = i
1074			continue
1075		}
1076		// U+2028 is LINE SEPARATOR.
1077		// U+2029 is PARAGRAPH SEPARATOR.
1078		// They are both technically valid characters in JSON strings,
1079		// but don't work in JSONP, which has to be evaluated as JavaScript,
1080		// and can lead to security holes there. It is valid JSON to
1081		// escape them, so we do so unconditionally.
1082		// See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion.
1083		if c == '\u2028' || c == '\u2029' {
1084			if start < i {
1085				e.WriteString(s[start:i])
1086			}
1087			e.WriteString(`\u202`)
1088			e.WriteByte(hex[c&0xF])
1089			i += size
1090			start = i
1091			continue
1092		}
1093		i += size
1094	}
1095	if start < len(s) {
1096		e.WriteString(s[start:])
1097	}
1098	e.WriteByte('"')
1099}
1100
1101// NOTE: keep in sync with string above.
1102func (e *encodeState) stringBytes(s []byte, escapeHTML bool) {
1103	e.WriteByte('"')
1104	start := 0
1105	for i := 0; i < len(s); {
1106		if b := s[i]; b < utf8.RuneSelf {
1107			if htmlSafeSet[b] || (!escapeHTML && safeSet[b]) {
1108				i++
1109				continue
1110			}
1111			if start < i {
1112				e.Write(s[start:i])
1113			}
1114			e.WriteByte('\\')
1115			switch b {
1116			case '\\', '"':
1117				e.WriteByte(b)
1118			case '\n':
1119				e.WriteByte('n')
1120			case '\r':
1121				e.WriteByte('r')
1122			case '\t':
1123				e.WriteByte('t')
1124			default:
1125				// This encodes bytes < 0x20 except for \t, \n and \r.
1126				// If escapeHTML is set, it also escapes <, >, and &
1127				// because they can lead to security holes when
1128				// user-controlled strings are rendered into JSON
1129				// and served to some browsers.
1130				e.WriteString(`u00`)
1131				e.WriteByte(hex[b>>4])
1132				e.WriteByte(hex[b&0xF])
1133			}
1134			i++
1135			start = i
1136			continue
1137		}
1138		c, size := utf8.DecodeRune(s[i:])
1139		if c == utf8.RuneError && size == 1 {
1140			if start < i {
1141				e.Write(s[start:i])
1142			}
1143			e.WriteString(`\ufffd`)
1144			i += size
1145			start = i
1146			continue
1147		}
1148		// U+2028 is LINE SEPARATOR.
1149		// U+2029 is PARAGRAPH SEPARATOR.
1150		// They are both technically valid characters in JSON strings,
1151		// but don't work in JSONP, which has to be evaluated as JavaScript,
1152		// and can lead to security holes there. It is valid JSON to
1153		// escape them, so we do so unconditionally.
1154		// See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion.
1155		if c == '\u2028' || c == '\u2029' {
1156			if start < i {
1157				e.Write(s[start:i])
1158			}
1159			e.WriteString(`\u202`)
1160			e.WriteByte(hex[c&0xF])
1161			i += size
1162			start = i
1163			continue
1164		}
1165		i += size
1166	}
1167	if start < len(s) {
1168		e.Write(s[start:])
1169	}
1170	e.WriteByte('"')
1171}
1172
1173// A field represents a single field found in a struct.
1174type field struct {
1175	name      string
1176	nameBytes []byte                 // []byte(name)
1177	equalFold func(s, t []byte) bool // bytes.EqualFold or equivalent
1178
1179	nameNonEsc  string // `"` + name + `":`
1180	nameEscHTML string // `"` + HTMLEscape(name) + `":`
1181
1182	tag       bool
1183	index     []int
1184	typ       reflect.Type
1185	omitEmpty bool
1186	quoted    bool
1187
1188	encoder encoderFunc
1189}
1190
1191// byIndex sorts field by index sequence.
1192type byIndex []field
1193
1194func (x byIndex) Len() int { return len(x) }
1195
1196func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
1197
1198func (x byIndex) Less(i, j int) bool {
1199	for k, xik := range x[i].index {
1200		if k >= len(x[j].index) {
1201			return false
1202		}
1203		if xik != x[j].index[k] {
1204			return xik < x[j].index[k]
1205		}
1206	}
1207	return len(x[i].index) < len(x[j].index)
1208}
1209
1210// typeFields returns a list of fields that JSON should recognize for the given type.
1211// The algorithm is breadth-first search over the set of structs to include - the top struct
1212// and then any reachable anonymous structs.
1213func typeFields(t reflect.Type) structFields {
1214	// Anonymous fields to explore at the current level and the next.
1215	current := []field{}
1216	next := []field{{typ: t}}
1217
1218	// Count of queued names for current level and the next.
1219	var count, nextCount map[reflect.Type]int
1220
1221	// Types already visited at an earlier level.
1222	visited := map[reflect.Type]bool{}
1223
1224	// Fields found.
1225	var fields []field
1226
1227	// Buffer to run HTMLEscape on field names.
1228	var nameEscBuf bytes.Buffer
1229
1230	for len(next) > 0 {
1231		current, next = next, current[:0]
1232		count, nextCount = nextCount, map[reflect.Type]int{}
1233
1234		for _, f := range current {
1235			if visited[f.typ] {
1236				continue
1237			}
1238			visited[f.typ] = true
1239
1240			// Scan f.typ for fields to include.
1241			for i := 0; i < f.typ.NumField(); i++ {
1242				sf := f.typ.Field(i)
1243				if sf.Anonymous {
1244					t := sf.Type
1245					if t.Kind() == reflect.Pointer {
1246						t = t.Elem()
1247					}
1248					if !sf.IsExported() && t.Kind() != reflect.Struct {
1249						// Ignore embedded fields of unexported non-struct types.
1250						continue
1251					}
1252					// Do not ignore embedded fields of unexported struct types
1253					// since they may have exported fields.
1254				} else if !sf.IsExported() {
1255					// Ignore unexported non-embedded fields.
1256					continue
1257				}
1258				tag := sf.Tag.Get("json")
1259				if tag == "-" {
1260					continue
1261				}
1262				name, opts := parseTag(tag)
1263				if !isValidTag(name) {
1264					name = ""
1265				}
1266				index := make([]int, len(f.index)+1)
1267				copy(index, f.index)
1268				index[len(f.index)] = i
1269
1270				ft := sf.Type
1271				if ft.Name() == "" && ft.Kind() == reflect.Pointer {
1272					// Follow pointer.
1273					ft = ft.Elem()
1274				}
1275
1276				// Only strings, floats, integers, and booleans can be quoted.
1277				quoted := false
1278				if opts.Contains("string") {
1279					switch ft.Kind() {
1280					case reflect.Bool,
1281						reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
1282						reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
1283						reflect.Float32, reflect.Float64,
1284						reflect.String:
1285						quoted = true
1286					}
1287				}
1288
1289				// Record found field and index sequence.
1290				if name != "" || !sf.Anonymous || ft.Kind() != reflect.Struct {
1291					tagged := name != ""
1292					if name == "" {
1293						name = sf.Name
1294					}
1295					field := field{
1296						name:      name,
1297						tag:       tagged,
1298						index:     index,
1299						typ:       ft,
1300						omitEmpty: opts.Contains("omitempty"),
1301						quoted:    quoted,
1302					}
1303					field.nameBytes = []byte(field.name)
1304					field.equalFold = foldFunc(field.nameBytes)
1305
1306					// Build nameEscHTML and nameNonEsc ahead of time.
1307					nameEscBuf.Reset()
1308					nameEscBuf.WriteString(`"`)
1309					HTMLEscape(&nameEscBuf, field.nameBytes)
1310					nameEscBuf.WriteString(`":`)
1311					field.nameEscHTML = nameEscBuf.String()
1312					field.nameNonEsc = `"` + field.name + `":`
1313
1314					fields = append(fields, field)
1315					if count[f.typ] > 1 {
1316						// If there were multiple instances, add a second,
1317						// so that the annihilation code will see a duplicate.
1318						// It only cares about the distinction between 1 or 2,
1319						// so don't bother generating any more copies.
1320						fields = append(fields, fields[len(fields)-1])
1321					}
1322					continue
1323				}
1324
1325				// Record new anonymous struct to explore in next round.
1326				nextCount[ft]++
1327				if nextCount[ft] == 1 {
1328					next = append(next, field{name: ft.Name(), index: index, typ: ft})
1329				}
1330			}
1331		}
1332	}
1333
1334	sort.Slice(fields, func(i, j int) bool {
1335		x := fields
1336		// sort field by name, breaking ties with depth, then
1337		// breaking ties with "name came from json tag", then
1338		// breaking ties with index sequence.
1339		if x[i].name != x[j].name {
1340			return x[i].name < x[j].name
1341		}
1342		if len(x[i].index) != len(x[j].index) {
1343			return len(x[i].index) < len(x[j].index)
1344		}
1345		if x[i].tag != x[j].tag {
1346			return x[i].tag
1347		}
1348		return byIndex(x).Less(i, j)
1349	})
1350
1351	// Delete all fields that are hidden by the Go rules for embedded fields,
1352	// except that fields with JSON tags are promoted.
1353
1354	// The fields are sorted in primary order of name, secondary order
1355	// of field index length. Loop over names; for each name, delete
1356	// hidden fields by choosing the one dominant field that survives.
1357	out := fields[:0]
1358	for advance, i := 0, 0; i < len(fields); i += advance {
1359		// One iteration per name.
1360		// Find the sequence of fields with the name of this first field.
1361		fi := fields[i]
1362		name := fi.name
1363		for advance = 1; i+advance < len(fields); advance++ {
1364			fj := fields[i+advance]
1365			if fj.name != name {
1366				break
1367			}
1368		}
1369		if advance == 1 { // Only one field with this name
1370			out = append(out, fi)
1371			continue
1372		}
1373		dominant, ok := dominantField(fields[i : i+advance])
1374		if ok {
1375			out = append(out, dominant)
1376		}
1377	}
1378
1379	fields = out
1380	sort.Sort(byIndex(fields))
1381
1382	for i := range fields {
1383		f := &fields[i]
1384		f.encoder = typeEncoder(typeByIndex(t, f.index))
1385	}
1386	nameIndex := make(map[string]int, len(fields))
1387	for i, field := range fields {
1388		nameIndex[field.name] = i
1389	}
1390	return structFields{fields, nameIndex}
1391}
1392
1393// dominantField looks through the fields, all of which are known to
1394// have the same name, to find the single field that dominates the
1395// others using Go's embedding rules, modified by the presence of
1396// JSON tags. If there are multiple top-level fields, the boolean
1397// will be false: This condition is an error in Go and we skip all
1398// the fields.
1399func dominantField(fields []field) (field, bool) {
1400	// The fields are sorted in increasing index-length order, then by presence of tag.
1401	// That means that the first field is the dominant one. We need only check
1402	// for error cases: two fields at top level, either both tagged or neither tagged.
1403	if len(fields) > 1 && len(fields[0].index) == len(fields[1].index) && fields[0].tag == fields[1].tag {
1404		return field{}, false
1405	}
1406	return fields[0], true
1407}
1408
1409var fieldCache sync.Map // map[reflect.Type]structFields
1410
1411// cachedTypeFields is like typeFields but uses a cache to avoid repeated work.
1412func cachedTypeFields(t reflect.Type) structFields {
1413	if f, ok := fieldCache.Load(t); ok {
1414		return f.(structFields)
1415	}
1416	f, _ := fieldCache.LoadOrStore(t, typeFields(t))
1417	return f.(structFields)
1418}