github.com/corona10/go@v0.0.0-20180224231303-7a218942be57/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 7159. 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 "sort" 21 "strconv" 22 "strings" 23 "sync" 24 "sync/atomic" 25 "unicode" 26 "unicode/utf8" 27 ) 28 29 // Marshal returns the JSON encoding of v. 30 // 31 // Marshal traverses the value v recursively. 32 // If an encountered value implements the Marshaler interface 33 // and is not a nil pointer, Marshal calls its MarshalJSON method 34 // to produce JSON. If no MarshalJSON method is present but the 35 // value implements encoding.TextMarshaler instead, Marshal calls 36 // its MarshalText method and encodes the result as a JSON string. 37 // The nil pointer exception is not strictly necessary 38 // but mimics a similar, necessary exception in the behavior of 39 // UnmarshalJSON. 40 // 41 // Otherwise, Marshal uses the following type-dependent default encodings: 42 // 43 // Boolean values encode as JSON booleans. 44 // 45 // Floating point, integer, and Number values encode as JSON numbers. 46 // 47 // String values encode as JSON strings coerced to valid UTF-8, 48 // replacing invalid bytes with the Unicode replacement rune. 49 // The angle brackets "<" and ">" are escaped to "\u003c" and "\u003e" 50 // to keep some browsers from misinterpreting JSON output as HTML. 51 // Ampersand "&" is also escaped to "\u0026" for the same reason. 52 // This escaping can be disabled using an Encoder that had SetEscapeHTML(false) 53 // called on it. 54 // 55 // Array and slice values encode as JSON arrays, except that 56 // []byte encodes as a base64-encoded string, and a nil slice 57 // encodes as the null JSON value. 58 // 59 // Struct values encode as JSON objects. 60 // Each exported struct field becomes a member of the object, using the 61 // field name as the object key, unless the field is omitted for one of the 62 // reasons given below. 63 // 64 // The encoding of each struct field can be customized by the format string 65 // stored under the "json" key in the struct field's tag. 66 // The format string gives the name of the field, possibly followed by a 67 // comma-separated list of options. The name may be empty in order to 68 // specify options without overriding the default field name. 69 // 70 // The "omitempty" option specifies that the field should be omitted 71 // from the encoding if the field has an empty value, defined as 72 // false, 0, a nil pointer, a nil interface value, and any empty array, 73 // slice, map, or string. 74 // 75 // As a special case, if the field tag is "-", the field is always omitted. 76 // Note that a field with name "-" can still be generated using the tag "-,". 77 // 78 // Examples of struct field tags and their meanings: 79 // 80 // // Field appears in JSON as key "myName". 81 // Field int `json:"myName"` 82 // 83 // // Field appears in JSON as key "myName" and 84 // // the field is omitted from the object if its value is empty, 85 // // as defined above. 86 // Field int `json:"myName,omitempty"` 87 // 88 // // Field appears in JSON as key "Field" (the default), but 89 // // the field is skipped if empty. 90 // // Note the leading comma. 91 // Field int `json:",omitempty"` 92 // 93 // // Field is ignored by this package. 94 // Field int `json:"-"` 95 // 96 // // Field appears in JSON as key "-". 97 // Field int `json:"-,"` 98 // 99 // The "string" option signals that a field is stored as JSON inside a 100 // JSON-encoded string. It applies only to fields of string, floating point, 101 // integer, or boolean types. This extra level of encoding is sometimes used 102 // when communicating with JavaScript programs: 103 // 104 // Int64String int64 `json:",string"` 105 // 106 // The key name will be used if it's a non-empty string consisting of 107 // only Unicode letters, digits, and ASCII punctuation except quotation 108 // marks, backslash, and comma. 109 // 110 // Anonymous struct fields are usually marshaled as if their inner exported fields 111 // were fields in the outer struct, subject to the usual Go visibility rules amended 112 // as described in the next paragraph. 113 // An anonymous struct field with a name given in its JSON tag is treated as 114 // having that name, rather than being anonymous. 115 // An anonymous struct field of interface type is treated the same as having 116 // that type as its name, rather than being anonymous. 117 // 118 // The Go visibility rules for struct fields are amended for JSON when 119 // deciding which field to marshal or unmarshal. If there are 120 // multiple fields at the same level, and that level is the least 121 // nested (and would therefore be the nesting level selected by the 122 // usual Go rules), the following extra rules apply: 123 // 124 // 1) Of those fields, if any are JSON-tagged, only tagged fields are considered, 125 // even if there are multiple untagged fields that would otherwise conflict. 126 // 127 // 2) If there is exactly one field (tagged or not according to the first rule), that is selected. 128 // 129 // 3) Otherwise there are multiple fields, and all are ignored; no error occurs. 130 // 131 // Handling of anonymous struct fields is new in Go 1.1. 132 // Prior to Go 1.1, anonymous struct fields were ignored. To force ignoring of 133 // an anonymous struct field in both current and earlier versions, give the field 134 // a JSON tag of "-". 135 // 136 // Map values encode as JSON objects. The map's key type must either be a 137 // string, an integer type, or implement encoding.TextMarshaler. The map keys 138 // are sorted and used as JSON object keys by applying the following rules, 139 // subject to the UTF-8 coercion described for string values above: 140 // - string keys are used directly 141 // - encoding.TextMarshalers are marshaled 142 // - integer keys are converted to strings 143 // 144 // Pointer values encode as the value pointed to. 145 // A nil pointer encodes as the null JSON value. 146 // 147 // Interface values encode as the value contained in the interface. 148 // A nil interface value encodes as the null JSON value. 149 // 150 // Channel, complex, and function values cannot be encoded in JSON. 151 // Attempting to encode such a value causes Marshal to return 152 // an UnsupportedTypeError. 153 // 154 // JSON cannot represent cyclic data structures and Marshal does not 155 // handle them. Passing cyclic structures to Marshal will result in 156 // an infinite recursion. 157 // 158 func Marshal(v interface{}) ([]byte, error) { 159 e := &encodeState{} 160 err := e.marshal(v, encOpts{escapeHTML: true}) 161 if err != nil { 162 return nil, err 163 } 164 return e.Bytes(), nil 165 } 166 167 // MarshalIndent is like Marshal but applies Indent to format the output. 168 // Each JSON element in the output will begin on a new line beginning with prefix 169 // followed by one or more copies of indent according to the indentation nesting. 170 func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) { 171 b, err := Marshal(v) 172 if err != nil { 173 return nil, err 174 } 175 var buf bytes.Buffer 176 err = Indent(&buf, b, prefix, indent) 177 if err != nil { 178 return nil, err 179 } 180 return buf.Bytes(), nil 181 } 182 183 // HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029 184 // characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029 185 // so that the JSON will be safe to embed inside HTML <script> tags. 186 // For historical reasons, web browsers don't honor standard HTML 187 // escaping within <script> tags, so an alternative JSON encoding must 188 // be used. 189 func HTMLEscape(dst *bytes.Buffer, src []byte) { 190 // The characters can only appear in string literals, 191 // so just scan the string one byte at a time. 192 start := 0 193 for i, c := range src { 194 if c == '<' || c == '>' || c == '&' { 195 if start < i { 196 dst.Write(src[start:i]) 197 } 198 dst.WriteString(`\u00`) 199 dst.WriteByte(hex[c>>4]) 200 dst.WriteByte(hex[c&0xF]) 201 start = i + 1 202 } 203 // Convert U+2028 and U+2029 (E2 80 A8 and E2 80 A9). 204 if c == 0xE2 && i+2 < len(src) && src[i+1] == 0x80 && src[i+2]&^1 == 0xA8 { 205 if start < i { 206 dst.Write(src[start:i]) 207 } 208 dst.WriteString(`\u202`) 209 dst.WriteByte(hex[src[i+2]&0xF]) 210 start = i + 3 211 } 212 } 213 if start < len(src) { 214 dst.Write(src[start:]) 215 } 216 } 217 218 // Marshaler is the interface implemented by types that 219 // can marshal themselves into valid JSON. 220 type Marshaler interface { 221 MarshalJSON() ([]byte, error) 222 } 223 224 // An UnsupportedTypeError is returned by Marshal when attempting 225 // to encode an unsupported value type. 226 type UnsupportedTypeError struct { 227 Type reflect.Type 228 } 229 230 func (e *UnsupportedTypeError) Error() string { 231 return "json: unsupported type: " + e.Type.String() 232 } 233 234 type UnsupportedValueError struct { 235 Value reflect.Value 236 Str string 237 } 238 239 func (e *UnsupportedValueError) Error() string { 240 return "json: unsupported value: " + e.Str 241 } 242 243 // Before Go 1.2, an InvalidUTF8Error was returned by Marshal when 244 // attempting to encode a string value with invalid UTF-8 sequences. 245 // As of Go 1.2, Marshal instead coerces the string to valid UTF-8 by 246 // replacing invalid bytes with the Unicode replacement rune U+FFFD. 247 // 248 // Deprecated: No longer used; kept for compatibility. 249 type InvalidUTF8Error struct { 250 S string // the whole string value that caused the error 251 } 252 253 func (e *InvalidUTF8Error) Error() string { 254 return "json: invalid UTF-8 in string: " + strconv.Quote(e.S) 255 } 256 257 type MarshalerError struct { 258 Type reflect.Type 259 Err error 260 } 261 262 func (e *MarshalerError) Error() string { 263 return "json: error calling MarshalJSON for type " + e.Type.String() + ": " + e.Err.Error() 264 } 265 266 var hex = "0123456789abcdef" 267 268 // An encodeState encodes JSON into a bytes.Buffer. 269 type encodeState struct { 270 bytes.Buffer // accumulated output 271 scratch [64]byte 272 } 273 274 var encodeStatePool sync.Pool 275 276 func newEncodeState() *encodeState { 277 if v := encodeStatePool.Get(); v != nil { 278 e := v.(*encodeState) 279 e.Reset() 280 return e 281 } 282 return new(encodeState) 283 } 284 285 func (e *encodeState) marshal(v interface{}, opts encOpts) (err error) { 286 defer func() { 287 if r := recover(); r != nil { 288 if je, ok := r.(jsonError); ok { 289 err = je.error 290 } else { 291 panic(r) 292 } 293 } 294 }() 295 e.reflectValue(reflect.ValueOf(v), opts) 296 return nil 297 } 298 299 // error aborts the encoding by panicking with err wrapped in jsonError. 300 func (e *encodeState) error(err error) { 301 panic(jsonError{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 isUnexported := sf.PkgPath != "" 1093 if sf.Anonymous { 1094 t := sf.Type 1095 if t.Kind() == reflect.Ptr { 1096 t = t.Elem() 1097 } 1098 if isUnexported && t.Kind() != reflect.Struct { 1099 // Ignore embedded fields of unexported non-struct types. 1100 continue 1101 } 1102 // Do not ignore embedded fields of unexported struct types 1103 // since they may have exported fields. 1104 } else if isUnexported { 1105 // Ignore unexported non-embedded fields. 1106 continue 1107 } 1108 tag := sf.Tag.Get("json") 1109 if tag == "-" { 1110 continue 1111 } 1112 name, opts := parseTag(tag) 1113 if !isValidTag(name) { 1114 name = "" 1115 } 1116 index := make([]int, len(f.index)+1) 1117 copy(index, f.index) 1118 index[len(f.index)] = i 1119 1120 ft := sf.Type 1121 if ft.Name() == "" && ft.Kind() == reflect.Ptr { 1122 // Follow pointer. 1123 ft = ft.Elem() 1124 } 1125 1126 // Only strings, floats, integers, and booleans can be quoted. 1127 quoted := false 1128 if opts.Contains("string") { 1129 switch ft.Kind() { 1130 case reflect.Bool, 1131 reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, 1132 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, 1133 reflect.Float32, reflect.Float64, 1134 reflect.String: 1135 quoted = true 1136 } 1137 } 1138 1139 // Record found field and index sequence. 1140 if name != "" || !sf.Anonymous || ft.Kind() != reflect.Struct { 1141 tagged := name != "" 1142 if name == "" { 1143 name = sf.Name 1144 } 1145 fields = append(fields, fillField(field{ 1146 name: name, 1147 tag: tagged, 1148 index: index, 1149 typ: ft, 1150 omitEmpty: opts.Contains("omitempty"), 1151 quoted: quoted, 1152 })) 1153 if count[f.typ] > 1 { 1154 // If there were multiple instances, add a second, 1155 // so that the annihilation code will see a duplicate. 1156 // It only cares about the distinction between 1 or 2, 1157 // so don't bother generating any more copies. 1158 fields = append(fields, fields[len(fields)-1]) 1159 } 1160 continue 1161 } 1162 1163 // Record new anonymous struct to explore in next round. 1164 nextCount[ft]++ 1165 if nextCount[ft] == 1 { 1166 next = append(next, fillField(field{name: ft.Name(), index: index, typ: ft})) 1167 } 1168 } 1169 } 1170 } 1171 1172 sort.Slice(fields, func(i, j int) bool { 1173 x := fields 1174 // sort field by name, breaking ties with depth, then 1175 // breaking ties with "name came from json tag", then 1176 // breaking ties with index sequence. 1177 if x[i].name != x[j].name { 1178 return x[i].name < x[j].name 1179 } 1180 if len(x[i].index) != len(x[j].index) { 1181 return len(x[i].index) < len(x[j].index) 1182 } 1183 if x[i].tag != x[j].tag { 1184 return x[i].tag 1185 } 1186 return byIndex(x).Less(i, j) 1187 }) 1188 1189 // Delete all fields that are hidden by the Go rules for embedded fields, 1190 // except that fields with JSON tags are promoted. 1191 1192 // The fields are sorted in primary order of name, secondary order 1193 // of field index length. Loop over names; for each name, delete 1194 // hidden fields by choosing the one dominant field that survives. 1195 out := fields[:0] 1196 for advance, i := 0, 0; i < len(fields); i += advance { 1197 // One iteration per name. 1198 // Find the sequence of fields with the name of this first field. 1199 fi := fields[i] 1200 name := fi.name 1201 for advance = 1; i+advance < len(fields); advance++ { 1202 fj := fields[i+advance] 1203 if fj.name != name { 1204 break 1205 } 1206 } 1207 if advance == 1 { // Only one field with this name 1208 out = append(out, fi) 1209 continue 1210 } 1211 dominant, ok := dominantField(fields[i : i+advance]) 1212 if ok { 1213 out = append(out, dominant) 1214 } 1215 } 1216 1217 fields = out 1218 sort.Sort(byIndex(fields)) 1219 1220 return fields 1221 } 1222 1223 // dominantField looks through the fields, all of which are known to 1224 // have the same name, to find the single field that dominates the 1225 // others using Go's embedding rules, modified by the presence of 1226 // JSON tags. If there are multiple top-level fields, the boolean 1227 // will be false: This condition is an error in Go and we skip all 1228 // the fields. 1229 func dominantField(fields []field) (field, bool) { 1230 // The fields are sorted in increasing index-length order. The winner 1231 // must therefore be one with the shortest index length. Drop all 1232 // longer entries, which is easy: just truncate the slice. 1233 length := len(fields[0].index) 1234 tagged := -1 // Index of first tagged field. 1235 for i, f := range fields { 1236 if len(f.index) > length { 1237 fields = fields[:i] 1238 break 1239 } 1240 if f.tag { 1241 if tagged >= 0 { 1242 // Multiple tagged fields at the same level: conflict. 1243 // Return no field. 1244 return field{}, false 1245 } 1246 tagged = i 1247 } 1248 } 1249 if tagged >= 0 { 1250 return fields[tagged], true 1251 } 1252 // All remaining fields have the same length. If there's more than one, 1253 // we have a conflict (two fields named "X" at the same level) and we 1254 // return no field. 1255 if len(fields) > 1 { 1256 return field{}, false 1257 } 1258 return fields[0], true 1259 } 1260 1261 var fieldCache struct { 1262 value atomic.Value // map[reflect.Type][]field 1263 mu sync.Mutex // used only by writers 1264 } 1265 1266 // cachedTypeFields is like typeFields but uses a cache to avoid repeated work. 1267 func cachedTypeFields(t reflect.Type) []field { 1268 m, _ := fieldCache.value.Load().(map[reflect.Type][]field) 1269 f := m[t] 1270 if f != nil { 1271 return f 1272 } 1273 1274 // Compute fields without lock. 1275 // Might duplicate effort but won't hold other computations back. 1276 f = typeFields(t) 1277 if f == nil { 1278 f = []field{} 1279 } 1280 1281 fieldCache.mu.Lock() 1282 m, _ = fieldCache.value.Load().(map[reflect.Type][]field) 1283 newM := make(map[reflect.Type][]field, len(m)+1) 1284 for k, v := range m { 1285 newM[k] = v 1286 } 1287 newM[t] = f 1288 fieldCache.value.Store(newM) 1289 fieldCache.mu.Unlock() 1290 return f 1291 }