github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/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 "unicode" 25 "unicode/utf8" 26 ) 27 28 // Marshal returns the JSON encoding of v. 29 // 30 // Marshal traverses the value v recursively. 31 // If an encountered value implements the Marshaler interface 32 // and is not a nil pointer, Marshal calls its MarshalJSON method 33 // to produce JSON. If no MarshalJSON method is present but the 34 // value implements encoding.TextMarshaler instead, Marshal calls 35 // its MarshalText method and encodes the result as a JSON string. 36 // The nil pointer exception is not strictly necessary 37 // but mimics a similar, necessary exception in the behavior of 38 // UnmarshalJSON. 39 // 40 // Otherwise, Marshal uses the following type-dependent default encodings: 41 // 42 // Boolean values encode as JSON booleans. 43 // 44 // Floating point, integer, and Number values encode as JSON numbers. 45 // 46 // String values encode as JSON strings coerced to valid UTF-8, 47 // replacing invalid bytes with the Unicode replacement rune. 48 // So that the JSON will be safe to embed inside HTML <script> tags, 49 // the string is encoded using HTMLEscape, 50 // which replaces "<", ">", "&", U+2028, and U+2029 are escaped 51 // to "\u003c","\u003e", "\u0026", "\u2028", and "\u2029". 52 // This replacement can be disabled when using an Encoder, 53 // by calling SetEscapeHTML(false). 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 // - keys of any string type 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 error. 157 // 158 func Marshal(v interface{}) ([]byte, error) { 159 e := newEncodeState() 160 161 err := e.marshal(v, encOpts{escapeHTML: false}) 162 if err != nil { 163 return nil, err 164 } 165 buf := append([]byte(nil), e.Bytes()...) 166 167 encodeStatePool.Put(e) 168 169 return buf, nil 170 } 171 172 // MarshalIndent is like Marshal but applies Indent to format the output. 173 // Each JSON element in the output will begin on a new line beginning with prefix 174 // followed by one or more copies of indent according to the indentation nesting. 175 func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) { 176 b, err := Marshal(v) 177 if err != nil { 178 return nil, err 179 } 180 var buf bytes.Buffer 181 err = Indent(&buf, b, prefix, indent) 182 if err != nil { 183 return nil, err 184 } 185 return buf.Bytes(), nil 186 } 187 188 // HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029 189 // characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029 190 // so that the JSON will be safe to embed inside HTML <script> tags. 191 // For historical reasons, web browsers don't honor standard HTML 192 // escaping within <script> tags, so an alternative JSON encoding must 193 // be used. 194 func HTMLEscape(dst *bytes.Buffer, src []byte) { 195 // The characters can only appear in string literals, 196 // so just scan the string one byte at a time. 197 start := 0 198 for i, c := range src { 199 if c == '<' || c == '>' || c == '&' { 200 if start < i { 201 dst.Write(src[start:i]) 202 } 203 dst.WriteString(`\u00`) 204 dst.WriteByte(hex[c>>4]) 205 dst.WriteByte(hex[c&0xF]) 206 start = i + 1 207 } 208 // Convert U+2028 and U+2029 (E2 80 A8 and E2 80 A9). 209 if c == 0xE2 && i+2 < len(src) && src[i+1] == 0x80 && src[i+2]&^1 == 0xA8 { 210 if start < i { 211 dst.Write(src[start:i]) 212 } 213 dst.WriteString(`\u202`) 214 dst.WriteByte(hex[src[i+2]&0xF]) 215 start = i + 3 216 } 217 } 218 if start < len(src) { 219 dst.Write(src[start:]) 220 } 221 } 222 223 // Marshaler is the interface implemented by types that 224 // can marshal themselves into valid JSON. 225 type Marshaler interface { 226 MarshalJSON() ([]byte, error) 227 } 228 229 // An UnsupportedTypeError is returned by Marshal when attempting 230 // to encode an unsupported value type. 231 type UnsupportedTypeError struct { 232 Type reflect.Type 233 } 234 235 func (e *UnsupportedTypeError) Error() string { 236 return "json: unsupported type: " + e.Type.String() 237 } 238 239 // An UnsupportedValueError is returned by Marshal when attempting 240 // to encode an unsupported value. 241 type UnsupportedValueError struct { 242 Value reflect.Value 243 Str string 244 } 245 246 func (e *UnsupportedValueError) Error() string { 247 return "json: unsupported value: " + e.Str 248 } 249 250 // Before Go 1.2, an InvalidUTF8Error was returned by Marshal when 251 // attempting to encode a string value with invalid UTF-8 sequences. 252 // As of Go 1.2, Marshal instead coerces the string to valid UTF-8 by 253 // replacing invalid bytes with the Unicode replacement rune U+FFFD. 254 // 255 // Deprecated: No longer used; kept for compatibility. 256 type InvalidUTF8Error struct { 257 S string // the whole string value that caused the error 258 } 259 260 func (e *InvalidUTF8Error) Error() string { 261 return "json: invalid UTF-8 in string: " + strconv.Quote(e.S) 262 } 263 264 // A MarshalerError represents an error from calling a MarshalJSON or MarshalText method. 265 type MarshalerError struct { 266 Type reflect.Type 267 Err error 268 sourceFunc string 269 } 270 271 func (e *MarshalerError) Error() string { 272 srcFunc := e.sourceFunc 273 if srcFunc == "" { 274 srcFunc = "MarshalJSON" 275 } 276 return "json: error calling " + srcFunc + 277 " for type " + e.Type.String() + 278 ": " + e.Err.Error() 279 } 280 281 // Unwrap returns the underlying error. 282 func (e *MarshalerError) Unwrap() error { return e.Err } 283 284 var hex = "0123456789abcdef" 285 286 // An encodeState encodes JSON into a bytes.Buffer. 287 type encodeState struct { 288 bytes.Buffer // accumulated output 289 scratch [64]byte 290 291 // Keep track of what pointers we've seen in the current recursive call 292 // path, to avoid cycles that could lead to a stack overflow. Only do 293 // the relatively expensive map operations if ptrLevel is larger than 294 // startDetectingCyclesAfter, so that we skip the work if we're within a 295 // reasonable amount of nested pointers deep. 296 ptrLevel uint 297 ptrSeen map[interface{}]struct{} 298 } 299 300 const startDetectingCyclesAfter = 1000 301 302 var encodeStatePool sync.Pool 303 304 func newEncodeState() *encodeState { 305 if v := encodeStatePool.Get(); v != nil { 306 e := v.(*encodeState) 307 e.Reset() 308 if len(e.ptrSeen) > 0 { 309 panic("ptrEncoder.encode should have emptied ptrSeen via defers") 310 } 311 e.ptrLevel = 0 312 return e 313 } 314 return &encodeState{ptrSeen: make(map[interface{}]struct{})} 315 } 316 317 // jsonError is an error wrapper type for internal use only. 318 // Panics with errors are wrapped in jsonError so that the top-level recover 319 // can distinguish intentional panics from this package. 320 type jsonError struct{ error } 321 322 func (e *encodeState) marshal(v interface{}, opts encOpts) (err error) { 323 defer func() { 324 if r := recover(); r != nil { 325 if je, ok := r.(jsonError); ok { 326 err = je.error 327 } else { 328 panic(r) 329 } 330 } 331 }() 332 e.reflectValue(reflect.ValueOf(v), opts) 333 return nil 334 } 335 336 // error aborts the encoding by panicking with err wrapped in jsonError. 337 func (e *encodeState) error(err error) { 338 panic(jsonError{err}) 339 } 340 341 func isEmptyValue(v reflect.Value) bool { 342 switch v.Kind() { 343 case reflect.Array, reflect.Map, reflect.Slice, reflect.String: 344 return v.Len() == 0 345 case reflect.Bool: 346 return !v.Bool() 347 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 348 return v.Int() == 0 349 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 350 return v.Uint() == 0 351 case reflect.Float32, reflect.Float64: 352 return v.Float() == 0 353 case reflect.Interface, reflect.Ptr: 354 return v.IsNil() 355 } 356 return false 357 } 358 359 func (e *encodeState) reflectValue(v reflect.Value, opts encOpts) { 360 valueEncoder(v)(e, v, opts) 361 } 362 363 type encOpts struct { 364 // quoted causes primitive fields to be encoded inside JSON strings. 365 quoted bool 366 // escapeHTML causes '<', '>', and '&' to be escaped in JSON strings. 367 escapeHTML bool 368 } 369 370 type encoderFunc func(e *encodeState, v reflect.Value, opts encOpts) 371 372 var encoderCache sync.Map // map[reflect.Type]encoderFunc 373 374 func valueEncoder(v reflect.Value) encoderFunc { 375 if !v.IsValid() { 376 return invalidValueEncoder 377 } 378 return typeEncoder(v.Type()) 379 } 380 381 func typeEncoder(t reflect.Type) encoderFunc { 382 if fi, ok := encoderCache.Load(t); ok { 383 return fi.(encoderFunc) 384 } 385 386 // To deal with recursive types, populate the map with an 387 // indirect func before we build it. This type waits on the 388 // real func (f) to be ready and then calls it. This indirect 389 // func is only used for recursive types. 390 var ( 391 wg sync.WaitGroup 392 f encoderFunc 393 ) 394 wg.Add(1) 395 fi, loaded := encoderCache.LoadOrStore(t, encoderFunc(func(e *encodeState, v reflect.Value, opts encOpts) { 396 wg.Wait() 397 f(e, v, opts) 398 })) 399 if loaded { 400 return fi.(encoderFunc) 401 } 402 403 // Compute the real encoder and replace the indirect func with it. 404 f = newTypeEncoder(t, true) 405 wg.Done() 406 encoderCache.Store(t, f) 407 return f 408 } 409 410 var ( 411 marshalerType = reflect.TypeOf((*Marshaler)(nil)).Elem() 412 textMarshalerType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem() 413 ) 414 415 // newTypeEncoder constructs an encoderFunc for a type. 416 // The returned encoder only checks CanAddr when allowAddr is true. 417 func newTypeEncoder(t reflect.Type, allowAddr bool) encoderFunc { 418 // If we have a non-pointer value whose type implements 419 // Marshaler with a value receiver, then we're better off taking 420 // the address of the value - otherwise we end up with an 421 // allocation as we cast the value to an interface. 422 if t.Kind() != reflect.Ptr && allowAddr && reflect.PtrTo(t).Implements(marshalerType) { 423 return newCondAddrEncoder(addrMarshalerEncoder, newTypeEncoder(t, false)) 424 } 425 if t.Implements(marshalerType) { 426 return marshalerEncoder 427 } 428 if t.Kind() != reflect.Ptr && allowAddr && reflect.PtrTo(t).Implements(textMarshalerType) { 429 return newCondAddrEncoder(addrTextMarshalerEncoder, newTypeEncoder(t, false)) 430 } 431 if t.Implements(textMarshalerType) { 432 return textMarshalerEncoder 433 } 434 435 switch t.Kind() { 436 case reflect.Bool: 437 return boolEncoder 438 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 439 return intEncoder 440 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 441 return uintEncoder 442 case reflect.Float32: 443 return float32Encoder 444 case reflect.Float64: 445 return float64Encoder 446 case reflect.String: 447 return stringEncoder 448 case reflect.Interface: 449 return interfaceEncoder 450 case reflect.Struct: 451 return newStructEncoder(t) 452 case reflect.Map: 453 return newMapEncoder(t) 454 case reflect.Slice: 455 return newSliceEncoder(t) 456 case reflect.Array: 457 return newArrayEncoder(t) 458 case reflect.Ptr: 459 return newPtrEncoder(t) 460 default: 461 return unsupportedTypeEncoder 462 } 463 } 464 465 func invalidValueEncoder(e *encodeState, v reflect.Value, _ encOpts) { 466 e.WriteString("null") 467 } 468 469 func marshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) { 470 if v.Kind() == reflect.Ptr && v.IsNil() { 471 e.WriteString("null") 472 return 473 } 474 m, ok := v.Interface().(Marshaler) 475 if !ok { 476 e.WriteString("null") 477 return 478 } 479 b, err := m.MarshalJSON() 480 if err == nil { 481 // copy JSON into buffer, checking validity. 482 err = compact(&e.Buffer, b, opts.escapeHTML) 483 } 484 if err != nil { 485 e.error(&MarshalerError{v.Type(), err, "MarshalJSON"}) 486 } 487 } 488 489 func addrMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) { 490 va := v.Addr() 491 if va.IsNil() { 492 e.WriteString("null") 493 return 494 } 495 m := va.Interface().(Marshaler) 496 b, err := m.MarshalJSON() 497 if err == nil { 498 // copy JSON into buffer, checking validity. 499 err = compact(&e.Buffer, b, opts.escapeHTML) 500 } 501 if err != nil { 502 e.error(&MarshalerError{v.Type(), err, "MarshalJSON"}) 503 } 504 } 505 506 func textMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) { 507 if v.Kind() == reflect.Ptr && v.IsNil() { 508 e.WriteString("null") 509 return 510 } 511 m, ok := v.Interface().(encoding.TextMarshaler) 512 if !ok { 513 e.WriteString("null") 514 return 515 } 516 b, err := m.MarshalText() 517 if err != nil { 518 e.error(&MarshalerError{v.Type(), err, "MarshalText"}) 519 } 520 e.stringBytes(b, opts.escapeHTML) 521 } 522 523 func addrTextMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) { 524 va := v.Addr() 525 if va.IsNil() { 526 e.WriteString("null") 527 return 528 } 529 m := va.Interface().(encoding.TextMarshaler) 530 b, err := m.MarshalText() 531 if err != nil { 532 e.error(&MarshalerError{v.Type(), err, "MarshalText"}) 533 } 534 e.stringBytes(b, opts.escapeHTML) 535 } 536 537 func boolEncoder(e *encodeState, v reflect.Value, opts encOpts) { 538 if opts.quoted { 539 e.WriteByte('"') 540 } 541 if v.Bool() { 542 e.WriteString("true") 543 } else { 544 e.WriteString("false") 545 } 546 if opts.quoted { 547 e.WriteByte('"') 548 } 549 } 550 551 func intEncoder(e *encodeState, v reflect.Value, opts encOpts) { 552 b := strconv.AppendInt(e.scratch[:0], v.Int(), 10) 553 if opts.quoted { 554 e.WriteByte('"') 555 } 556 e.Write(b) 557 if opts.quoted { 558 e.WriteByte('"') 559 } 560 } 561 562 func uintEncoder(e *encodeState, v reflect.Value, opts encOpts) { 563 b := strconv.AppendUint(e.scratch[:0], v.Uint(), 10) 564 if opts.quoted { 565 e.WriteByte('"') 566 } 567 e.Write(b) 568 if opts.quoted { 569 e.WriteByte('"') 570 } 571 } 572 573 type floatEncoder int // number of bits 574 575 func (bits floatEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { 576 f := v.Float() 577 if math.IsInf(f, 0) || math.IsNaN(f) { 578 e.error(&UnsupportedValueError{v, strconv.FormatFloat(f, 'g', -1, int(bits))}) 579 } 580 581 // Convert as if by ES6 number to string conversion. 582 // This matches most other JSON generators. 583 // See golang.org/issue/6384 and golang.org/issue/14135. 584 // Like fmt %g, but the exponent cutoffs are different 585 // and exponents themselves are not padded to two digits. 586 b := e.scratch[:0] 587 abs := math.Abs(f) 588 fmt := byte('f') 589 // Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right. 590 if abs != 0 { 591 if bits == 64 && (abs < 1e-6 || abs >= 1e21) || bits == 32 && (float32(abs) < 1e-6 || float32(abs) >= 1e21) { 592 fmt = 'e' 593 } 594 } 595 b = strconv.AppendFloat(b, f, fmt, -1, int(bits)) 596 if fmt == 'e' { 597 // clean up e-09 to e-9 598 n := len(b) 599 if n >= 4 && b[n-4] == 'e' && b[n-3] == '-' && b[n-2] == '0' { 600 b[n-2] = b[n-1] 601 b = b[:n-1] 602 } 603 } 604 605 if opts.quoted { 606 e.WriteByte('"') 607 } 608 e.Write(b) 609 if opts.quoted { 610 e.WriteByte('"') 611 } 612 } 613 614 var ( 615 float32Encoder = (floatEncoder(32)).encode 616 float64Encoder = (floatEncoder(64)).encode 617 ) 618 619 func stringEncoder(e *encodeState, v reflect.Value, opts encOpts) { 620 if v.Type() == numberType { 621 numStr := v.String() 622 // In Go1.5 the empty string encodes to "0", while this is not a valid number literal 623 // we keep compatibility so check validity after this. 624 if numStr == "" { 625 numStr = "0" // Number's zero-val 626 } 627 if !isValidNumber(numStr) { 628 e.error(fmt.Errorf("json: invalid number literal %q", numStr)) 629 } 630 if opts.quoted { 631 e.WriteByte('"') 632 } 633 e.WriteString(numStr) 634 if opts.quoted { 635 e.WriteByte('"') 636 } 637 return 638 } 639 if opts.quoted { 640 e2 := newEncodeState() 641 // Since we encode the string twice, we only need to escape HTML 642 // the first time. 643 e2.string(v.String(), opts.escapeHTML) 644 e.stringBytes(e2.Bytes(), false) 645 encodeStatePool.Put(e2) 646 } else { 647 e.string(v.String(), opts.escapeHTML) 648 } 649 } 650 651 // isValidNumber reports whether s is a valid JSON number literal. 652 func isValidNumber(s string) bool { 653 // This function implements the JSON numbers grammar. 654 // See https://tools.ietf.org/html/rfc7159#section-6 655 // and https://www.json.org/img/number.png 656 657 if s == "" { 658 return false 659 } 660 661 // Optional - 662 if s[0] == '-' { 663 s = s[1:] 664 if s == "" { 665 return false 666 } 667 } 668 669 // Digits or dot 670 switch { 671 default: 672 return false 673 674 case '0' <= s[0] && s[0] <= '9': 675 s = s[1:] 676 for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { 677 s = s[1:] 678 } 679 680 case s[0] == '.': 681 if len(s) < 2 || s[1] < '0' || '9' < s[1] { 682 return false 683 } 684 } 685 686 // . followed by 1 or more digits. 687 if len(s) >= 2 && s[0] == '.' && '0' <= s[1] && s[1] <= '9' { 688 s = s[2:] 689 for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { 690 s = s[1:] 691 } 692 } 693 694 // e or E followed by an optional - or + and 695 // 1 or more digits. 696 if len(s) >= 2 && (s[0] == 'e' || s[0] == 'E') { 697 s = s[1:] 698 if s[0] == '+' || s[0] == '-' { 699 s = s[1:] 700 if s == "" { 701 return false 702 } 703 } 704 for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { 705 s = s[1:] 706 } 707 } 708 709 // Make sure we are at the end. 710 return s == "" 711 } 712 713 func interfaceEncoder(e *encodeState, v reflect.Value, opts encOpts) { 714 if v.IsNil() { 715 e.WriteString("null") 716 return 717 } 718 e.reflectValue(v.Elem(), opts) 719 } 720 721 func unsupportedTypeEncoder(e *encodeState, v reflect.Value, _ encOpts) { 722 e.error(&UnsupportedTypeError{v.Type()}) 723 } 724 725 type structEncoder struct { 726 fields structFields 727 } 728 729 type structFields struct { 730 list []field 731 nameIndex map[string]int 732 } 733 734 func (se structEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { 735 next := byte('{') 736 FieldLoop: 737 for i := range se.fields.list { 738 f := &se.fields.list[i] 739 740 // Find the nested struct field by following f.index. 741 fv := v 742 for _, i := range f.index { 743 if fv.Kind() == reflect.Ptr { 744 if fv.IsNil() { 745 continue FieldLoop 746 } 747 fv = fv.Elem() 748 } 749 fv = fv.Field(i) 750 } 751 752 if f.omitEmpty && isEmptyValue(fv) { 753 continue 754 } 755 e.WriteByte(next) 756 next = ',' 757 if opts.escapeHTML { 758 e.WriteString(f.nameEscHTML) 759 } else { 760 e.WriteString(f.nameNonEsc) 761 } 762 opts.quoted = f.quoted 763 f.encoder(e, fv, opts) 764 } 765 if next == '{' { 766 e.WriteString("{}") 767 } else { 768 e.WriteByte('}') 769 } 770 } 771 772 func newStructEncoder(t reflect.Type) encoderFunc { 773 se := structEncoder{fields: cachedTypeFields(t)} 774 return se.encode 775 } 776 777 type mapEncoder struct { 778 elemEnc encoderFunc 779 } 780 781 func (me mapEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { 782 if v.IsNil() { 783 e.WriteString("null") 784 return 785 } 786 if e.ptrLevel++; e.ptrLevel > startDetectingCyclesAfter { 787 // We're a large number of nested ptrEncoder.encode calls deep; 788 // start checking if we've run into a pointer cycle. 789 ptr := v.Pointer() 790 if _, ok := e.ptrSeen[ptr]; ok { 791 e.error(&UnsupportedValueError{v, fmt.Sprintf("encountered a cycle via %s", v.Type())}) 792 } 793 e.ptrSeen[ptr] = struct{}{} 794 defer delete(e.ptrSeen, ptr) 795 } 796 e.WriteByte('{') 797 798 // Extract and sort the keys. 799 keys := v.MapKeys() 800 sv := make([]reflectWithString, len(keys)) 801 for i, v := range keys { 802 sv[i].v = v 803 if err := sv[i].resolve(); err != nil { 804 e.error(fmt.Errorf("json: encoding error for type %q: %q", v.Type().String(), err.Error())) 805 } 806 } 807 sort.Slice(sv, func(i, j int) bool { return sv[i].s < sv[j].s }) 808 809 for i, kv := range sv { 810 if i > 0 { 811 e.WriteByte(',') 812 } 813 e.string(kv.s, opts.escapeHTML) 814 e.WriteByte(':') 815 me.elemEnc(e, v.MapIndex(kv.v), opts) 816 } 817 e.WriteByte('}') 818 e.ptrLevel-- 819 } 820 821 func newMapEncoder(t reflect.Type) encoderFunc { 822 switch t.Key().Kind() { 823 case reflect.String, 824 reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, 825 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 826 default: 827 if !t.Key().Implements(textMarshalerType) { 828 return unsupportedTypeEncoder 829 } 830 } 831 me := mapEncoder{typeEncoder(t.Elem())} 832 return me.encode 833 } 834 835 func encodeByteSlice(e *encodeState, v reflect.Value, _ encOpts) { 836 if v.IsNil() { 837 e.WriteString("null") 838 return 839 } 840 s := v.Bytes() 841 e.WriteByte('"') 842 encodedLen := base64.StdEncoding.EncodedLen(len(s)) 843 if encodedLen <= len(e.scratch) { 844 // If the encoded bytes fit in e.scratch, avoid an extra 845 // allocation and use the cheaper Encoding.Encode. 846 dst := e.scratch[:encodedLen] 847 base64.StdEncoding.Encode(dst, s) 848 e.Write(dst) 849 } else if encodedLen <= 1024 { 850 // The encoded bytes are short enough to allocate for, and 851 // Encoding.Encode is still cheaper. 852 dst := make([]byte, encodedLen) 853 base64.StdEncoding.Encode(dst, s) 854 e.Write(dst) 855 } else { 856 // The encoded bytes are too long to cheaply allocate, and 857 // Encoding.Encode is no longer noticeably cheaper. 858 enc := base64.NewEncoder(base64.StdEncoding, e) 859 enc.Write(s) 860 enc.Close() 861 } 862 e.WriteByte('"') 863 } 864 865 // sliceEncoder just wraps an arrayEncoder, checking to make sure the value isn't nil. 866 type sliceEncoder struct { 867 arrayEnc encoderFunc 868 } 869 870 func (se sliceEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { 871 if v.IsNil() { 872 e.WriteString("null") 873 return 874 } 875 if e.ptrLevel++; e.ptrLevel > startDetectingCyclesAfter { 876 // We're a large number of nested ptrEncoder.encode calls deep; 877 // start checking if we've run into a pointer cycle. 878 // Here we use a struct to memorize the pointer to the first element of the slice 879 // and its length. 880 ptr := struct { 881 ptr uintptr 882 len int 883 }{v.Pointer(), v.Len()} 884 if _, ok := e.ptrSeen[ptr]; ok { 885 e.error(&UnsupportedValueError{v, fmt.Sprintf("encountered a cycle via %s", v.Type())}) 886 } 887 e.ptrSeen[ptr] = struct{}{} 888 defer delete(e.ptrSeen, ptr) 889 } 890 se.arrayEnc(e, v, opts) 891 e.ptrLevel-- 892 } 893 894 func newSliceEncoder(t reflect.Type) encoderFunc { 895 // Byte slices get special treatment; arrays don't. 896 if t.Elem().Kind() == reflect.Uint8 { 897 p := reflect.PtrTo(t.Elem()) 898 if !p.Implements(marshalerType) && !p.Implements(textMarshalerType) { 899 return encodeByteSlice 900 } 901 } 902 enc := sliceEncoder{newArrayEncoder(t)} 903 return enc.encode 904 } 905 906 type arrayEncoder struct { 907 elemEnc encoderFunc 908 } 909 910 func (ae arrayEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { 911 e.WriteByte('[') 912 n := v.Len() 913 for i := 0; i < n; i++ { 914 if i > 0 { 915 e.WriteByte(',') 916 } 917 ae.elemEnc(e, v.Index(i), opts) 918 } 919 e.WriteByte(']') 920 } 921 922 func newArrayEncoder(t reflect.Type) encoderFunc { 923 enc := arrayEncoder{typeEncoder(t.Elem())} 924 return enc.encode 925 } 926 927 type ptrEncoder struct { 928 elemEnc encoderFunc 929 } 930 931 func (pe ptrEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { 932 if v.IsNil() { 933 e.WriteString("null") 934 return 935 } 936 if e.ptrLevel++; e.ptrLevel > startDetectingCyclesAfter { 937 // We're a large number of nested ptrEncoder.encode calls deep; 938 // start checking if we've run into a pointer cycle. 939 ptr := v.Interface() 940 if _, ok := e.ptrSeen[ptr]; ok { 941 e.error(&UnsupportedValueError{v, fmt.Sprintf("encountered a cycle via %s", v.Type())}) 942 } 943 e.ptrSeen[ptr] = struct{}{} 944 defer delete(e.ptrSeen, ptr) 945 } 946 pe.elemEnc(e, v.Elem(), opts) 947 e.ptrLevel-- 948 } 949 950 func newPtrEncoder(t reflect.Type) encoderFunc { 951 enc := ptrEncoder{typeEncoder(t.Elem())} 952 return enc.encode 953 } 954 955 type condAddrEncoder struct { 956 canAddrEnc, elseEnc encoderFunc 957 } 958 959 func (ce condAddrEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { 960 if v.CanAddr() { 961 ce.canAddrEnc(e, v, opts) 962 } else { 963 ce.elseEnc(e, v, opts) 964 } 965 } 966 967 // newCondAddrEncoder returns an encoder that checks whether its value 968 // CanAddr and delegates to canAddrEnc if so, else to elseEnc. 969 func newCondAddrEncoder(canAddrEnc, elseEnc encoderFunc) encoderFunc { 970 enc := condAddrEncoder{canAddrEnc: canAddrEnc, elseEnc: elseEnc} 971 return enc.encode 972 } 973 974 func isValidTag(s string) bool { 975 if s == "" { 976 return false 977 } 978 for _, c := range s { 979 switch { 980 case strings.ContainsRune("!#$%&()*+-./:;<=>?@[]^_{|}~ ", c): 981 // Backslash and quote chars are reserved, but 982 // otherwise any punctuation chars are allowed 983 // in a tag name. 984 case !unicode.IsLetter(c) && !unicode.IsDigit(c): 985 return false 986 } 987 } 988 return true 989 } 990 991 func typeByIndex(t reflect.Type, index []int) reflect.Type { 992 for _, i := range index { 993 if t.Kind() == reflect.Ptr { 994 t = t.Elem() 995 } 996 t = t.Field(i).Type 997 } 998 return t 999 } 1000 1001 type reflectWithString struct { 1002 v reflect.Value 1003 s string 1004 } 1005 1006 func (w *reflectWithString) resolve() error { 1007 if w.v.Kind() == reflect.String { 1008 w.s = w.v.String() 1009 return nil 1010 } 1011 if tm, ok := w.v.Interface().(encoding.TextMarshaler); ok { 1012 if w.v.Kind() == reflect.Ptr && w.v.IsNil() { 1013 return nil 1014 } 1015 buf, err := tm.MarshalText() 1016 w.s = string(buf) 1017 return err 1018 } 1019 switch w.v.Kind() { 1020 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 1021 w.s = strconv.FormatInt(w.v.Int(), 10) 1022 return nil 1023 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 1024 w.s = strconv.FormatUint(w.v.Uint(), 10) 1025 return nil 1026 } 1027 panic("unexpected map key type") 1028 } 1029 1030 // NOTE: keep in sync with stringBytes below. 1031 func (e *encodeState) string(s string, escapeHTML bool) { 1032 e.WriteByte('"') 1033 start := 0 1034 for i := 0; i < len(s); { 1035 if b := s[i]; b < utf8.RuneSelf { 1036 if htmlSafeSet[b] || (!escapeHTML && safeSet[b]) { 1037 i++ 1038 continue 1039 } 1040 if start < i { 1041 e.WriteString(s[start:i]) 1042 } 1043 e.WriteByte('\\') 1044 switch b { 1045 case '\\', '"': 1046 e.WriteByte(b) 1047 case '\n': 1048 e.WriteByte('n') 1049 case '\r': 1050 e.WriteByte('r') 1051 case '\t': 1052 e.WriteByte('t') 1053 default: 1054 // This encodes bytes < 0x20 except for \t, \n and \r. 1055 // If escapeHTML is set, it also escapes <, >, and & 1056 // because they can lead to security holes when 1057 // user-controlled strings are rendered into JSON 1058 // and served to some browsers. 1059 e.WriteString(`u00`) 1060 e.WriteByte(hex[b>>4]) 1061 e.WriteByte(hex[b&0xF]) 1062 } 1063 i++ 1064 start = i 1065 continue 1066 } 1067 c, size := utf8.DecodeRuneInString(s[i:]) 1068 if c == utf8.RuneError && size == 1 { 1069 if start < i { 1070 e.WriteString(s[start:i]) 1071 } 1072 e.WriteString(`\ufffd`) 1073 i += size 1074 start = i 1075 continue 1076 } 1077 // U+2028 is LINE SEPARATOR. 1078 // U+2029 is PARAGRAPH SEPARATOR. 1079 // They are both technically valid characters in JSON strings, 1080 // but don't work in JSONP, which has to be evaluated as JavaScript, 1081 // and can lead to security holes there. It is valid JSON to 1082 // escape them, so we do so unconditionally. 1083 // See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion. 1084 if c == '\u2028' || c == '\u2029' { 1085 if start < i { 1086 e.WriteString(s[start:i]) 1087 } 1088 e.WriteString(`\u202`) 1089 e.WriteByte(hex[c&0xF]) 1090 i += size 1091 start = i 1092 continue 1093 } 1094 i += size 1095 } 1096 if start < len(s) { 1097 e.WriteString(s[start:]) 1098 } 1099 e.WriteByte('"') 1100 } 1101 1102 // NOTE: keep in sync with string above. 1103 func (e *encodeState) stringBytes(s []byte, escapeHTML bool) { 1104 e.WriteByte('"') 1105 start := 0 1106 for i := 0; i < len(s); { 1107 if b := s[i]; b < utf8.RuneSelf { 1108 if htmlSafeSet[b] || (!escapeHTML && safeSet[b]) { 1109 i++ 1110 continue 1111 } 1112 if start < i { 1113 e.Write(s[start:i]) 1114 } 1115 e.WriteByte('\\') 1116 switch b { 1117 case '\\', '"': 1118 e.WriteByte(b) 1119 case '\n': 1120 e.WriteByte('n') 1121 case '\r': 1122 e.WriteByte('r') 1123 case '\t': 1124 e.WriteByte('t') 1125 default: 1126 // This encodes bytes < 0x20 except for \t, \n and \r. 1127 // If escapeHTML is set, it also escapes <, >, and & 1128 // because they can lead to security holes when 1129 // user-controlled strings are rendered into JSON 1130 // and served to some browsers. 1131 e.WriteString(`u00`) 1132 e.WriteByte(hex[b>>4]) 1133 e.WriteByte(hex[b&0xF]) 1134 } 1135 i++ 1136 start = i 1137 continue 1138 } 1139 c, size := utf8.DecodeRune(s[i:]) 1140 if c == utf8.RuneError && size == 1 { 1141 if start < i { 1142 e.Write(s[start:i]) 1143 } 1144 e.WriteString(`\ufffd`) 1145 i += size 1146 start = i 1147 continue 1148 } 1149 // U+2028 is LINE SEPARATOR. 1150 // U+2029 is PARAGRAPH SEPARATOR. 1151 // They are both technically valid characters in JSON strings, 1152 // but don't work in JSONP, which has to be evaluated as JavaScript, 1153 // and can lead to security holes there. It is valid JSON to 1154 // escape them, so we do so unconditionally. 1155 // See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion. 1156 if c == '\u2028' || c == '\u2029' { 1157 if start < i { 1158 e.Write(s[start:i]) 1159 } 1160 e.WriteString(`\u202`) 1161 e.WriteByte(hex[c&0xF]) 1162 i += size 1163 start = i 1164 continue 1165 } 1166 i += size 1167 } 1168 if start < len(s) { 1169 e.Write(s[start:]) 1170 } 1171 e.WriteByte('"') 1172 } 1173 1174 // A field represents a single field found in a struct. 1175 type field struct { 1176 name string 1177 nameBytes []byte // []byte(name) 1178 equalFold func(s, t []byte) bool // bytes.EqualFold or equivalent 1179 1180 nameNonEsc string // `"` + name + `":` 1181 nameEscHTML string // `"` + HTMLEscape(name) + `":` 1182 1183 tag bool 1184 index []int 1185 typ reflect.Type 1186 omitEmpty bool 1187 quoted bool 1188 1189 encoder encoderFunc 1190 } 1191 1192 // byIndex sorts field by index sequence. 1193 type byIndex []field 1194 1195 func (x byIndex) Len() int { return len(x) } 1196 1197 func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 1198 1199 func (x byIndex) Less(i, j int) bool { 1200 for k, xik := range x[i].index { 1201 if k >= len(x[j].index) { 1202 return false 1203 } 1204 if xik != x[j].index[k] { 1205 return xik < x[j].index[k] 1206 } 1207 } 1208 return len(x[i].index) < len(x[j].index) 1209 } 1210 1211 // typeFields returns a list of fields that JSON should recognize for the given type. 1212 // The algorithm is breadth-first search over the set of structs to include - the top struct 1213 // and then any reachable anonymous structs. 1214 func typeFields(t reflect.Type) structFields { 1215 // Anonymous fields to explore at the current level and the next. 1216 current := []field{} 1217 next := []field{{typ: t}} 1218 1219 // Count of queued names for current level and the next. 1220 var count, nextCount map[reflect.Type]int 1221 1222 // Types already visited at an earlier level. 1223 visited := map[reflect.Type]bool{} 1224 1225 // Fields found. 1226 var fields []field 1227 1228 // Buffer to run HTMLEscape on field names. 1229 var nameEscBuf bytes.Buffer 1230 1231 for len(next) > 0 { 1232 current, next = next, current[:0] 1233 count, nextCount = nextCount, map[reflect.Type]int{} 1234 1235 for _, f := range current { 1236 if visited[f.typ] { 1237 continue 1238 } 1239 visited[f.typ] = true 1240 1241 // Scan f.typ for fields to include. 1242 for i := 0; i < f.typ.NumField(); i++ { 1243 sf := f.typ.Field(i) 1244 isUnexported := sf.PkgPath != "" 1245 if sf.Anonymous { 1246 t := sf.Type 1247 if t.Kind() == reflect.Ptr { 1248 t = t.Elem() 1249 } 1250 if isUnexported && t.Kind() != reflect.Struct { 1251 // Ignore embedded fields of unexported non-struct types. 1252 continue 1253 } 1254 // Do not ignore embedded fields of unexported struct types 1255 // since they may have exported fields. 1256 } else if isUnexported { 1257 // Ignore unexported non-embedded fields. 1258 continue 1259 } 1260 tag := sf.Tag.Get("json") 1261 if tag == "-" { 1262 continue 1263 } 1264 name, opts := parseTag(tag) 1265 if !isValidTag(name) { 1266 name = "" 1267 } 1268 index := make([]int, len(f.index)+1) 1269 copy(index, f.index) 1270 index[len(f.index)] = i 1271 1272 ft := sf.Type 1273 if ft.Name() == "" && ft.Kind() == reflect.Ptr { 1274 // Follow pointer. 1275 ft = ft.Elem() 1276 } 1277 1278 // Only strings, floats, integers, and booleans can be quoted. 1279 quoted := false 1280 if opts.Contains("string") { 1281 switch ft.Kind() { 1282 case reflect.Bool, 1283 reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, 1284 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, 1285 reflect.Float32, reflect.Float64, 1286 reflect.String: 1287 quoted = true 1288 } 1289 } 1290 1291 // Record found field and index sequence. 1292 if name != "" || !sf.Anonymous || ft.Kind() != reflect.Struct { 1293 tagged := name != "" 1294 if name == "" { 1295 name = sf.Name 1296 } 1297 field := field{ 1298 name: name, 1299 tag: tagged, 1300 index: index, 1301 typ: ft, 1302 omitEmpty: opts.Contains("omitempty"), 1303 quoted: quoted, 1304 } 1305 field.nameBytes = []byte(field.name) 1306 field.equalFold = foldFunc(field.nameBytes) 1307 1308 // Build nameEscHTML and nameNonEsc ahead of time. 1309 nameEscBuf.Reset() 1310 nameEscBuf.WriteString(`"`) 1311 HTMLEscape(&nameEscBuf, field.nameBytes) 1312 nameEscBuf.WriteString(`":`) 1313 field.nameEscHTML = nameEscBuf.String() 1314 field.nameNonEsc = `"` + field.name + `":` 1315 1316 fields = append(fields, field) 1317 if count[f.typ] > 1 { 1318 // If there were multiple instances, add a second, 1319 // so that the annihilation code will see a duplicate. 1320 // It only cares about the distinction between 1 or 2, 1321 // so don't bother generating any more copies. 1322 fields = append(fields, fields[len(fields)-1]) 1323 } 1324 continue 1325 } 1326 1327 // Record new anonymous struct to explore in next round. 1328 nextCount[ft]++ 1329 if nextCount[ft] == 1 { 1330 next = append(next, field{name: ft.Name(), index: index, typ: ft}) 1331 } 1332 } 1333 } 1334 } 1335 1336 sort.Slice(fields, func(i, j int) bool { 1337 x := fields 1338 // sort field by name, breaking ties with depth, then 1339 // breaking ties with "name came from json tag", then 1340 // breaking ties with index sequence. 1341 if x[i].name != x[j].name { 1342 return x[i].name < x[j].name 1343 } 1344 if len(x[i].index) != len(x[j].index) { 1345 return len(x[i].index) < len(x[j].index) 1346 } 1347 if x[i].tag != x[j].tag { 1348 return x[i].tag 1349 } 1350 return byIndex(x).Less(i, j) 1351 }) 1352 1353 // Delete all fields that are hidden by the Go rules for embedded fields, 1354 // except that fields with JSON tags are promoted. 1355 1356 // The fields are sorted in primary order of name, secondary order 1357 // of field index length. Loop over names; for each name, delete 1358 // hidden fields by choosing the one dominant field that survives. 1359 out := fields[:0] 1360 for advance, i := 0, 0; i < len(fields); i += advance { 1361 // One iteration per name. 1362 // Find the sequence of fields with the name of this first field. 1363 fi := fields[i] 1364 name := fi.name 1365 for advance = 1; i+advance < len(fields); advance++ { 1366 fj := fields[i+advance] 1367 if fj.name != name { 1368 break 1369 } 1370 } 1371 if advance == 1 { // Only one field with this name 1372 out = append(out, fi) 1373 continue 1374 } 1375 dominant, ok := dominantField(fields[i : i+advance]) 1376 if ok { 1377 out = append(out, dominant) 1378 } 1379 } 1380 1381 fields = out 1382 sort.Sort(byIndex(fields)) 1383 1384 for i := range fields { 1385 f := &fields[i] 1386 f.encoder = typeEncoder(typeByIndex(t, f.index)) 1387 } 1388 nameIndex := make(map[string]int, len(fields)) 1389 for i, field := range fields { 1390 nameIndex[field.name] = i 1391 } 1392 return structFields{fields, nameIndex} 1393 } 1394 1395 // dominantField looks through the fields, all of which are known to 1396 // have the same name, to find the single field that dominates the 1397 // others using Go's embedding rules, modified by the presence of 1398 // JSON tags. If there are multiple top-level fields, the boolean 1399 // will be false: This condition is an error in Go and we skip all 1400 // the fields. 1401 func dominantField(fields []field) (field, bool) { 1402 // The fields are sorted in increasing index-length order, then by presence of tag. 1403 // That means that the first field is the dominant one. We need only check 1404 // for error cases: two fields at top level, either both tagged or neither tagged. 1405 if len(fields) > 1 && len(fields[0].index) == len(fields[1].index) && fields[0].tag == fields[1].tag { 1406 return field{}, false 1407 } 1408 return fields[0], true 1409 } 1410 1411 var fieldCache sync.Map // map[reflect.Type]structFields 1412 1413 // cachedTypeFields is like typeFields but uses a cache to avoid repeated work. 1414 func cachedTypeFields(t reflect.Type) structFields { 1415 if f, ok := fieldCache.Load(t); ok { 1416 return f.(structFields) 1417 } 1418 f, _ := fieldCache.LoadOrStore(t, typeFields(t)) 1419 return f.(structFields) 1420 }