github.com/eun/go@v0.0.0-20170811110501-92cfd07a6cfd/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) int { 875 len0 := e.Len() 876 e.WriteByte('"') 877 start := 0 878 for i := 0; i < len(s); { 879 if b := s[i]; b < utf8.RuneSelf { 880 if htmlSafeSet[b] || (!escapeHTML && safeSet[b]) { 881 i++ 882 continue 883 } 884 if start < i { 885 e.WriteString(s[start:i]) 886 } 887 switch b { 888 case '\\', '"': 889 e.WriteByte('\\') 890 e.WriteByte(b) 891 case '\n': 892 e.WriteByte('\\') 893 e.WriteByte('n') 894 case '\r': 895 e.WriteByte('\\') 896 e.WriteByte('r') 897 case '\t': 898 e.WriteByte('\\') 899 e.WriteByte('t') 900 default: 901 // This encodes bytes < 0x20 except for \t, \n and \r. 902 // If escapeHTML is set, it also escapes <, >, and & 903 // because they can lead to security holes when 904 // user-controlled strings are rendered into JSON 905 // and served to some browsers. 906 e.WriteString(`\u00`) 907 e.WriteByte(hex[b>>4]) 908 e.WriteByte(hex[b&0xF]) 909 } 910 i++ 911 start = i 912 continue 913 } 914 c, size := utf8.DecodeRuneInString(s[i:]) 915 if c == utf8.RuneError && size == 1 { 916 if start < i { 917 e.WriteString(s[start:i]) 918 } 919 e.WriteString(`\ufffd`) 920 i += size 921 start = i 922 continue 923 } 924 // U+2028 is LINE SEPARATOR. 925 // U+2029 is PARAGRAPH SEPARATOR. 926 // They are both technically valid characters in JSON strings, 927 // but don't work in JSONP, which has to be evaluated as JavaScript, 928 // and can lead to security holes there. It is valid JSON to 929 // escape them, so we do so unconditionally. 930 // See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion. 931 if c == '\u2028' || c == '\u2029' { 932 if start < i { 933 e.WriteString(s[start:i]) 934 } 935 e.WriteString(`\u202`) 936 e.WriteByte(hex[c&0xF]) 937 i += size 938 start = i 939 continue 940 } 941 i += size 942 } 943 if start < len(s) { 944 e.WriteString(s[start:]) 945 } 946 e.WriteByte('"') 947 return e.Len() - len0 948 } 949 950 // NOTE: keep in sync with string above. 951 func (e *encodeState) stringBytes(s []byte, escapeHTML bool) int { 952 len0 := e.Len() 953 e.WriteByte('"') 954 start := 0 955 for i := 0; i < len(s); { 956 if b := s[i]; b < utf8.RuneSelf { 957 if htmlSafeSet[b] || (!escapeHTML && safeSet[b]) { 958 i++ 959 continue 960 } 961 if start < i { 962 e.Write(s[start:i]) 963 } 964 switch b { 965 case '\\', '"': 966 e.WriteByte('\\') 967 e.WriteByte(b) 968 case '\n': 969 e.WriteByte('\\') 970 e.WriteByte('n') 971 case '\r': 972 e.WriteByte('\\') 973 e.WriteByte('r') 974 case '\t': 975 e.WriteByte('\\') 976 e.WriteByte('t') 977 default: 978 // This encodes bytes < 0x20 except for \t, \n and \r. 979 // If escapeHTML is set, it also escapes <, >, and & 980 // because they can lead to security holes when 981 // user-controlled strings are rendered into JSON 982 // and served to some browsers. 983 e.WriteString(`\u00`) 984 e.WriteByte(hex[b>>4]) 985 e.WriteByte(hex[b&0xF]) 986 } 987 i++ 988 start = i 989 continue 990 } 991 c, size := utf8.DecodeRune(s[i:]) 992 if c == utf8.RuneError && size == 1 { 993 if start < i { 994 e.Write(s[start:i]) 995 } 996 e.WriteString(`\ufffd`) 997 i += size 998 start = i 999 continue 1000 } 1001 // U+2028 is LINE SEPARATOR. 1002 // U+2029 is PARAGRAPH SEPARATOR. 1003 // They are both technically valid characters in JSON strings, 1004 // but don't work in JSONP, which has to be evaluated as JavaScript, 1005 // and can lead to security holes there. It is valid JSON to 1006 // escape them, so we do so unconditionally. 1007 // See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion. 1008 if c == '\u2028' || c == '\u2029' { 1009 if start < i { 1010 e.Write(s[start:i]) 1011 } 1012 e.WriteString(`\u202`) 1013 e.WriteByte(hex[c&0xF]) 1014 i += size 1015 start = i 1016 continue 1017 } 1018 i += size 1019 } 1020 if start < len(s) { 1021 e.Write(s[start:]) 1022 } 1023 e.WriteByte('"') 1024 return e.Len() - len0 1025 } 1026 1027 // A field represents a single field found in a struct. 1028 type field struct { 1029 name string 1030 nameBytes []byte // []byte(name) 1031 equalFold func(s, t []byte) bool // bytes.EqualFold or equivalent 1032 1033 tag bool 1034 index []int 1035 typ reflect.Type 1036 omitEmpty bool 1037 quoted bool 1038 } 1039 1040 func fillField(f field) field { 1041 f.nameBytes = []byte(f.name) 1042 f.equalFold = foldFunc(f.nameBytes) 1043 return f 1044 } 1045 1046 // byIndex sorts field by index sequence. 1047 type byIndex []field 1048 1049 func (x byIndex) Len() int { return len(x) } 1050 1051 func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 1052 1053 func (x byIndex) Less(i, j int) bool { 1054 for k, xik := range x[i].index { 1055 if k >= len(x[j].index) { 1056 return false 1057 } 1058 if xik != x[j].index[k] { 1059 return xik < x[j].index[k] 1060 } 1061 } 1062 return len(x[i].index) < len(x[j].index) 1063 } 1064 1065 // typeFields returns a list of fields that JSON should recognize for the given type. 1066 // The algorithm is breadth-first search over the set of structs to include - the top struct 1067 // and then any reachable anonymous structs. 1068 func typeFields(t reflect.Type) []field { 1069 // Anonymous fields to explore at the current level and the next. 1070 current := []field{} 1071 next := []field{{typ: t}} 1072 1073 // Count of queued names for current level and the next. 1074 count := map[reflect.Type]int{} 1075 nextCount := map[reflect.Type]int{} 1076 1077 // Types already visited at an earlier level. 1078 visited := map[reflect.Type]bool{} 1079 1080 // Fields found. 1081 var fields []field 1082 1083 for len(next) > 0 { 1084 current, next = next, current[:0] 1085 count, nextCount = nextCount, map[reflect.Type]int{} 1086 1087 for _, f := range current { 1088 if visited[f.typ] { 1089 continue 1090 } 1091 visited[f.typ] = true 1092 1093 // Scan f.typ for fields to include. 1094 for i := 0; i < f.typ.NumField(); i++ { 1095 sf := f.typ.Field(i) 1096 if sf.Anonymous { 1097 t := sf.Type 1098 if t.Kind() == reflect.Ptr { 1099 t = t.Elem() 1100 } 1101 // If embedded, StructField.PkgPath is not a reliable 1102 // indicator of whether the field is exported. 1103 // See https://golang.org/issue/21122 1104 if !isExported(t.Name()) && t.Kind() != reflect.Struct { 1105 // Ignore embedded fields of unexported non-struct types. 1106 // Do not ignore embedded fields of unexported struct types 1107 // since they may have exported fields. 1108 continue 1109 } 1110 } else if sf.PkgPath != "" { 1111 // Ignore unexported non-embedded fields. 1112 continue 1113 } 1114 tag := sf.Tag.Get("json") 1115 if tag == "-" { 1116 continue 1117 } 1118 name, opts := parseTag(tag) 1119 if !isValidTag(name) { 1120 name = "" 1121 } 1122 index := make([]int, len(f.index)+1) 1123 copy(index, f.index) 1124 index[len(f.index)] = i 1125 1126 ft := sf.Type 1127 if ft.Name() == "" && ft.Kind() == reflect.Ptr { 1128 // Follow pointer. 1129 ft = ft.Elem() 1130 } 1131 1132 // Only strings, floats, integers, and booleans can be quoted. 1133 quoted := false 1134 if opts.Contains("string") { 1135 switch ft.Kind() { 1136 case reflect.Bool, 1137 reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, 1138 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, 1139 reflect.Float32, reflect.Float64, 1140 reflect.String: 1141 quoted = true 1142 } 1143 } 1144 1145 // Record found field and index sequence. 1146 if name != "" || !sf.Anonymous || ft.Kind() != reflect.Struct { 1147 tagged := name != "" 1148 if name == "" { 1149 name = sf.Name 1150 } 1151 fields = append(fields, fillField(field{ 1152 name: name, 1153 tag: tagged, 1154 index: index, 1155 typ: ft, 1156 omitEmpty: opts.Contains("omitempty"), 1157 quoted: quoted, 1158 })) 1159 if count[f.typ] > 1 { 1160 // If there were multiple instances, add a second, 1161 // so that the annihilation code will see a duplicate. 1162 // It only cares about the distinction between 1 or 2, 1163 // so don't bother generating any more copies. 1164 fields = append(fields, fields[len(fields)-1]) 1165 } 1166 continue 1167 } 1168 1169 // Record new anonymous struct to explore in next round. 1170 nextCount[ft]++ 1171 if nextCount[ft] == 1 { 1172 next = append(next, fillField(field{name: ft.Name(), index: index, typ: ft})) 1173 } 1174 } 1175 } 1176 } 1177 1178 sort.Slice(fields, func(i, j int) bool { 1179 x := fields 1180 // sort field by name, breaking ties with depth, then 1181 // breaking ties with "name came from json tag", then 1182 // breaking ties with index sequence. 1183 if x[i].name != x[j].name { 1184 return x[i].name < x[j].name 1185 } 1186 if len(x[i].index) != len(x[j].index) { 1187 return len(x[i].index) < len(x[j].index) 1188 } 1189 if x[i].tag != x[j].tag { 1190 return x[i].tag 1191 } 1192 return byIndex(x).Less(i, j) 1193 }) 1194 1195 // Delete all fields that are hidden by the Go rules for embedded fields, 1196 // except that fields with JSON tags are promoted. 1197 1198 // The fields are sorted in primary order of name, secondary order 1199 // of field index length. Loop over names; for each name, delete 1200 // hidden fields by choosing the one dominant field that survives. 1201 out := fields[:0] 1202 for advance, i := 0, 0; i < len(fields); i += advance { 1203 // One iteration per name. 1204 // Find the sequence of fields with the name of this first field. 1205 fi := fields[i] 1206 name := fi.name 1207 for advance = 1; i+advance < len(fields); advance++ { 1208 fj := fields[i+advance] 1209 if fj.name != name { 1210 break 1211 } 1212 } 1213 if advance == 1 { // Only one field with this name 1214 out = append(out, fi) 1215 continue 1216 } 1217 dominant, ok := dominantField(fields[i : i+advance]) 1218 if ok { 1219 out = append(out, dominant) 1220 } 1221 } 1222 1223 fields = out 1224 sort.Sort(byIndex(fields)) 1225 1226 return fields 1227 } 1228 1229 // isExported reports whether the identifier is exported. 1230 func isExported(id string) bool { 1231 r, _ := utf8.DecodeRuneInString(id) 1232 return unicode.IsUpper(r) 1233 } 1234 1235 // dominantField looks through the fields, all of which are known to 1236 // have the same name, to find the single field that dominates the 1237 // others using Go's embedding rules, modified by the presence of 1238 // JSON tags. If there are multiple top-level fields, the boolean 1239 // will be false: This condition is an error in Go and we skip all 1240 // the fields. 1241 func dominantField(fields []field) (field, bool) { 1242 // The fields are sorted in increasing index-length order. The winner 1243 // must therefore be one with the shortest index length. Drop all 1244 // longer entries, which is easy: just truncate the slice. 1245 length := len(fields[0].index) 1246 tagged := -1 // Index of first tagged field. 1247 for i, f := range fields { 1248 if len(f.index) > length { 1249 fields = fields[:i] 1250 break 1251 } 1252 if f.tag { 1253 if tagged >= 0 { 1254 // Multiple tagged fields at the same level: conflict. 1255 // Return no field. 1256 return field{}, false 1257 } 1258 tagged = i 1259 } 1260 } 1261 if tagged >= 0 { 1262 return fields[tagged], true 1263 } 1264 // All remaining fields have the same length. If there's more than one, 1265 // we have a conflict (two fields named "X" at the same level) and we 1266 // return no field. 1267 if len(fields) > 1 { 1268 return field{}, false 1269 } 1270 return fields[0], true 1271 } 1272 1273 var fieldCache struct { 1274 value atomic.Value // map[reflect.Type][]field 1275 mu sync.Mutex // used only by writers 1276 } 1277 1278 // cachedTypeFields is like typeFields but uses a cache to avoid repeated work. 1279 func cachedTypeFields(t reflect.Type) []field { 1280 m, _ := fieldCache.value.Load().(map[reflect.Type][]field) 1281 f := m[t] 1282 if f != nil { 1283 return f 1284 } 1285 1286 // Compute fields without lock. 1287 // Might duplicate effort but won't hold other computations back. 1288 f = typeFields(t) 1289 if f == nil { 1290 f = []field{} 1291 } 1292 1293 fieldCache.mu.Lock() 1294 m, _ = fieldCache.value.Load().(map[reflect.Type][]field) 1295 newM := make(map[reflect.Type][]field, len(m)+1) 1296 for k, v := range m { 1297 newM[k] = v 1298 } 1299 newM[t] = f 1300 fieldCache.value.Store(newM) 1301 fieldCache.mu.Unlock() 1302 return f 1303 }