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