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.
157//
158func Marshal(v interface{}) ([]byte, error) {
159	e := newEncodeState()
160
161	err := e.marshal(v, encOpts{escapeHTML: true})
162	if err != nil {
163		return nil, err
164	}
165	buf := append([]byte(nil), e.Bytes()...)
166
167	encodeStatePool.Put(e)
168
169	return buf, nil
170}
171
172// MarshalIndent is like Marshal but applies Indent to format the output.
173// Each JSON element in the output will begin on a new line beginning with prefix
174// followed by one or more copies of indent according to the indentation nesting.
175func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
176	b, err := Marshal(v)
177	if err != nil {
178		return nil, err
179	}
180	var buf bytes.Buffer
181	err = Indent(&buf, b, prefix, indent)
182	if err != nil {
183		return nil, err
184	}
185	return buf.Bytes(), nil
186}
187
188// HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029
189// characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029
190// so that the JSON will be safe to embed inside HTML <script> tags.
191// For historical reasons, web browsers don't honor standard HTML
192// escaping within <script> tags, so an alternative JSON encoding must
193// be used.
194func HTMLEscape(dst *bytes.Buffer, src []byte) {
195	// The characters can only appear in string literals,
196	// so just scan the string one byte at a time.
197	start := 0
198	for i, c := range src {
199		if c == '<' || c == '>' || c == '&' {
200			if start < i {
201				dst.Write(src[start:i])
202			}
203			dst.WriteString(`\u00`)
204			dst.WriteByte(hex[c>>4])
205			dst.WriteByte(hex[c&0xF])
206			start = i + 1
207		}
208		// Convert U+2028 and U+2029 (E2 80 A8 and E2 80 A9).
209		if c == 0xE2 && i+2 < len(src) && src[i+1] == 0x80 && src[i+2]&^1 == 0xA8 {
210			if start < i {
211				dst.Write(src[start:i])
212			}
213			dst.WriteString(`\u202`)
214			dst.WriteByte(hex[src[i+2]&0xF])
215			start = i + 3
216		}
217	}
218	if start < len(src) {
219		dst.Write(src[start:])
220	}
221}
222
223// Marshaler is the interface implemented by types that
224// can marshal themselves into valid JSON.
225type Marshaler interface {
226	MarshalJSON() ([]byte, error)
227}
228
229// An UnsupportedTypeError is returned by Marshal when attempting
230// to encode an unsupported value type.
231type UnsupportedTypeError struct {
232	Type reflect.Type
233}
234
235func (e *UnsupportedTypeError) Error() string {
236	return "json: unsupported type: " + e.Type.String()
237}
238
239// An UnsupportedValueError is returned by Marshal when attempting
240// to encode an unsupported value.
241type UnsupportedValueError struct {
242	Value reflect.Value
243	Str   string
244}
245
246func (e *UnsupportedValueError) Error() string {
247	return "json: unsupported value: " + e.Str
248}
249
250// Before Go 1.2, an InvalidUTF8Error was returned by Marshal when
251// attempting to encode a string value with invalid UTF-8 sequences.
252// As of Go 1.2, Marshal instead coerces the string to valid UTF-8 by
253// replacing invalid bytes with the Unicode replacement rune U+FFFD.
254//
255// Deprecated: No longer used; kept for compatibility.
256type InvalidUTF8Error struct {
257	S string // the whole string value that caused the error
258}
259
260func (e *InvalidUTF8Error) Error() string {
261	return "json: invalid UTF-8 in string: " + strconv.Quote(e.S)
262}
263
264// A MarshalerError represents an error from calling a MarshalJSON or MarshalText method.
265type MarshalerError struct {
266	Type       reflect.Type
267	Err        error
268	sourceFunc string
269}
270
271func (e *MarshalerError) Error() string {
272	srcFunc := e.sourceFunc
273	if srcFunc == "" {
274		srcFunc = "MarshalJSON"
275	}
276	return "json: error calling " + srcFunc +
277		" for type " + e.Type.String() +
278		": " + e.Err.Error()
279}
280
281// Unwrap returns the underlying error.
282func (e *MarshalerError) Unwrap() error { return e.Err }
283
284var hex = "0123456789abcdef"
285
286// An encodeState encodes JSON into a bytes.Buffer.
287type encodeState struct {
288	bytes.Buffer // accumulated output
289	scratch      [64]byte
290
291	// Keep track of what pointers we've seen in the current recursive call
292	// path, to avoid cycles that could lead to a stack overflow. Only do
293	// the relatively expensive map operations if ptrLevel is larger than
294	// startDetectingCyclesAfter, so that we skip the work if we're within a
295	// reasonable amount of nested pointers deep.
296	ptrLevel uint
297	ptrSeen  map[interface{}]struct{}
298}
299
300const startDetectingCyclesAfter = 1000
301
302var encodeStatePool sync.Pool
303
304func newEncodeState() *encodeState {
305	if v := encodeStatePool.Get(); v != nil {
306		e := v.(*encodeState)
307		e.Reset()
308		if len(e.ptrSeen) > 0 {
309			panic("ptrEncoder.encode should have emptied ptrSeen via defers")
310		}
311		e.ptrLevel = 0
312		return e
313	}
314	return &encodeState{ptrSeen: make(map[interface{}]struct{})}
315}
316
317// jsonError is an error wrapper type for internal use only.
318// Panics with errors are wrapped in jsonError so that the top-level recover
319// can distinguish intentional panics from this package.
320type jsonError struct{ error }
321
322func (e *encodeState) marshal(v interface{}, opts encOpts) (err error) {
323	defer func() {
324		if r := recover(); r != nil {
325			if je, ok := r.(jsonError); ok {
326				err = je.error
327			} else {
328				panic(r)
329			}
330		}
331	}()
332	e.reflectValue(reflect.ValueOf(v), opts)
333	return nil
334}
335
336// error aborts the encoding by panicking with err wrapped in jsonError.
337func (e *encodeState) error(err error) {
338	panic(jsonError{err})
339}
340
341func isEmptyValue(v reflect.Value) bool {
342	switch v.Kind() {
343	case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
344		return v.Len() == 0
345	case reflect.Bool:
346		return !v.Bool()
347	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
348		return v.Int() == 0
349	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
350		return v.Uint() == 0
351	case reflect.Float32, reflect.Float64:
352		return v.Float() == 0
353	case reflect.Interface, reflect.Ptr:
354		return v.IsNil()
355	}
356	return false
357}
358
359func (e *encodeState) reflectValue(v reflect.Value, opts encOpts) {
360	valueEncoder(v)(e, v, opts)
361}
362
363type encOpts struct {
364	// quoted causes primitive fields to be encoded inside JSON strings.
365	quoted bool
366	// escapeHTML causes '<', '>', and '&' to be escaped in JSON strings.
367	escapeHTML bool
368}
369
370type encoderFunc func(e *encodeState, v reflect.Value, opts encOpts)
371
372var encoderCache sync.Map // map[reflect.Type]encoderFunc
373
374func valueEncoder(v reflect.Value) encoderFunc {
375	if !v.IsValid() {
376		return invalidValueEncoder
377	}
378	return typeEncoder(v.Type())
379}
380
381func typeEncoder(t reflect.Type) encoderFunc {
382	if fi, ok := encoderCache.Load(t); ok {
383		return fi.(encoderFunc)
384	}
385
386	// To deal with recursive types, populate the map with an
387	// indirect func before we build it. This type waits on the
388	// real func (f) to be ready and then calls it. This indirect
389	// func is only used for recursive types.
390	var (
391		wg sync.WaitGroup
392		f  encoderFunc
393	)
394	wg.Add(1)
395	fi, loaded := encoderCache.LoadOrStore(t, encoderFunc(func(e *encodeState, v reflect.Value, opts encOpts) {
396		wg.Wait()
397		f(e, v, opts)
398	}))
399	if loaded {
400		return fi.(encoderFunc)
401	}
402
403	// Compute the real encoder and replace the indirect func with it.
404	f = newTypeEncoder(t, true)
405	wg.Done()
406	encoderCache.Store(t, f)
407	return f
408}
409
410var (
411	marshalerType     = reflect.TypeOf((*Marshaler)(nil)).Elem()
412	textMarshalerType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
413)
414
415// newTypeEncoder constructs an encoderFunc for a type.
416// The returned encoder only checks CanAddr when allowAddr is true.
417func newTypeEncoder(t reflect.Type, allowAddr bool) encoderFunc {
418	// If we have a non-pointer value whose type implements
419	// Marshaler with a value receiver, then we're better off taking
420	// the address of the value - otherwise we end up with an
421	// allocation as we cast the value to an interface.
422	if t.Kind() != reflect.Ptr && allowAddr && reflect.PtrTo(t).Implements(marshalerType) {
423		return newCondAddrEncoder(addrMarshalerEncoder, newTypeEncoder(t, false))
424	}
425	if t.Implements(marshalerType) {
426		return marshalerEncoder
427	}
428	if t.Kind() != reflect.Ptr && allowAddr && reflect.PtrTo(t).Implements(textMarshalerType) {
429		return newCondAddrEncoder(addrTextMarshalerEncoder, newTypeEncoder(t, false))
430	}
431	if t.Implements(textMarshalerType) {
432		return textMarshalerEncoder
433	}
434
435	switch t.Kind() {
436	case reflect.Bool:
437		return boolEncoder
438	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
439		return intEncoder
440	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
441		return uintEncoder
442	case reflect.Float32:
443		return float32Encoder
444	case reflect.Float64:
445		return float64Encoder
446	case reflect.String:
447		return stringEncoder
448	case reflect.Interface:
449		return interfaceEncoder
450	case reflect.Struct:
451		return newStructEncoder(t)
452	case reflect.Map:
453		return newMapEncoder(t)
454	case reflect.Slice:
455		return newSliceEncoder(t)
456	case reflect.Array:
457		return newArrayEncoder(t)
458	case reflect.Ptr:
459		return newPtrEncoder(t)
460	default:
461		return unsupportedTypeEncoder
462	}
463}
464
465func invalidValueEncoder(e *encodeState, v reflect.Value, _ encOpts) {
466	e.WriteString("null")
467}
468
469func marshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
470	if v.Kind() == reflect.Ptr && v.IsNil() {
471		e.WriteString("null")
472		return
473	}
474	m, ok := v.Interface().(Marshaler)
475	if !ok {
476		e.WriteString("null")
477		return
478	}
479	b, err := m.MarshalJSON()
480	if err == nil {
481		// copy JSON into buffer, checking validity.
482		err = compact(&e.Buffer, b, opts.escapeHTML)
483	}
484	if err != nil {
485		e.error(&MarshalerError{v.Type(), err, "MarshalJSON"})
486	}
487}
488
489func addrMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
490	va := v.Addr()
491	if va.IsNil() {
492		e.WriteString("null")
493		return
494	}
495	m := va.Interface().(Marshaler)
496	b, err := m.MarshalJSON()
497	if err == nil {
498		// copy JSON into buffer, checking validity.
499		err = compact(&e.Buffer, b, opts.escapeHTML)
500	}
501	if err != nil {
502		e.error(&MarshalerError{v.Type(), err, "MarshalJSON"})
503	}
504}
505
506func textMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
507	if v.Kind() == reflect.Ptr && v.IsNil() {
508		e.WriteString("null")
509		return
510	}
511	m, ok := v.Interface().(encoding.TextMarshaler)
512	if !ok {
513		e.WriteString("null")
514		return
515	}
516	b, err := m.MarshalText()
517	if err != nil {
518		e.error(&MarshalerError{v.Type(), err, "MarshalText"})
519	}
520	e.stringBytes(b, opts.escapeHTML)
521}
522
523func addrTextMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
524	va := v.Addr()
525	if va.IsNil() {
526		e.WriteString("null")
527		return
528	}
529	m := va.Interface().(encoding.TextMarshaler)
530	b, err := m.MarshalText()
531	if err != nil {
532		e.error(&MarshalerError{v.Type(), err, "MarshalText"})
533	}
534	e.stringBytes(b, opts.escapeHTML)
535}
536
537func boolEncoder(e *encodeState, v reflect.Value, opts encOpts) {
538	if opts.quoted {
539		e.WriteByte('"')
540	}
541	if v.Bool() {
542		e.WriteString("true")
543	} else {
544		e.WriteString("false")
545	}
546	if opts.quoted {
547		e.WriteByte('"')
548	}
549}
550
551func intEncoder(e *encodeState, v reflect.Value, opts encOpts) {
552	b := strconv.AppendInt(e.scratch[:0], v.Int(), 10)
553	if opts.quoted {
554		e.WriteByte('"')
555	}
556	e.Write(b)
557	if opts.quoted {
558		e.WriteByte('"')
559	}
560}
561
562func uintEncoder(e *encodeState, v reflect.Value, opts encOpts) {
563	b := strconv.AppendUint(e.scratch[:0], v.Uint(), 10)
564	if opts.quoted {
565		e.WriteByte('"')
566	}
567	e.Write(b)
568	if opts.quoted {
569		e.WriteByte('"')
570	}
571}
572
573type floatEncoder int // number of bits
574
575func (bits floatEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
576	f := v.Float()
577	if math.IsInf(f, 0) || math.IsNaN(f) {
578		e.error(&UnsupportedValueError{v, strconv.FormatFloat(f, 'g', -1, int(bits))})
579	}
580
581	// Convert as if by ES6 number to string conversion.
582	// This matches most other JSON generators.
583	// See golang.org/issue/6384 and golang.org/issue/14135.
584	// Like fmt %g, but the exponent cutoffs are different
585	// and exponents themselves are not padded to two digits.
586	b := e.scratch[:0]
587	abs := math.Abs(f)
588	fmt := byte('f')
589	// Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right.
590	if abs != 0 {
591		if bits == 64 && (abs < 1e-6 || abs >= 1e21) || bits == 32 && (float32(abs) < 1e-6 || float32(abs) >= 1e21) {
592			fmt = 'e'
593		}
594	}
595	b = strconv.AppendFloat(b, f, fmt, -1, int(bits))
596	if fmt == 'e' {
597		// clean up e-09 to e-9
598		n := len(b)
599		if n >= 4 && b[n-4] == 'e' && b[n-3] == '-' && b[n-2] == '0' {
600			b[n-2] = b[n-1]
601			b = b[:n-1]
602		}
603	}
604
605	if opts.quoted {
606		e.WriteByte('"')
607	}
608	e.Write(b)
609	if opts.quoted {
610		e.WriteByte('"')
611	}
612}
613
614var (
615	float32Encoder = (floatEncoder(32)).encode
616	float64Encoder = (floatEncoder(64)).encode
617)
618
619func stringEncoder(e *encodeState, v reflect.Value, opts encOpts) {
620	if v.Type() == numberType {
621		numStr := v.String()
622		// In Go1.5 the empty string encodes to "0", while this is not a valid number literal
623		// we keep compatibility so check validity after this.
624		if numStr == "" {
625			numStr = "0" // Number's zero-val
626		}
627		if !isValidNumber(numStr) {
628			e.error(fmt.Errorf("json: invalid number literal %q", numStr))
629		}
630		if opts.quoted {
631			e.WriteByte('"')
632		}
633		e.WriteString(numStr)
634		if opts.quoted {
635			e.WriteByte('"')
636		}
637		return
638	}
639	if opts.quoted {
640		e2 := newEncodeState()
641		// Since we encode the string twice, we only need to escape HTML
642		// the first time.
643		e2.string(v.String(), opts.escapeHTML)
644		e.stringBytes(e2.Bytes(), false)
645		encodeStatePool.Put(e2)
646	} else {
647		e.string(v.String(), opts.escapeHTML)
648	}
649}
650
651// isValidNumber reports whether s is a valid JSON number literal.
652func isValidNumber(s string) bool {
653	// This function implements the JSON numbers grammar.
654	// See https://tools.ietf.org/html/rfc7159#section-6
655	// and https://www.json.org/img/number.png
656
657	if s == "" {
658		return false
659	}
660
661	// Optional -
662	if s[0] == '-' {
663		s = s[1:]
664		if s == "" {
665			return false
666		}
667	}
668
669	// Digits
670	switch {
671	default:
672		return false
673
674	case s[0] == '0':
675		s = s[1:]
676
677	case '1' <= s[0] && s[0] <= '9':
678		s = s[1:]
679		for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
680			s = s[1:]
681		}
682	}
683
684	// . followed by 1 or more digits.
685	if len(s) >= 2 && s[0] == '.' && '0' <= s[1] && s[1] <= '9' {
686		s = s[2:]
687		for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
688			s = s[1:]
689		}
690	}
691
692	// e or E followed by an optional - or + and
693	// 1 or more digits.
694	if len(s) >= 2 && (s[0] == 'e' || s[0] == 'E') {
695		s = s[1:]
696		if s[0] == '+' || s[0] == '-' {
697			s = s[1:]
698			if s == "" {
699				return false
700			}
701		}
702		for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
703			s = s[1:]
704		}
705	}
706
707	// Make sure we are at the end.
708	return s == ""
709}
710
711func interfaceEncoder(e *encodeState, v reflect.Value, opts encOpts) {
712	if v.IsNil() {
713		e.WriteString("null")
714		return
715	}
716	e.reflectValue(v.Elem(), opts)
717}
718
719func unsupportedTypeEncoder(e *encodeState, v reflect.Value, _ encOpts) {
720	e.error(&UnsupportedTypeError{v.Type()})
721}
722
723type structEncoder struct {
724	fields structFields
725}
726
727type structFields struct {
728	list      []field
729	nameIndex map[string]int
730}
731
732func (se structEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
733	next := byte('{')
734FieldLoop:
735	for i := range se.fields.list {
736		f := &se.fields.list[i]
737
738		// Find the nested struct field by following f.index.
739		fv := v
740		for _, i := range f.index {
741			if fv.Kind() == reflect.Ptr {
742				if fv.IsNil() {
743					continue FieldLoop
744				}
745				fv = fv.Elem()
746			}
747			fv = fv.Field(i)
748		}
749
750		if f.omitEmpty && isEmptyValue(fv) {
751			continue
752		}
753		e.WriteByte(next)
754		next = ','
755		if opts.escapeHTML {
756			e.WriteString(f.nameEscHTML)
757		} else {
758			e.WriteString(f.nameNonEsc)
759		}
760		opts.quoted = f.quoted
761		f.encoder(e, fv, opts)
762	}
763	if next == '{' {
764		e.WriteString("{}")
765	} else {
766		e.WriteByte('}')
767	}
768}
769
770func newStructEncoder(t reflect.Type) encoderFunc {
771	se := structEncoder{fields: cachedTypeFields(t)}
772	return se.encode
773}
774
775type mapEncoder struct {
776	elemEnc encoderFunc
777}
778
779func (me mapEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
780	if v.IsNil() {
781		e.WriteString("null")
782		return
783	}
784	if e.ptrLevel++; e.ptrLevel > startDetectingCyclesAfter {
785		// We're a large number of nested ptrEncoder.encode calls deep;
786		// start checking if we've run into a pointer cycle.
787		ptr := v.Pointer()
788		if _, ok := e.ptrSeen[ptr]; ok {
789			e.error(&UnsupportedValueError{v, fmt.Sprintf("encountered a cycle via %s", v.Type())})
790		}
791		e.ptrSeen[ptr] = struct{}{}
792		defer delete(e.ptrSeen, ptr)
793	}
794	e.WriteByte('{')
795
796	// Extract and sort the keys.
797	keys := v.MapKeys()
798	sv := make([]reflectWithString, len(keys))
799	for i, v := range keys {
800		sv[i].v = v
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].s < sv[j].s })
806
807	for i, kv := range sv {
808		if i > 0 {
809			e.WriteByte(',')
810		}
811		e.string(kv.s, opts.escapeHTML)
812		e.WriteByte(':')
813		me.elemEnc(e, v.MapIndex(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 uintptr
880			len int
881		}{v.Pointer(), 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.PtrTo(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.Ptr {
992			t = t.Elem()
993		}
994		t = t.Field(i).Type
995	}
996	return t
997}
998
999type reflectWithString struct {
1000	v reflect.Value
1001	s string
1002}
1003
1004func (w *reflectWithString) resolve() error {
1005	if w.v.Kind() == reflect.String {
1006		w.s = w.v.String()
1007		return nil
1008	}
1009	if tm, ok := w.v.Interface().(encoding.TextMarshaler); ok {
1010		if w.v.Kind() == reflect.Ptr && w.v.IsNil() {
1011			return nil
1012		}
1013		buf, err := tm.MarshalText()
1014		w.s = string(buf)
1015		return err
1016	}
1017	switch w.v.Kind() {
1018	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
1019		w.s = strconv.FormatInt(w.v.Int(), 10)
1020		return nil
1021	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
1022		w.s = strconv.FormatUint(w.v.Uint(), 10)
1023		return nil
1024	}
1025	panic("unexpected map key type")
1026}
1027
1028// NOTE: keep in sync with stringBytes below.
1029func (e *encodeState) string(s string, escapeHTML bool) {
1030	e.WriteByte('"')
1031	start := 0
1032	for i := 0; i < len(s); {
1033		if b := s[i]; b < utf8.RuneSelf {
1034			if htmlSafeSet[b] || (!escapeHTML && safeSet[b]) {
1035				i++
1036				continue
1037			}
1038			if start < i {
1039				e.WriteString(s[start:i])
1040			}
1041			e.WriteByte('\\')
1042			switch b {
1043			case '\\', '"':
1044				e.WriteByte(b)
1045			case '\n':
1046				e.WriteByte('n')
1047			case '\r':
1048				e.WriteByte('r')
1049			case '\t':
1050				e.WriteByte('t')
1051			default:
1052				// This encodes bytes < 0x20 except for \t, \n and \r.
1053				// If escapeHTML is set, it also escapes <, >, and &
1054				// because they can lead to security holes when
1055				// user-controlled strings are rendered into JSON
1056				// and served to some browsers.
1057				e.WriteString(`u00`)
1058				e.WriteByte(hex[b>>4])
1059				e.WriteByte(hex[b&0xF])
1060			}
1061			i++
1062			start = i
1063			continue
1064		}
1065		c, size := utf8.DecodeRuneInString(s[i:])
1066		if c == utf8.RuneError && size == 1 {
1067			if start < i {
1068				e.WriteString(s[start:i])
1069			}
1070			e.WriteString(`\ufffd`)
1071			i += size
1072			start = i
1073			continue
1074		}
1075		// U+2028 is LINE SEPARATOR.
1076		// U+2029 is PARAGRAPH SEPARATOR.
1077		// They are both technically valid characters in JSON strings,
1078		// but don't work in JSONP, which has to be evaluated as JavaScript,
1079		// and can lead to security holes there. It is valid JSON to
1080		// escape them, so we do so unconditionally.
1081		// See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion.
1082		if c == '\u2028' || c == '\u2029' {
1083			if start < i {
1084				e.WriteString(s[start:i])
1085			}
1086			e.WriteString(`\u202`)
1087			e.WriteByte(hex[c&0xF])
1088			i += size
1089			start = i
1090			continue
1091		}
1092		i += size
1093	}
1094	if start < len(s) {
1095		e.WriteString(s[start:])
1096	}
1097	e.WriteByte('"')
1098}
1099
1100// NOTE: keep in sync with string above.
1101func (e *encodeState) stringBytes(s []byte, escapeHTML bool) {
1102	e.WriteByte('"')
1103	start := 0
1104	for i := 0; i < len(s); {
1105		if b := s[i]; b < utf8.RuneSelf {
1106			if htmlSafeSet[b] || (!escapeHTML && safeSet[b]) {
1107				i++
1108				continue
1109			}
1110			if start < i {
1111				e.Write(s[start:i])
1112			}
1113			e.WriteByte('\\')
1114			switch b {
1115			case '\\', '"':
1116				e.WriteByte(b)
1117			case '\n':
1118				e.WriteByte('n')
1119			case '\r':
1120				e.WriteByte('r')
1121			case '\t':
1122				e.WriteByte('t')
1123			default:
1124				// This encodes bytes < 0x20 except for \t, \n and \r.
1125				// If escapeHTML is set, it also escapes <, >, and &
1126				// because they can lead to security holes when
1127				// user-controlled strings are rendered into JSON
1128				// and served to some browsers.
1129				e.WriteString(`u00`)
1130				e.WriteByte(hex[b>>4])
1131				e.WriteByte(hex[b&0xF])
1132			}
1133			i++
1134			start = i
1135			continue
1136		}
1137		c, size := utf8.DecodeRune(s[i:])
1138		if c == utf8.RuneError && size == 1 {
1139			if start < i {
1140				e.Write(s[start:i])
1141			}
1142			e.WriteString(`\ufffd`)
1143			i += size
1144			start = i
1145			continue
1146		}
1147		// U+2028 is LINE SEPARATOR.
1148		// U+2029 is PARAGRAPH SEPARATOR.
1149		// They are both technically valid characters in JSON strings,
1150		// but don't work in JSONP, which has to be evaluated as JavaScript,
1151		// and can lead to security holes there. It is valid JSON to
1152		// escape them, so we do so unconditionally.
1153		// See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion.
1154		if c == '\u2028' || c == '\u2029' {
1155			if start < i {
1156				e.Write(s[start:i])
1157			}
1158			e.WriteString(`\u202`)
1159			e.WriteByte(hex[c&0xF])
1160			i += size
1161			start = i
1162			continue
1163		}
1164		i += size
1165	}
1166	if start < len(s) {
1167		e.Write(s[start:])
1168	}
1169	e.WriteByte('"')
1170}
1171
1172// A field represents a single field found in a struct.
1173type field struct {
1174	name      string
1175	nameBytes []byte                 // []byte(name)
1176	equalFold func(s, t []byte) bool // bytes.EqualFold or equivalent
1177
1178	nameNonEsc  string // `"` + name + `":`
1179	nameEscHTML string // `"` + HTMLEscape(name) + `":`
1180
1181	tag       bool
1182	index     []int
1183	typ       reflect.Type
1184	omitEmpty bool
1185	quoted    bool
1186
1187	encoder encoderFunc
1188}
1189
1190// byIndex sorts field by index sequence.
1191type byIndex []field
1192
1193func (x byIndex) Len() int { return len(x) }
1194
1195func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
1196
1197func (x byIndex) Less(i, j int) bool {
1198	for k, xik := range x[i].index {
1199		if k >= len(x[j].index) {
1200			return false
1201		}
1202		if xik != x[j].index[k] {
1203			return xik < x[j].index[k]
1204		}
1205	}
1206	return len(x[i].index) < len(x[j].index)
1207}
1208
1209// typeFields returns a list of fields that JSON should recognize for the given type.
1210// The algorithm is breadth-first search over the set of structs to include - the top struct
1211// and then any reachable anonymous structs.
1212func typeFields(t reflect.Type) structFields {
1213	// Anonymous fields to explore at the current level and the next.
1214	current := []field{}
1215	next := []field{{typ: t}}
1216
1217	// Count of queued names for current level and the next.
1218	var count, nextCount map[reflect.Type]int
1219
1220	// Types already visited at an earlier level.
1221	visited := map[reflect.Type]bool{}
1222
1223	// Fields found.
1224	var fields []field
1225
1226	// Buffer to run HTMLEscape on field names.
1227	var nameEscBuf bytes.Buffer
1228
1229	for len(next) > 0 {
1230		current, next = next, current[:0]
1231		count, nextCount = nextCount, map[reflect.Type]int{}
1232
1233		for _, f := range current {
1234			if visited[f.typ] {
1235				continue
1236			}
1237			visited[f.typ] = true
1238
1239			// Scan f.typ for fields to include.
1240			for i := 0; i < f.typ.NumField(); i++ {
1241				sf := f.typ.Field(i)
1242				isUnexported := sf.PkgPath != ""
1243				if sf.Anonymous {
1244					t := sf.Type
1245					if t.Kind() == reflect.Ptr {
1246						t = t.Elem()
1247					}
1248					if isUnexported && 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 isUnexported {
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.Ptr {
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}
1419