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