github.com/hlts2/go@v0.0.0-20170904000733-812b34efaed8/src/encoding/json/encode.go (about) 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 sync.Map // map[reflect.Type]encoderFunc 336 337 func valueEncoder(v reflect.Value) encoderFunc { 338 if !v.IsValid() { 339 return invalidValueEncoder 340 } 341 return typeEncoder(v.Type()) 342 } 343 344 func typeEncoder(t reflect.Type) encoderFunc { 345 if fi, ok := encoderCache.Load(t); ok { 346 return fi.(encoderFunc) 347 } 348 349 // To deal with recursive types, populate the map with an 350 // indirect func before we build it. This type waits on the 351 // real func (f) to be ready and then calls it. This indirect 352 // func is only used for recursive types. 353 var ( 354 wg sync.WaitGroup 355 f encoderFunc 356 ) 357 wg.Add(1) 358 fi, loaded := encoderCache.LoadOrStore(t, encoderFunc(func(e *encodeState, v reflect.Value, opts encOpts) { 359 wg.Wait() 360 f(e, v, opts) 361 })) 362 if loaded { 363 return fi.(encoderFunc) 364 } 365 366 // Compute the real encoder and replace the indirect func with it. 367 f = newTypeEncoder(t, true) 368 wg.Done() 369 encoderCache.Store(t, f) 370 return f 371 } 372 373 var ( 374 marshalerType = reflect.TypeOf(new(Marshaler)).Elem() 375 textMarshalerType = reflect.TypeOf(new(encoding.TextMarshaler)).Elem() 376 ) 377 378 // newTypeEncoder constructs an encoderFunc for a type. 379 // The returned encoder only checks CanAddr when allowAddr is true. 380 func newTypeEncoder(t reflect.Type, allowAddr bool) encoderFunc { 381 if t.Implements(marshalerType) { 382 return marshalerEncoder 383 } 384 if t.Kind() != reflect.Ptr && allowAddr { 385 if reflect.PtrTo(t).Implements(marshalerType) { 386 return newCondAddrEncoder(addrMarshalerEncoder, newTypeEncoder(t, false)) 387 } 388 } 389 390 if t.Implements(textMarshalerType) { 391 return textMarshalerEncoder 392 } 393 if t.Kind() != reflect.Ptr && allowAddr { 394 if reflect.PtrTo(t).Implements(textMarshalerType) { 395 return newCondAddrEncoder(addrTextMarshalerEncoder, newTypeEncoder(t, false)) 396 } 397 } 398 399 switch t.Kind() { 400 case reflect.Bool: 401 return boolEncoder 402 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 403 return intEncoder 404 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 405 return uintEncoder 406 case reflect.Float32: 407 return float32Encoder 408 case reflect.Float64: 409 return float64Encoder 410 case reflect.String: 411 return stringEncoder 412 case reflect.Interface: 413 return interfaceEncoder 414 case reflect.Struct: 415 return newStructEncoder(t) 416 case reflect.Map: 417 return newMapEncoder(t) 418 case reflect.Slice: 419 return newSliceEncoder(t) 420 case reflect.Array: 421 return newArrayEncoder(t) 422 case reflect.Ptr: 423 return newPtrEncoder(t) 424 default: 425 return unsupportedTypeEncoder 426 } 427 } 428 429 func invalidValueEncoder(e *encodeState, v reflect.Value, _ encOpts) { 430 e.WriteString("null") 431 } 432 433 func marshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) { 434 if v.Kind() == reflect.Ptr && v.IsNil() { 435 e.WriteString("null") 436 return 437 } 438 m, ok := v.Interface().(Marshaler) 439 if !ok { 440 e.WriteString("null") 441 return 442 } 443 b, err := m.MarshalJSON() 444 if err == nil { 445 // copy JSON into buffer, checking validity. 446 err = compact(&e.Buffer, b, opts.escapeHTML) 447 } 448 if err != nil { 449 e.error(&MarshalerError{v.Type(), err}) 450 } 451 } 452 453 func addrMarshalerEncoder(e *encodeState, v reflect.Value, _ encOpts) { 454 va := v.Addr() 455 if va.IsNil() { 456 e.WriteString("null") 457 return 458 } 459 m := va.Interface().(Marshaler) 460 b, err := m.MarshalJSON() 461 if err == nil { 462 // copy JSON into buffer, checking validity. 463 err = compact(&e.Buffer, b, true) 464 } 465 if err != nil { 466 e.error(&MarshalerError{v.Type(), err}) 467 } 468 } 469 470 func textMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) { 471 if v.Kind() == reflect.Ptr && v.IsNil() { 472 e.WriteString("null") 473 return 474 } 475 m := v.Interface().(encoding.TextMarshaler) 476 b, err := m.MarshalText() 477 if err != nil { 478 e.error(&MarshalerError{v.Type(), err}) 479 } 480 e.stringBytes(b, opts.escapeHTML) 481 } 482 483 func addrTextMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) { 484 va := v.Addr() 485 if va.IsNil() { 486 e.WriteString("null") 487 return 488 } 489 m := va.Interface().(encoding.TextMarshaler) 490 b, err := m.MarshalText() 491 if err != nil { 492 e.error(&MarshalerError{v.Type(), err}) 493 } 494 e.stringBytes(b, opts.escapeHTML) 495 } 496 497 func boolEncoder(e *encodeState, v reflect.Value, opts encOpts) { 498 if opts.quoted { 499 e.WriteByte('"') 500 } 501 if v.Bool() { 502 e.WriteString("true") 503 } else { 504 e.WriteString("false") 505 } 506 if opts.quoted { 507 e.WriteByte('"') 508 } 509 } 510 511 func intEncoder(e *encodeState, v reflect.Value, opts encOpts) { 512 b := strconv.AppendInt(e.scratch[:0], v.Int(), 10) 513 if opts.quoted { 514 e.WriteByte('"') 515 } 516 e.Write(b) 517 if opts.quoted { 518 e.WriteByte('"') 519 } 520 } 521 522 func uintEncoder(e *encodeState, v reflect.Value, opts encOpts) { 523 b := strconv.AppendUint(e.scratch[:0], v.Uint(), 10) 524 if opts.quoted { 525 e.WriteByte('"') 526 } 527 e.Write(b) 528 if opts.quoted { 529 e.WriteByte('"') 530 } 531 } 532 533 type floatEncoder int // number of bits 534 535 func (bits floatEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { 536 f := v.Float() 537 if math.IsInf(f, 0) || math.IsNaN(f) { 538 e.error(&UnsupportedValueError{v, strconv.FormatFloat(f, 'g', -1, int(bits))}) 539 } 540 541 // Convert as if by ES6 number to string conversion. 542 // This matches most other JSON generators. 543 // See golang.org/issue/6384 and golang.org/issue/14135. 544 // Like fmt %g, but the exponent cutoffs are different 545 // and exponents themselves are not padded to two digits. 546 b := e.scratch[:0] 547 abs := math.Abs(f) 548 fmt := byte('f') 549 // Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right. 550 if abs != 0 { 551 if bits == 64 && (abs < 1e-6 || abs >= 1e21) || bits == 32 && (float32(abs) < 1e-6 || float32(abs) >= 1e21) { 552 fmt = 'e' 553 } 554 } 555 b = strconv.AppendFloat(b, f, fmt, -1, int(bits)) 556 if fmt == 'e' { 557 // clean up e-09 to e-9 558 n := len(b) 559 if n >= 4 && b[n-4] == 'e' && b[n-3] == '-' && b[n-2] == '0' { 560 b[n-2] = b[n-1] 561 b = b[:n-1] 562 } 563 } 564 565 if opts.quoted { 566 e.WriteByte('"') 567 } 568 e.Write(b) 569 if opts.quoted { 570 e.WriteByte('"') 571 } 572 } 573 574 var ( 575 float32Encoder = (floatEncoder(32)).encode 576 float64Encoder = (floatEncoder(64)).encode 577 ) 578 579 func stringEncoder(e *encodeState, v reflect.Value, opts encOpts) { 580 if v.Type() == numberType { 581 numStr := v.String() 582 // In Go1.5 the empty string encodes to "0", while this is not a valid number literal 583 // we keep compatibility so check validity after this. 584 if numStr == "" { 585 numStr = "0" // Number's zero-val 586 } 587 if !isValidNumber(numStr) { 588 e.error(fmt.Errorf("json: invalid number literal %q", numStr)) 589 } 590 e.WriteString(numStr) 591 return 592 } 593 if opts.quoted { 594 sb, err := Marshal(v.String()) 595 if err != nil { 596 e.error(err) 597 } 598 e.string(string(sb), opts.escapeHTML) 599 } else { 600 e.string(v.String(), opts.escapeHTML) 601 } 602 } 603 604 func interfaceEncoder(e *encodeState, v reflect.Value, opts encOpts) { 605 if v.IsNil() { 606 e.WriteString("null") 607 return 608 } 609 e.reflectValue(v.Elem(), opts) 610 } 611 612 func unsupportedTypeEncoder(e *encodeState, v reflect.Value, _ encOpts) { 613 e.error(&UnsupportedTypeError{v.Type()}) 614 } 615 616 type structEncoder struct { 617 fields []field 618 fieldEncs []encoderFunc 619 } 620 621 func (se *structEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { 622 e.WriteByte('{') 623 first := true 624 for i, f := range se.fields { 625 fv := fieldByIndex(v, f.index) 626 if !fv.IsValid() || f.omitEmpty && isEmptyValue(fv) { 627 continue 628 } 629 if first { 630 first = false 631 } else { 632 e.WriteByte(',') 633 } 634 e.string(f.name, opts.escapeHTML) 635 e.WriteByte(':') 636 opts.quoted = f.quoted 637 se.fieldEncs[i](e, fv, opts) 638 } 639 e.WriteByte('}') 640 } 641 642 func newStructEncoder(t reflect.Type) encoderFunc { 643 fields := cachedTypeFields(t) 644 se := &structEncoder{ 645 fields: fields, 646 fieldEncs: make([]encoderFunc, len(fields)), 647 } 648 for i, f := range fields { 649 se.fieldEncs[i] = typeEncoder(typeByIndex(t, f.index)) 650 } 651 return se.encode 652 } 653 654 type mapEncoder struct { 655 elemEnc encoderFunc 656 } 657 658 func (me *mapEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { 659 if v.IsNil() { 660 e.WriteString("null") 661 return 662 } 663 e.WriteByte('{') 664 665 // Extract and sort the keys. 666 keys := v.MapKeys() 667 sv := make([]reflectWithString, len(keys)) 668 for i, v := range keys { 669 sv[i].v = v 670 if err := sv[i].resolve(); err != nil { 671 e.error(&MarshalerError{v.Type(), err}) 672 } 673 } 674 sort.Slice(sv, func(i, j int) bool { return sv[i].s < sv[j].s }) 675 676 for i, kv := range sv { 677 if i > 0 { 678 e.WriteByte(',') 679 } 680 e.string(kv.s, opts.escapeHTML) 681 e.WriteByte(':') 682 me.elemEnc(e, v.MapIndex(kv.v), opts) 683 } 684 e.WriteByte('}') 685 } 686 687 func newMapEncoder(t reflect.Type) encoderFunc { 688 switch t.Key().Kind() { 689 case reflect.String, 690 reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, 691 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 692 default: 693 if !t.Key().Implements(textMarshalerType) { 694 return unsupportedTypeEncoder 695 } 696 } 697 me := &mapEncoder{typeEncoder(t.Elem())} 698 return me.encode 699 } 700 701 func encodeByteSlice(e *encodeState, v reflect.Value, _ encOpts) { 702 if v.IsNil() { 703 e.WriteString("null") 704 return 705 } 706 s := v.Bytes() 707 e.WriteByte('"') 708 if len(s) < 1024 { 709 // for small buffers, using Encode directly is much faster. 710 dst := make([]byte, base64.StdEncoding.EncodedLen(len(s))) 711 base64.StdEncoding.Encode(dst, s) 712 e.Write(dst) 713 } else { 714 // for large buffers, avoid unnecessary extra temporary 715 // buffer space. 716 enc := base64.NewEncoder(base64.StdEncoding, e) 717 enc.Write(s) 718 enc.Close() 719 } 720 e.WriteByte('"') 721 } 722 723 // sliceEncoder just wraps an arrayEncoder, checking to make sure the value isn't nil. 724 type sliceEncoder struct { 725 arrayEnc encoderFunc 726 } 727 728 func (se *sliceEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { 729 if v.IsNil() { 730 e.WriteString("null") 731 return 732 } 733 se.arrayEnc(e, v, opts) 734 } 735 736 func newSliceEncoder(t reflect.Type) encoderFunc { 737 // Byte slices get special treatment; arrays don't. 738 if t.Elem().Kind() == reflect.Uint8 { 739 p := reflect.PtrTo(t.Elem()) 740 if !p.Implements(marshalerType) && !p.Implements(textMarshalerType) { 741 return encodeByteSlice 742 } 743 } 744 enc := &sliceEncoder{newArrayEncoder(t)} 745 return enc.encode 746 } 747 748 type arrayEncoder struct { 749 elemEnc encoderFunc 750 } 751 752 func (ae *arrayEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { 753 e.WriteByte('[') 754 n := v.Len() 755 for i := 0; i < n; i++ { 756 if i > 0 { 757 e.WriteByte(',') 758 } 759 ae.elemEnc(e, v.Index(i), opts) 760 } 761 e.WriteByte(']') 762 } 763 764 func newArrayEncoder(t reflect.Type) encoderFunc { 765 enc := &arrayEncoder{typeEncoder(t.Elem())} 766 return enc.encode 767 } 768 769 type ptrEncoder struct { 770 elemEnc encoderFunc 771 } 772 773 func (pe *ptrEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { 774 if v.IsNil() { 775 e.WriteString("null") 776 return 777 } 778 pe.elemEnc(e, v.Elem(), opts) 779 } 780 781 func newPtrEncoder(t reflect.Type) encoderFunc { 782 enc := &ptrEncoder{typeEncoder(t.Elem())} 783 return enc.encode 784 } 785 786 type condAddrEncoder struct { 787 canAddrEnc, elseEnc encoderFunc 788 } 789 790 func (ce *condAddrEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { 791 if v.CanAddr() { 792 ce.canAddrEnc(e, v, opts) 793 } else { 794 ce.elseEnc(e, v, opts) 795 } 796 } 797 798 // newCondAddrEncoder returns an encoder that checks whether its value 799 // CanAddr and delegates to canAddrEnc if so, else to elseEnc. 800 func newCondAddrEncoder(canAddrEnc, elseEnc encoderFunc) encoderFunc { 801 enc := &condAddrEncoder{canAddrEnc: canAddrEnc, elseEnc: elseEnc} 802 return enc.encode 803 } 804 805 func isValidTag(s string) bool { 806 if s == "" { 807 return false 808 } 809 for _, c := range s { 810 switch { 811 case strings.ContainsRune("!#$%&()*+-./:<=>?@[]^_{|}~ ", c): 812 // Backslash and quote chars are reserved, but 813 // otherwise any punctuation chars are allowed 814 // in a tag name. 815 default: 816 if !unicode.IsLetter(c) && !unicode.IsDigit(c) { 817 return false 818 } 819 } 820 } 821 return true 822 } 823 824 func fieldByIndex(v reflect.Value, index []int) reflect.Value { 825 for _, i := range index { 826 if v.Kind() == reflect.Ptr { 827 if v.IsNil() { 828 return reflect.Value{} 829 } 830 v = v.Elem() 831 } 832 v = v.Field(i) 833 } 834 return v 835 } 836 837 func typeByIndex(t reflect.Type, index []int) reflect.Type { 838 for _, i := range index { 839 if t.Kind() == reflect.Ptr { 840 t = t.Elem() 841 } 842 t = t.Field(i).Type 843 } 844 return t 845 } 846 847 type reflectWithString struct { 848 v reflect.Value 849 s string 850 } 851 852 func (w *reflectWithString) resolve() error { 853 if w.v.Kind() == reflect.String { 854 w.s = w.v.String() 855 return nil 856 } 857 if tm, ok := w.v.Interface().(encoding.TextMarshaler); ok { 858 buf, err := tm.MarshalText() 859 w.s = string(buf) 860 return err 861 } 862 switch w.v.Kind() { 863 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 864 w.s = strconv.FormatInt(w.v.Int(), 10) 865 return nil 866 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 867 w.s = strconv.FormatUint(w.v.Uint(), 10) 868 return nil 869 } 870 panic("unexpected map key type") 871 } 872 873 // NOTE: keep in sync with stringBytes below. 874 func (e *encodeState) string(s string, escapeHTML bool) { 875 e.WriteByte('"') 876 start := 0 877 for i := 0; i < len(s); { 878 if b := s[i]; b < utf8.RuneSelf { 879 if htmlSafeSet[b] || (!escapeHTML && safeSet[b]) { 880 i++ 881 continue 882 } 883 if start < i { 884 e.WriteString(s[start:i]) 885 } 886 switch b { 887 case '\\', '"': 888 e.WriteByte('\\') 889 e.WriteByte(b) 890 case '\n': 891 e.WriteByte('\\') 892 e.WriteByte('n') 893 case '\r': 894 e.WriteByte('\\') 895 e.WriteByte('r') 896 case '\t': 897 e.WriteByte('\\') 898 e.WriteByte('t') 899 default: 900 // This encodes bytes < 0x20 except for \t, \n and \r. 901 // If escapeHTML is set, it also escapes <, >, and & 902 // because they can lead to security holes when 903 // user-controlled strings are rendered into JSON 904 // and served to some browsers. 905 e.WriteString(`\u00`) 906 e.WriteByte(hex[b>>4]) 907 e.WriteByte(hex[b&0xF]) 908 } 909 i++ 910 start = i 911 continue 912 } 913 c, size := utf8.DecodeRuneInString(s[i:]) 914 if c == utf8.RuneError && size == 1 { 915 if start < i { 916 e.WriteString(s[start:i]) 917 } 918 e.WriteString(`\ufffd`) 919 i += size 920 start = i 921 continue 922 } 923 // U+2028 is LINE SEPARATOR. 924 // U+2029 is PARAGRAPH SEPARATOR. 925 // They are both technically valid characters in JSON strings, 926 // but don't work in JSONP, which has to be evaluated as JavaScript, 927 // and can lead to security holes there. It is valid JSON to 928 // escape them, so we do so unconditionally. 929 // See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion. 930 if c == '\u2028' || c == '\u2029' { 931 if start < i { 932 e.WriteString(s[start:i]) 933 } 934 e.WriteString(`\u202`) 935 e.WriteByte(hex[c&0xF]) 936 i += size 937 start = i 938 continue 939 } 940 i += size 941 } 942 if start < len(s) { 943 e.WriteString(s[start:]) 944 } 945 e.WriteByte('"') 946 } 947 948 // NOTE: keep in sync with string above. 949 func (e *encodeState) stringBytes(s []byte, escapeHTML bool) { 950 e.WriteByte('"') 951 start := 0 952 for i := 0; i < len(s); { 953 if b := s[i]; b < utf8.RuneSelf { 954 if htmlSafeSet[b] || (!escapeHTML && safeSet[b]) { 955 i++ 956 continue 957 } 958 if start < i { 959 e.Write(s[start:i]) 960 } 961 switch b { 962 case '\\', '"': 963 e.WriteByte('\\') 964 e.WriteByte(b) 965 case '\n': 966 e.WriteByte('\\') 967 e.WriteByte('n') 968 case '\r': 969 e.WriteByte('\\') 970 e.WriteByte('r') 971 case '\t': 972 e.WriteByte('\\') 973 e.WriteByte('t') 974 default: 975 // This encodes bytes < 0x20 except for \t, \n and \r. 976 // If escapeHTML is set, it also escapes <, >, and & 977 // because they can lead to security holes when 978 // user-controlled strings are rendered into JSON 979 // and served to some browsers. 980 e.WriteString(`\u00`) 981 e.WriteByte(hex[b>>4]) 982 e.WriteByte(hex[b&0xF]) 983 } 984 i++ 985 start = i 986 continue 987 } 988 c, size := utf8.DecodeRune(s[i:]) 989 if c == utf8.RuneError && size == 1 { 990 if start < i { 991 e.Write(s[start:i]) 992 } 993 e.WriteString(`\ufffd`) 994 i += size 995 start = i 996 continue 997 } 998 // U+2028 is LINE SEPARATOR. 999 // U+2029 is PARAGRAPH SEPARATOR. 1000 // They are both technically valid characters in JSON strings, 1001 // but don't work in JSONP, which has to be evaluated as JavaScript, 1002 // and can lead to security holes there. It is valid JSON to 1003 // escape them, so we do so unconditionally. 1004 // See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion. 1005 if c == '\u2028' || c == '\u2029' { 1006 if start < i { 1007 e.Write(s[start:i]) 1008 } 1009 e.WriteString(`\u202`) 1010 e.WriteByte(hex[c&0xF]) 1011 i += size 1012 start = i 1013 continue 1014 } 1015 i += size 1016 } 1017 if start < len(s) { 1018 e.Write(s[start:]) 1019 } 1020 e.WriteByte('"') 1021 } 1022 1023 // A field represents a single field found in a struct. 1024 type field struct { 1025 name string 1026 nameBytes []byte // []byte(name) 1027 equalFold func(s, t []byte) bool // bytes.EqualFold or equivalent 1028 1029 tag bool 1030 index []int 1031 typ reflect.Type 1032 omitEmpty bool 1033 quoted bool 1034 } 1035 1036 func fillField(f field) field { 1037 f.nameBytes = []byte(f.name) 1038 f.equalFold = foldFunc(f.nameBytes) 1039 return f 1040 } 1041 1042 // byIndex sorts field by index sequence. 1043 type byIndex []field 1044 1045 func (x byIndex) Len() int { return len(x) } 1046 1047 func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 1048 1049 func (x byIndex) Less(i, j int) bool { 1050 for k, xik := range x[i].index { 1051 if k >= len(x[j].index) { 1052 return false 1053 } 1054 if xik != x[j].index[k] { 1055 return xik < x[j].index[k] 1056 } 1057 } 1058 return len(x[i].index) < len(x[j].index) 1059 } 1060 1061 // typeFields returns a list of fields that JSON should recognize for the given type. 1062 // The algorithm is breadth-first search over the set of structs to include - the top struct 1063 // and then any reachable anonymous structs. 1064 func typeFields(t reflect.Type) []field { 1065 // Anonymous fields to explore at the current level and the next. 1066 current := []field{} 1067 next := []field{{typ: t}} 1068 1069 // Count of queued names for current level and the next. 1070 count := map[reflect.Type]int{} 1071 nextCount := map[reflect.Type]int{} 1072 1073 // Types already visited at an earlier level. 1074 visited := map[reflect.Type]bool{} 1075 1076 // Fields found. 1077 var fields []field 1078 1079 for len(next) > 0 { 1080 current, next = next, current[:0] 1081 count, nextCount = nextCount, map[reflect.Type]int{} 1082 1083 for _, f := range current { 1084 if visited[f.typ] { 1085 continue 1086 } 1087 visited[f.typ] = true 1088 1089 // Scan f.typ for fields to include. 1090 for i := 0; i < f.typ.NumField(); i++ { 1091 sf := f.typ.Field(i) 1092 if sf.Anonymous { 1093 t := sf.Type 1094 if t.Kind() == reflect.Ptr { 1095 t = t.Elem() 1096 } 1097 // If embedded, StructField.PkgPath is not a reliable 1098 // indicator of whether the field is exported. 1099 // See https://golang.org/issue/21122 1100 if !isExported(t.Name()) && t.Kind() != reflect.Struct { 1101 // Ignore embedded fields of unexported non-struct types. 1102 // Do not ignore embedded fields of unexported struct types 1103 // since they may have exported fields. 1104 continue 1105 } 1106 } else if sf.PkgPath != "" { 1107 // Ignore unexported non-embedded fields. 1108 continue 1109 } 1110 tag := sf.Tag.Get("json") 1111 if tag == "-" { 1112 continue 1113 } 1114 name, opts := parseTag(tag) 1115 if !isValidTag(name) { 1116 name = "" 1117 } 1118 index := make([]int, len(f.index)+1) 1119 copy(index, f.index) 1120 index[len(f.index)] = i 1121 1122 ft := sf.Type 1123 if ft.Name() == "" && ft.Kind() == reflect.Ptr { 1124 // Follow pointer. 1125 ft = ft.Elem() 1126 } 1127 1128 // Only strings, floats, integers, and booleans can be quoted. 1129 quoted := false 1130 if opts.Contains("string") { 1131 switch ft.Kind() { 1132 case reflect.Bool, 1133 reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, 1134 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, 1135 reflect.Float32, reflect.Float64, 1136 reflect.String: 1137 quoted = true 1138 } 1139 } 1140 1141 // Record found field and index sequence. 1142 if name != "" || !sf.Anonymous || ft.Kind() != reflect.Struct { 1143 tagged := name != "" 1144 if name == "" { 1145 name = sf.Name 1146 } 1147 fields = append(fields, fillField(field{ 1148 name: name, 1149 tag: tagged, 1150 index: index, 1151 typ: ft, 1152 omitEmpty: opts.Contains("omitempty"), 1153 quoted: quoted, 1154 })) 1155 if count[f.typ] > 1 { 1156 // If there were multiple instances, add a second, 1157 // so that the annihilation code will see a duplicate. 1158 // It only cares about the distinction between 1 or 2, 1159 // so don't bother generating any more copies. 1160 fields = append(fields, fields[len(fields)-1]) 1161 } 1162 continue 1163 } 1164 1165 // Record new anonymous struct to explore in next round. 1166 nextCount[ft]++ 1167 if nextCount[ft] == 1 { 1168 next = append(next, fillField(field{name: ft.Name(), index: index, typ: ft})) 1169 } 1170 } 1171 } 1172 } 1173 1174 sort.Slice(fields, func(i, j int) bool { 1175 x := fields 1176 // sort field by name, breaking ties with depth, then 1177 // breaking ties with "name came from json tag", then 1178 // breaking ties with index sequence. 1179 if x[i].name != x[j].name { 1180 return x[i].name < x[j].name 1181 } 1182 if len(x[i].index) != len(x[j].index) { 1183 return len(x[i].index) < len(x[j].index) 1184 } 1185 if x[i].tag != x[j].tag { 1186 return x[i].tag 1187 } 1188 return byIndex(x).Less(i, j) 1189 }) 1190 1191 // Delete all fields that are hidden by the Go rules for embedded fields, 1192 // except that fields with JSON tags are promoted. 1193 1194 // The fields are sorted in primary order of name, secondary order 1195 // of field index length. Loop over names; for each name, delete 1196 // hidden fields by choosing the one dominant field that survives. 1197 out := fields[:0] 1198 for advance, i := 0, 0; i < len(fields); i += advance { 1199 // One iteration per name. 1200 // Find the sequence of fields with the name of this first field. 1201 fi := fields[i] 1202 name := fi.name 1203 for advance = 1; i+advance < len(fields); advance++ { 1204 fj := fields[i+advance] 1205 if fj.name != name { 1206 break 1207 } 1208 } 1209 if advance == 1 { // Only one field with this name 1210 out = append(out, fi) 1211 continue 1212 } 1213 dominant, ok := dominantField(fields[i : i+advance]) 1214 if ok { 1215 out = append(out, dominant) 1216 } 1217 } 1218 1219 fields = out 1220 sort.Sort(byIndex(fields)) 1221 1222 return fields 1223 } 1224 1225 // isExported reports whether the identifier is exported. 1226 func isExported(id string) bool { 1227 r, _ := utf8.DecodeRuneInString(id) 1228 return unicode.IsUpper(r) 1229 } 1230 1231 // dominantField looks through the fields, all of which are known to 1232 // have the same name, to find the single field that dominates the 1233 // others using Go's embedding rules, modified by the presence of 1234 // JSON tags. If there are multiple top-level fields, the boolean 1235 // will be false: This condition is an error in Go and we skip all 1236 // the fields. 1237 func dominantField(fields []field) (field, bool) { 1238 // The fields are sorted in increasing index-length order. The winner 1239 // must therefore be one with the shortest index length. Drop all 1240 // longer entries, which is easy: just truncate the slice. 1241 length := len(fields[0].index) 1242 tagged := -1 // Index of first tagged field. 1243 for i, f := range fields { 1244 if len(f.index) > length { 1245 fields = fields[:i] 1246 break 1247 } 1248 if f.tag { 1249 if tagged >= 0 { 1250 // Multiple tagged fields at the same level: conflict. 1251 // Return no field. 1252 return field{}, false 1253 } 1254 tagged = i 1255 } 1256 } 1257 if tagged >= 0 { 1258 return fields[tagged], true 1259 } 1260 // All remaining fields have the same length. If there's more than one, 1261 // we have a conflict (two fields named "X" at the same level) and we 1262 // return no field. 1263 if len(fields) > 1 { 1264 return field{}, false 1265 } 1266 return fields[0], true 1267 } 1268 1269 var fieldCache struct { 1270 value atomic.Value // map[reflect.Type][]field 1271 mu sync.Mutex // used only by writers 1272 } 1273 1274 // cachedTypeFields is like typeFields but uses a cache to avoid repeated work. 1275 func cachedTypeFields(t reflect.Type) []field { 1276 m, _ := fieldCache.value.Load().(map[reflect.Type][]field) 1277 f := m[t] 1278 if f != nil { 1279 return f 1280 } 1281 1282 // Compute fields without lock. 1283 // Might duplicate effort but won't hold other computations back. 1284 f = typeFields(t) 1285 if f == nil { 1286 f = []field{} 1287 } 1288 1289 fieldCache.mu.Lock() 1290 m, _ = fieldCache.value.Load().(map[reflect.Type][]field) 1291 newM := make(map[reflect.Type][]field, len(m)+1) 1292 for k, v := range m { 1293 newM[k] = v 1294 } 1295 newM[t] = f 1296 fieldCache.value.Store(newM) 1297 fieldCache.mu.Unlock() 1298 return f 1299 }