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 objects as defined in
6// RFC 4627. The mapping between JSON objects 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	"runtime"
21	"sort"
22	"strconv"
23	"strings"
24	"sync"
25	"unicode"
26	"unicode/utf8"
27)
28
29// Marshal returns the JSON encoding of v.
30//
31// Marshal traverses the value v recursively.
32// If an encountered value implements the Marshaler interface
33// and is not a nil pointer, Marshal calls its MarshalJSON method
34// to produce JSON. If no MarshalJSON method is present but the
35// value implements encoding.TextMarshaler instead, Marshal calls
36// its MarshalText method.
37// The nil pointer exception is not strictly necessary
38// but mimics a similar, necessary exception in the behavior of
39// UnmarshalJSON.
40//
41// Otherwise, Marshal uses the following type-dependent default encodings:
42//
43// Boolean values encode as JSON booleans.
44//
45// Floating point, integer, and Number values encode as JSON numbers.
46//
47// String values encode as JSON strings coerced to valid UTF-8,
48// replacing invalid bytes with the Unicode replacement rune.
49// The angle brackets "<" and ">" are escaped to "\u003c" and "\u003e"
50// to keep some browsers from misinterpreting JSON output as HTML.
51// Ampersand "&" is also escaped to "\u0026" for the same reason.
52//
53// Array and slice values encode as JSON arrays, except that
54// []byte encodes as a base64-encoded string, and a nil slice
55// encodes as the null JSON object.
56//
57// Struct values encode as JSON objects. Each exported struct field
58// becomes a member of the object unless
59//   - the field's tag is "-", or
60//   - the field is empty and its tag specifies the "omitempty" option.
61// The empty values are false, 0, any
62// nil pointer or interface value, and any array, slice, map, or string of
63// length zero. The object's default key string is the struct field name
64// but can be specified in the struct field's tag value. The "json" key in
65// the struct field's tag value is the key name, followed by an optional comma
66// and options. Examples:
67//
68//   // Field is ignored by this package.
69//   Field int `json:"-"`
70//
71//   // Field appears in JSON as key "myName".
72//   Field int `json:"myName"`
73//
74//   // Field appears in JSON as key "myName" and
75//   // the field is omitted from the object if its value is empty,
76//   // as defined above.
77//   Field int `json:"myName,omitempty"`
78//
79//   // Field appears in JSON as key "Field" (the default), but
80//   // the field is skipped if empty.
81//   // Note the leading comma.
82//   Field int `json:",omitempty"`
83//
84// The "string" option signals that a field is stored as JSON inside a
85// JSON-encoded string. It applies only to fields of string, floating point,
86// integer, or boolean types. This extra level of encoding is sometimes used
87// when communicating with JavaScript programs:
88//
89//    Int64String int64 `json:",string"`
90//
91// The key name will be used if it's a non-empty string consisting of
92// only Unicode letters, digits, dollar signs, percent signs, hyphens,
93// underscores and slashes.
94//
95// Anonymous struct fields are usually marshaled as if their inner exported fields
96// were fields in the outer struct, subject to the usual Go visibility rules amended
97// as described in the next paragraph.
98// An anonymous struct field with a name given in its JSON tag is treated as
99// having that name, rather than being anonymous.
100// An anonymous struct field of interface type is treated the same as having
101// that type as its name, rather than being anonymous.
102//
103// The Go visibility rules for struct fields are amended for JSON when
104// deciding which field to marshal or unmarshal. If there are
105// multiple fields at the same level, and that level is the least
106// nested (and would therefore be the nesting level selected by the
107// usual Go rules), the following extra rules apply:
108//
109// 1) Of those fields, if any are JSON-tagged, only tagged fields are considered,
110// even if there are multiple untagged fields that would otherwise conflict.
111// 2) If there is exactly one field (tagged or not according to the first rule), that is selected.
112// 3) Otherwise there are multiple fields, and all are ignored; no error occurs.
113//
114// Handling of anonymous struct fields is new in Go 1.1.
115// Prior to Go 1.1, anonymous struct fields were ignored. To force ignoring of
116// an anonymous struct field in both current and earlier versions, give the field
117// a JSON tag of "-".
118//
119// Map values encode as JSON objects.
120// The map's key type must be string; the map keys are used as JSON object
121// keys, subject to the UTF-8 coercion described for string values above.
122//
123// Pointer values encode as the value pointed to.
124// A nil pointer encodes as the null JSON object.
125//
126// Interface values encode as the value contained in the interface.
127// A nil interface value encodes as the null JSON object.
128//
129// Channel, complex, and function values cannot be encoded in JSON.
130// Attempting to encode such a value causes Marshal to return
131// an UnsupportedTypeError.
132//
133// JSON cannot represent cyclic data structures and Marshal does not
134// handle them.  Passing cyclic structures to Marshal will result in
135// an infinite recursion.
136//
137func Marshal(v interface{}) ([]byte, error) {
138	e := &encodeState{}
139	err := e.marshal(v)
140	if err != nil {
141		return nil, err
142	}
143	return e.Bytes(), nil
144}
145
146// MarshalIndent is like Marshal but applies Indent to format the output.
147func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
148	b, err := Marshal(v)
149	if err != nil {
150		return nil, err
151	}
152	var buf bytes.Buffer
153	err = Indent(&buf, b, prefix, indent)
154	if err != nil {
155		return nil, err
156	}
157	return buf.Bytes(), nil
158}
159
160// HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029
161// characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029
162// so that the JSON will be safe to embed inside HTML <script> tags.
163// For historical reasons, web browsers don't honor standard HTML
164// escaping within <script> tags, so an alternative JSON encoding must
165// be used.
166func HTMLEscape(dst *bytes.Buffer, src []byte) {
167	// The characters can only appear in string literals,
168	// so just scan the string one byte at a time.
169	start := 0
170	for i, c := range src {
171		if c == '<' || c == '>' || c == '&' {
172			if start < i {
173				dst.Write(src[start:i])
174			}
175			dst.WriteString(`\u00`)
176			dst.WriteByte(hex[c>>4])
177			dst.WriteByte(hex[c&0xF])
178			start = i + 1
179		}
180		// Convert U+2028 and U+2029 (E2 80 A8 and E2 80 A9).
181		if c == 0xE2 && i+2 < len(src) && src[i+1] == 0x80 && src[i+2]&^1 == 0xA8 {
182			if start < i {
183				dst.Write(src[start:i])
184			}
185			dst.WriteString(`\u202`)
186			dst.WriteByte(hex[src[i+2]&0xF])
187			start = i + 3
188		}
189	}
190	if start < len(src) {
191		dst.Write(src[start:])
192	}
193}
194
195// Marshaler is the interface implemented by objects that
196// can marshal themselves into valid JSON.
197type Marshaler interface {
198	MarshalJSON() ([]byte, error)
199}
200
201// An UnsupportedTypeError is returned by Marshal when attempting
202// to encode an unsupported value type.
203type UnsupportedTypeError struct {
204	Type reflect.Type
205}
206
207func (e *UnsupportedTypeError) Error() string {
208	return "json: unsupported type: " + e.Type.String()
209}
210
211type UnsupportedValueError struct {
212	Value reflect.Value
213	Str   string
214}
215
216func (e *UnsupportedValueError) Error() string {
217	return "json: unsupported value: " + e.Str
218}
219
220// Before Go 1.2, an InvalidUTF8Error was returned by Marshal when
221// attempting to encode a string value with invalid UTF-8 sequences.
222// As of Go 1.2, Marshal instead coerces the string to valid UTF-8 by
223// replacing invalid bytes with the Unicode replacement rune U+FFFD.
224// This error is no longer generated but is kept for backwards compatibility
225// with programs that might mention it.
226type InvalidUTF8Error struct {
227	S string // the whole string value that caused the error
228}
229
230func (e *InvalidUTF8Error) Error() string {
231	return "json: invalid UTF-8 in string: " + strconv.Quote(e.S)
232}
233
234type MarshalerError struct {
235	Type reflect.Type
236	Err  error
237}
238
239func (e *MarshalerError) Error() string {
240	return "json: error calling MarshalJSON for type " + e.Type.String() + ": " + e.Err.Error()
241}
242
243var hex = "0123456789abcdef"
244
245// An encodeState encodes JSON into a bytes.Buffer.
246type encodeState struct {
247	bytes.Buffer // accumulated output
248	scratch      [64]byte
249}
250
251var encodeStatePool sync.Pool
252
253func newEncodeState() *encodeState {
254	if v := encodeStatePool.Get(); v != nil {
255		e := v.(*encodeState)
256		e.Reset()
257		return e
258	}
259	return new(encodeState)
260}
261
262func (e *encodeState) marshal(v interface{}) (err error) {
263	defer func() {
264		if r := recover(); r != nil {
265			if _, ok := r.(runtime.Error); ok {
266				panic(r)
267			}
268			if s, ok := r.(string); ok {
269				panic(s)
270			}
271			err = r.(error)
272		}
273	}()
274	e.reflectValue(reflect.ValueOf(v))
275	return nil
276}
277
278func (e *encodeState) error(err error) {
279	panic(err)
280}
281
282func isEmptyValue(v reflect.Value) bool {
283	switch v.Kind() {
284	case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
285		return v.Len() == 0
286	case reflect.Bool:
287		return !v.Bool()
288	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
289		return v.Int() == 0
290	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
291		return v.Uint() == 0
292	case reflect.Float32, reflect.Float64:
293		return v.Float() == 0
294	case reflect.Interface, reflect.Ptr:
295		return v.IsNil()
296	}
297	return false
298}
299
300func (e *encodeState) reflectValue(v reflect.Value) {
301	valueEncoder(v)(e, v, false)
302}
303
304type encoderFunc func(e *encodeState, v reflect.Value, quoted bool)
305
306var encoderCache struct {
307	sync.RWMutex
308	m map[reflect.Type]encoderFunc
309}
310
311func valueEncoder(v reflect.Value) encoderFunc {
312	if !v.IsValid() {
313		return invalidValueEncoder
314	}
315	return typeEncoder(v.Type())
316}
317
318func typeEncoder(t reflect.Type) encoderFunc {
319	encoderCache.RLock()
320	f := encoderCache.m[t]
321	encoderCache.RUnlock()
322	if f != nil {
323		return f
324	}
325
326	// To deal with recursive types, populate the map with an
327	// indirect func before we build it. This type waits on the
328	// real func (f) to be ready and then calls it.  This indirect
329	// func is only used for recursive types.
330	encoderCache.Lock()
331	if encoderCache.m == nil {
332		encoderCache.m = make(map[reflect.Type]encoderFunc)
333	}
334	var wg sync.WaitGroup
335	wg.Add(1)
336	encoderCache.m[t] = func(e *encodeState, v reflect.Value, quoted bool) {
337		wg.Wait()
338		f(e, v, quoted)
339	}
340	encoderCache.Unlock()
341
342	// Compute fields without lock.
343	// Might duplicate effort but won't hold other computations back.
344	f = newTypeEncoder(t, true)
345	wg.Done()
346	encoderCache.Lock()
347	encoderCache.m[t] = f
348	encoderCache.Unlock()
349	return f
350}
351
352var (
353	marshalerType     = reflect.TypeOf(new(Marshaler)).Elem()
354	textMarshalerType = reflect.TypeOf(new(encoding.TextMarshaler)).Elem()
355)
356
357// newTypeEncoder constructs an encoderFunc for a type.
358// The returned encoder only checks CanAddr when allowAddr is true.
359func newTypeEncoder(t reflect.Type, allowAddr bool) encoderFunc {
360	if t.Implements(marshalerType) {
361		return marshalerEncoder
362	}
363	if t.Kind() != reflect.Ptr && allowAddr {
364		if reflect.PtrTo(t).Implements(marshalerType) {
365			return newCondAddrEncoder(addrMarshalerEncoder, newTypeEncoder(t, false))
366		}
367	}
368
369	if t.Implements(textMarshalerType) {
370		return textMarshalerEncoder
371	}
372	if t.Kind() != reflect.Ptr && allowAddr {
373		if reflect.PtrTo(t).Implements(textMarshalerType) {
374			return newCondAddrEncoder(addrTextMarshalerEncoder, newTypeEncoder(t, false))
375		}
376	}
377
378	switch t.Kind() {
379	case reflect.Bool:
380		return boolEncoder
381	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
382		return intEncoder
383	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
384		return uintEncoder
385	case reflect.Float32:
386		return float32Encoder
387	case reflect.Float64:
388		return float64Encoder
389	case reflect.String:
390		return stringEncoder
391	case reflect.Interface:
392		return interfaceEncoder
393	case reflect.Struct:
394		return newStructEncoder(t)
395	case reflect.Map:
396		return newMapEncoder(t)
397	case reflect.Slice:
398		return newSliceEncoder(t)
399	case reflect.Array:
400		return newArrayEncoder(t)
401	case reflect.Ptr:
402		return newPtrEncoder(t)
403	default:
404		return unsupportedTypeEncoder
405	}
406}
407
408func invalidValueEncoder(e *encodeState, v reflect.Value, quoted bool) {
409	e.WriteString("null")
410}
411
412func marshalerEncoder(e *encodeState, v reflect.Value, quoted bool) {
413	if v.Kind() == reflect.Ptr && v.IsNil() {
414		e.WriteString("null")
415		return
416	}
417	m := v.Interface().(Marshaler)
418	b, err := m.MarshalJSON()
419	if err == nil {
420		// copy JSON into buffer, checking validity.
421		err = compact(&e.Buffer, b, true)
422	}
423	if err != nil {
424		e.error(&MarshalerError{v.Type(), err})
425	}
426}
427
428func addrMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) {
429	va := v.Addr()
430	if va.IsNil() {
431		e.WriteString("null")
432		return
433	}
434	m := va.Interface().(Marshaler)
435	b, err := m.MarshalJSON()
436	if err == nil {
437		// copy JSON into buffer, checking validity.
438		err = compact(&e.Buffer, b, true)
439	}
440	if err != nil {
441		e.error(&MarshalerError{v.Type(), err})
442	}
443}
444
445func textMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) {
446	if v.Kind() == reflect.Ptr && v.IsNil() {
447		e.WriteString("null")
448		return
449	}
450	m := v.Interface().(encoding.TextMarshaler)
451	b, err := m.MarshalText()
452	if err != nil {
453		e.error(&MarshalerError{v.Type(), err})
454	}
455	e.stringBytes(b)
456}
457
458func addrTextMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) {
459	va := v.Addr()
460	if va.IsNil() {
461		e.WriteString("null")
462		return
463	}
464	m := va.Interface().(encoding.TextMarshaler)
465	b, err := m.MarshalText()
466	if err != nil {
467		e.error(&MarshalerError{v.Type(), err})
468	}
469	e.stringBytes(b)
470}
471
472func boolEncoder(e *encodeState, v reflect.Value, quoted bool) {
473	if quoted {
474		e.WriteByte('"')
475	}
476	if v.Bool() {
477		e.WriteString("true")
478	} else {
479		e.WriteString("false")
480	}
481	if quoted {
482		e.WriteByte('"')
483	}
484}
485
486func intEncoder(e *encodeState, v reflect.Value, quoted bool) {
487	b := strconv.AppendInt(e.scratch[:0], v.Int(), 10)
488	if quoted {
489		e.WriteByte('"')
490	}
491	e.Write(b)
492	if quoted {
493		e.WriteByte('"')
494	}
495}
496
497func uintEncoder(e *encodeState, v reflect.Value, quoted bool) {
498	b := strconv.AppendUint(e.scratch[:0], v.Uint(), 10)
499	if quoted {
500		e.WriteByte('"')
501	}
502	e.Write(b)
503	if quoted {
504		e.WriteByte('"')
505	}
506}
507
508type floatEncoder int // number of bits
509
510func (bits floatEncoder) encode(e *encodeState, v reflect.Value, quoted bool) {
511	f := v.Float()
512	if math.IsInf(f, 0) || math.IsNaN(f) {
513		e.error(&UnsupportedValueError{v, strconv.FormatFloat(f, 'g', -1, int(bits))})
514	}
515	b := strconv.AppendFloat(e.scratch[:0], f, 'g', -1, int(bits))
516	if quoted {
517		e.WriteByte('"')
518	}
519	e.Write(b)
520	if quoted {
521		e.WriteByte('"')
522	}
523}
524
525var (
526	float32Encoder = (floatEncoder(32)).encode
527	float64Encoder = (floatEncoder(64)).encode
528)
529
530func stringEncoder(e *encodeState, v reflect.Value, quoted bool) {
531	if v.Type() == numberType {
532		numStr := v.String()
533		// In Go1.5 the empty string encodes to "0", while this is not a valid number literal
534		// we keep compatibility so check validity after this.
535		if numStr == "" {
536			numStr = "0" // Number's zero-val
537		}
538		if !isValidNumber(numStr) {
539			e.error(fmt.Errorf("json: invalid number literal %q", numStr))
540		}
541		e.WriteString(numStr)
542		return
543	}
544	if quoted {
545		sb, err := Marshal(v.String())
546		if err != nil {
547			e.error(err)
548		}
549		e.string(string(sb))
550	} else {
551		e.string(v.String())
552	}
553}
554
555func interfaceEncoder(e *encodeState, v reflect.Value, quoted bool) {
556	if v.IsNil() {
557		e.WriteString("null")
558		return
559	}
560	e.reflectValue(v.Elem())
561}
562
563func unsupportedTypeEncoder(e *encodeState, v reflect.Value, quoted bool) {
564	e.error(&UnsupportedTypeError{v.Type()})
565}
566
567type structEncoder struct {
568	fields    []field
569	fieldEncs []encoderFunc
570}
571
572func (se *structEncoder) encode(e *encodeState, v reflect.Value, quoted bool) {
573	e.WriteByte('{')
574	first := true
575	for i, f := range se.fields {
576		fv := fieldByIndex(v, f.index)
577		if !fv.IsValid() || f.omitEmpty && isEmptyValue(fv) {
578			continue
579		}
580		if first {
581			first = false
582		} else {
583			e.WriteByte(',')
584		}
585		e.string(f.name)
586		e.WriteByte(':')
587		se.fieldEncs[i](e, fv, f.quoted)
588	}
589	e.WriteByte('}')
590}
591
592func newStructEncoder(t reflect.Type) encoderFunc {
593	fields := cachedTypeFields(t)
594	se := &structEncoder{
595		fields:    fields,
596		fieldEncs: make([]encoderFunc, len(fields)),
597	}
598	for i, f := range fields {
599		se.fieldEncs[i] = typeEncoder(typeByIndex(t, f.index))
600	}
601	return se.encode
602}
603
604type mapEncoder struct {
605	elemEnc encoderFunc
606}
607
608func (me *mapEncoder) encode(e *encodeState, v reflect.Value, _ bool) {
609	if v.IsNil() {
610		e.WriteString("null")
611		return
612	}
613	e.WriteByte('{')
614	var sv stringValues = v.MapKeys()
615	sort.Sort(sv)
616	for i, k := range sv {
617		if i > 0 {
618			e.WriteByte(',')
619		}
620		e.string(k.String())
621		e.WriteByte(':')
622		me.elemEnc(e, v.MapIndex(k), false)
623	}
624	e.WriteByte('}')
625}
626
627func newMapEncoder(t reflect.Type) encoderFunc {
628	if t.Key().Kind() != reflect.String {
629		return unsupportedTypeEncoder
630	}
631	me := &mapEncoder{typeEncoder(t.Elem())}
632	return me.encode
633}
634
635func encodeByteSlice(e *encodeState, v reflect.Value, _ bool) {
636	if v.IsNil() {
637		e.WriteString("null")
638		return
639	}
640	s := v.Bytes()
641	e.WriteByte('"')
642	if len(s) < 1024 {
643		// for small buffers, using Encode directly is much faster.
644		dst := make([]byte, base64.StdEncoding.EncodedLen(len(s)))
645		base64.StdEncoding.Encode(dst, s)
646		e.Write(dst)
647	} else {
648		// for large buffers, avoid unnecessary extra temporary
649		// buffer space.
650		enc := base64.NewEncoder(base64.StdEncoding, e)
651		enc.Write(s)
652		enc.Close()
653	}
654	e.WriteByte('"')
655}
656
657// sliceEncoder just wraps an arrayEncoder, checking to make sure the value isn't nil.
658type sliceEncoder struct {
659	arrayEnc encoderFunc
660}
661
662func (se *sliceEncoder) encode(e *encodeState, v reflect.Value, _ bool) {
663	if v.IsNil() {
664		e.WriteString("null")
665		return
666	}
667	se.arrayEnc(e, v, false)
668}
669
670func newSliceEncoder(t reflect.Type) encoderFunc {
671	// Byte slices get special treatment; arrays don't.
672	if t.Elem().Kind() == reflect.Uint8 {
673		return encodeByteSlice
674	}
675	enc := &sliceEncoder{newArrayEncoder(t)}
676	return enc.encode
677}
678
679type arrayEncoder struct {
680	elemEnc encoderFunc
681}
682
683func (ae *arrayEncoder) encode(e *encodeState, v reflect.Value, _ bool) {
684	e.WriteByte('[')
685	n := v.Len()
686	for i := 0; i < n; i++ {
687		if i > 0 {
688			e.WriteByte(',')
689		}
690		ae.elemEnc(e, v.Index(i), false)
691	}
692	e.WriteByte(']')
693}
694
695func newArrayEncoder(t reflect.Type) encoderFunc {
696	enc := &arrayEncoder{typeEncoder(t.Elem())}
697	return enc.encode
698}
699
700type ptrEncoder struct {
701	elemEnc encoderFunc
702}
703
704func (pe *ptrEncoder) encode(e *encodeState, v reflect.Value, quoted bool) {
705	if v.IsNil() {
706		e.WriteString("null")
707		return
708	}
709	pe.elemEnc(e, v.Elem(), quoted)
710}
711
712func newPtrEncoder(t reflect.Type) encoderFunc {
713	enc := &ptrEncoder{typeEncoder(t.Elem())}
714	return enc.encode
715}
716
717type condAddrEncoder struct {
718	canAddrEnc, elseEnc encoderFunc
719}
720
721func (ce *condAddrEncoder) encode(e *encodeState, v reflect.Value, quoted bool) {
722	if v.CanAddr() {
723		ce.canAddrEnc(e, v, quoted)
724	} else {
725		ce.elseEnc(e, v, quoted)
726	}
727}
728
729// newCondAddrEncoder returns an encoder that checks whether its value
730// CanAddr and delegates to canAddrEnc if so, else to elseEnc.
731func newCondAddrEncoder(canAddrEnc, elseEnc encoderFunc) encoderFunc {
732	enc := &condAddrEncoder{canAddrEnc: canAddrEnc, elseEnc: elseEnc}
733	return enc.encode
734}
735
736func isValidTag(s string) bool {
737	if s == "" {
738		return false
739	}
740	for _, c := range s {
741		switch {
742		case strings.ContainsRune("!#$%&()*+-./:<=>?@[]^_{|}~ ", c):
743			// Backslash and quote chars are reserved, but
744			// otherwise any punctuation chars are allowed
745			// in a tag name.
746		default:
747			if !unicode.IsLetter(c) && !unicode.IsDigit(c) {
748				return false
749			}
750		}
751	}
752	return true
753}
754
755func fieldByIndex(v reflect.Value, index []int) reflect.Value {
756	for _, i := range index {
757		if v.Kind() == reflect.Ptr {
758			if v.IsNil() {
759				return reflect.Value{}
760			}
761			v = v.Elem()
762		}
763		v = v.Field(i)
764	}
765	return v
766}
767
768func typeByIndex(t reflect.Type, index []int) reflect.Type {
769	for _, i := range index {
770		if t.Kind() == reflect.Ptr {
771			t = t.Elem()
772		}
773		t = t.Field(i).Type
774	}
775	return t
776}
777
778// stringValues is a slice of reflect.Value holding *reflect.StringValue.
779// It implements the methods to sort by string.
780type stringValues []reflect.Value
781
782func (sv stringValues) Len() int           { return len(sv) }
783func (sv stringValues) Swap(i, j int)      { sv[i], sv[j] = sv[j], sv[i] }
784func (sv stringValues) Less(i, j int) bool { return sv.get(i) < sv.get(j) }
785func (sv stringValues) get(i int) string   { return sv[i].String() }
786
787// NOTE: keep in sync with stringBytes below.
788func (e *encodeState) string(s string) int {
789	len0 := e.Len()
790	e.WriteByte('"')
791	start := 0
792	for i := 0; i < len(s); {
793		if b := s[i]; b < utf8.RuneSelf {
794			if 0x20 <= b && b != '\\' && b != '"' && b != '<' && b != '>' && b != '&' {
795				i++
796				continue
797			}
798			if start < i {
799				e.WriteString(s[start:i])
800			}
801			switch b {
802			case '\\', '"':
803				e.WriteByte('\\')
804				e.WriteByte(b)
805			case '\n':
806				e.WriteByte('\\')
807				e.WriteByte('n')
808			case '\r':
809				e.WriteByte('\\')
810				e.WriteByte('r')
811			case '\t':
812				e.WriteByte('\\')
813				e.WriteByte('t')
814			default:
815				// This encodes bytes < 0x20 except for \n and \r,
816				// as well as <, > and &. The latter are escaped because they
817				// can lead to security holes when user-controlled strings
818				// are rendered into JSON and served to some browsers.
819				e.WriteString(`\u00`)
820				e.WriteByte(hex[b>>4])
821				e.WriteByte(hex[b&0xF])
822			}
823			i++
824			start = i
825			continue
826		}
827		c, size := utf8.DecodeRuneInString(s[i:])
828		if c == utf8.RuneError && size == 1 {
829			if start < i {
830				e.WriteString(s[start:i])
831			}
832			e.WriteString(`\ufffd`)
833			i += size
834			start = i
835			continue
836		}
837		// U+2028 is LINE SEPARATOR.
838		// U+2029 is PARAGRAPH SEPARATOR.
839		// They are both technically valid characters in JSON strings,
840		// but don't work in JSONP, which has to be evaluated as JavaScript,
841		// and can lead to security holes there. It is valid JSON to
842		// escape them, so we do so unconditionally.
843		// See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion.
844		if c == '\u2028' || c == '\u2029' {
845			if start < i {
846				e.WriteString(s[start:i])
847			}
848			e.WriteString(`\u202`)
849			e.WriteByte(hex[c&0xF])
850			i += size
851			start = i
852			continue
853		}
854		i += size
855	}
856	if start < len(s) {
857		e.WriteString(s[start:])
858	}
859	e.WriteByte('"')
860	return e.Len() - len0
861}
862
863// NOTE: keep in sync with string above.
864func (e *encodeState) stringBytes(s []byte) int {
865	len0 := e.Len()
866	e.WriteByte('"')
867	start := 0
868	for i := 0; i < len(s); {
869		if b := s[i]; b < utf8.RuneSelf {
870			if 0x20 <= b && b != '\\' && b != '"' && b != '<' && b != '>' && b != '&' {
871				i++
872				continue
873			}
874			if start < i {
875				e.Write(s[start:i])
876			}
877			switch b {
878			case '\\', '"':
879				e.WriteByte('\\')
880				e.WriteByte(b)
881			case '\n':
882				e.WriteByte('\\')
883				e.WriteByte('n')
884			case '\r':
885				e.WriteByte('\\')
886				e.WriteByte('r')
887			case '\t':
888				e.WriteByte('\\')
889				e.WriteByte('t')
890			default:
891				// This encodes bytes < 0x20 except for \n and \r,
892				// as well as <, >, and &. The latter are escaped because they
893				// can lead to security holes when user-controlled strings
894				// are rendered into JSON and served to some browsers.
895				e.WriteString(`\u00`)
896				e.WriteByte(hex[b>>4])
897				e.WriteByte(hex[b&0xF])
898			}
899			i++
900			start = i
901			continue
902		}
903		c, size := utf8.DecodeRune(s[i:])
904		if c == utf8.RuneError && size == 1 {
905			if start < i {
906				e.Write(s[start:i])
907			}
908			e.WriteString(`\ufffd`)
909			i += size
910			start = i
911			continue
912		}
913		// U+2028 is LINE SEPARATOR.
914		// U+2029 is PARAGRAPH SEPARATOR.
915		// They are both technically valid characters in JSON strings,
916		// but don't work in JSONP, which has to be evaluated as JavaScript,
917		// and can lead to security holes there. It is valid JSON to
918		// escape them, so we do so unconditionally.
919		// See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion.
920		if c == '\u2028' || c == '\u2029' {
921			if start < i {
922				e.Write(s[start:i])
923			}
924			e.WriteString(`\u202`)
925			e.WriteByte(hex[c&0xF])
926			i += size
927			start = i
928			continue
929		}
930		i += size
931	}
932	if start < len(s) {
933		e.Write(s[start:])
934	}
935	e.WriteByte('"')
936	return e.Len() - len0
937}
938
939// A field represents a single field found in a struct.
940type field struct {
941	name      string
942	nameBytes []byte                 // []byte(name)
943	equalFold func(s, t []byte) bool // bytes.EqualFold or equivalent
944
945	tag       bool
946	index     []int
947	typ       reflect.Type
948	omitEmpty bool
949	quoted    bool
950}
951
952func fillField(f field) field {
953	f.nameBytes = []byte(f.name)
954	f.equalFold = foldFunc(f.nameBytes)
955	return f
956}
957
958// byName sorts field by name, breaking ties with depth,
959// then breaking ties with "name came from json tag", then
960// breaking ties with index sequence.
961type byName []field
962
963func (x byName) Len() int { return len(x) }
964
965func (x byName) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
966
967func (x byName) Less(i, j int) bool {
968	if x[i].name != x[j].name {
969		return x[i].name < x[j].name
970	}
971	if len(x[i].index) != len(x[j].index) {
972		return len(x[i].index) < len(x[j].index)
973	}
974	if x[i].tag != x[j].tag {
975		return x[i].tag
976	}
977	return byIndex(x).Less(i, j)
978}
979
980// byIndex sorts field by index sequence.
981type byIndex []field
982
983func (x byIndex) Len() int { return len(x) }
984
985func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
986
987func (x byIndex) Less(i, j int) bool {
988	for k, xik := range x[i].index {
989		if k >= len(x[j].index) {
990			return false
991		}
992		if xik != x[j].index[k] {
993			return xik < x[j].index[k]
994		}
995	}
996	return len(x[i].index) < len(x[j].index)
997}
998
999// typeFields returns a list of fields that JSON should recognize for the given type.
1000// The algorithm is breadth-first search over the set of structs to include - the top struct
1001// and then any reachable anonymous structs.
1002func typeFields(t reflect.Type) []field {
1003	// Anonymous fields to explore at the current level and the next.
1004	current := []field{}
1005	next := []field{{typ: t}}
1006
1007	// Count of queued names for current level and the next.
1008	count := map[reflect.Type]int{}
1009	nextCount := map[reflect.Type]int{}
1010
1011	// Types already visited at an earlier level.
1012	visited := map[reflect.Type]bool{}
1013
1014	// Fields found.
1015	var fields []field
1016
1017	for len(next) > 0 {
1018		current, next = next, current[:0]
1019		count, nextCount = nextCount, map[reflect.Type]int{}
1020
1021		for _, f := range current {
1022			if visited[f.typ] {
1023				continue
1024			}
1025			visited[f.typ] = true
1026
1027			// Scan f.typ for fields to include.
1028			for i := 0; i < f.typ.NumField(); i++ {
1029				sf := f.typ.Field(i)
1030				if sf.PkgPath != "" && !sf.Anonymous { // unexported
1031					continue
1032				}
1033				tag := sf.Tag.Get("json")
1034				if tag == "-" {
1035					continue
1036				}
1037				name, opts := parseTag(tag)
1038				if !isValidTag(name) {
1039					name = ""
1040				}
1041				index := make([]int, len(f.index)+1)
1042				copy(index, f.index)
1043				index[len(f.index)] = i
1044
1045				ft := sf.Type
1046				if ft.Name() == "" && ft.Kind() == reflect.Ptr {
1047					// Follow pointer.
1048					ft = ft.Elem()
1049				}
1050
1051				// Only strings, floats, integers, and booleans can be quoted.
1052				quoted := false
1053				if opts.Contains("string") {
1054					switch ft.Kind() {
1055					case reflect.Bool,
1056						reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
1057						reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
1058						reflect.Float32, reflect.Float64,
1059						reflect.String:
1060						quoted = true
1061					}
1062				}
1063
1064				// Record found field and index sequence.
1065				if name != "" || !sf.Anonymous || ft.Kind() != reflect.Struct {
1066					tagged := name != ""
1067					if name == "" {
1068						name = sf.Name
1069					}
1070					fields = append(fields, fillField(field{
1071						name:      name,
1072						tag:       tagged,
1073						index:     index,
1074						typ:       ft,
1075						omitEmpty: opts.Contains("omitempty"),
1076						quoted:    quoted,
1077					}))
1078					if count[f.typ] > 1 {
1079						// If there were multiple instances, add a second,
1080						// so that the annihilation code will see a duplicate.
1081						// It only cares about the distinction between 1 or 2,
1082						// so don't bother generating any more copies.
1083						fields = append(fields, fields[len(fields)-1])
1084					}
1085					continue
1086				}
1087
1088				// Record new anonymous struct to explore in next round.
1089				nextCount[ft]++
1090				if nextCount[ft] == 1 {
1091					next = append(next, fillField(field{name: ft.Name(), index: index, typ: ft}))
1092				}
1093			}
1094		}
1095	}
1096
1097	sort.Sort(byName(fields))
1098
1099	// Delete all fields that are hidden by the Go rules for embedded fields,
1100	// except that fields with JSON tags are promoted.
1101
1102	// The fields are sorted in primary order of name, secondary order
1103	// of field index length. Loop over names; for each name, delete
1104	// hidden fields by choosing the one dominant field that survives.
1105	out := fields[:0]
1106	for advance, i := 0, 0; i < len(fields); i += advance {
1107		// One iteration per name.
1108		// Find the sequence of fields with the name of this first field.
1109		fi := fields[i]
1110		name := fi.name
1111		for advance = 1; i+advance < len(fields); advance++ {
1112			fj := fields[i+advance]
1113			if fj.name != name {
1114				break
1115			}
1116		}
1117		if advance == 1 { // Only one field with this name
1118			out = append(out, fi)
1119			continue
1120		}
1121		dominant, ok := dominantField(fields[i : i+advance])
1122		if ok {
1123			out = append(out, dominant)
1124		}
1125	}
1126
1127	fields = out
1128	sort.Sort(byIndex(fields))
1129
1130	return fields
1131}
1132
1133// dominantField looks through the fields, all of which are known to
1134// have the same name, to find the single field that dominates the
1135// others using Go's embedding rules, modified by the presence of
1136// JSON tags. If there are multiple top-level fields, the boolean
1137// will be false: This condition is an error in Go and we skip all
1138// the fields.
1139func dominantField(fields []field) (field, bool) {
1140	// The fields are sorted in increasing index-length order. The winner
1141	// must therefore be one with the shortest index length. Drop all
1142	// longer entries, which is easy: just truncate the slice.
1143	length := len(fields[0].index)
1144	tagged := -1 // Index of first tagged field.
1145	for i, f := range fields {
1146		if len(f.index) > length {
1147			fields = fields[:i]
1148			break
1149		}
1150		if f.tag {
1151			if tagged >= 0 {
1152				// Multiple tagged fields at the same level: conflict.
1153				// Return no field.
1154				return field{}, false
1155			}
1156			tagged = i
1157		}
1158	}
1159	if tagged >= 0 {
1160		return fields[tagged], true
1161	}
1162	// All remaining fields have the same length. If there's more than one,
1163	// we have a conflict (two fields named "X" at the same level) and we
1164	// return no field.
1165	if len(fields) > 1 {
1166		return field{}, false
1167	}
1168	return fields[0], true
1169}
1170
1171var fieldCache struct {
1172	sync.RWMutex
1173	m map[reflect.Type][]field
1174}
1175
1176// cachedTypeFields is like typeFields but uses a cache to avoid repeated work.
1177func cachedTypeFields(t reflect.Type) []field {
1178	fieldCache.RLock()
1179	f := fieldCache.m[t]
1180	fieldCache.RUnlock()
1181	if f != nil {
1182		return f
1183	}
1184
1185	// Compute fields without lock.
1186	// Might duplicate effort but won't hold other computations back.
1187	f = typeFields(t)
1188	if f == nil {
1189		f = []field{}
1190	}
1191
1192	fieldCache.Lock()
1193	if fieldCache.m == nil {
1194		fieldCache.m = map[reflect.Type][]field{}
1195	}
1196	fieldCache.m[t] = f
1197	fieldCache.Unlock()
1198	return f
1199}
1200