github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/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 objects as defined in 6 // RFC 4627. The mapping between JSON objects 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 "math" 18 "reflect" 19 "runtime" 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. 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 // The angle brackets "<" and ">" are escaped to "\u003c" and "\u003e" 49 // to keep some browsers from misinterpreting JSON output as HTML. 50 // Ampersand "&" is also escaped to "\u0026" for the same reason. 51 // 52 // Array and slice values encode as JSON arrays, except that 53 // []byte encodes as a base64-encoded string, and a nil slice 54 // encodes as the null JSON object. 55 // 56 // Struct values encode as JSON objects. Each exported struct field 57 // becomes a member of the object unless 58 // - the field's tag is "-", or 59 // - the field is empty and its tag specifies the "omitempty" option. 60 // The empty values are false, 0, any 61 // nil pointer or interface value, and any array, slice, map, or string of 62 // length zero. The object's default key string is the struct field name 63 // but can be specified in the struct field's tag value. The "json" key in 64 // the struct field's tag value is the key name, followed by an optional comma 65 // and options. Examples: 66 // 67 // // Field is ignored by this package. 68 // Field int `json:"-"` 69 // 70 // // Field appears in JSON as key "myName". 71 // Field int `json:"myName"` 72 // 73 // // Field appears in JSON as key "myName" and 74 // // the field is omitted from the object if its value is empty, 75 // // as defined above. 76 // Field int `json:"myName,omitempty"` 77 // 78 // // Field appears in JSON as key "Field" (the default), but 79 // // the field is skipped if empty. 80 // // Note the leading comma. 81 // Field int `json:",omitempty"` 82 // 83 // The "string" option signals that a field is stored as JSON inside a 84 // JSON-encoded string. It applies only to fields of string, floating point, 85 // integer, or boolean types. This extra level of encoding is sometimes used 86 // when communicating with JavaScript programs: 87 // 88 // Int64String int64 `json:",string"` 89 // 90 // The key name will be used if it's a non-empty string consisting of 91 // only Unicode letters, digits, dollar signs, percent signs, hyphens, 92 // underscores and slashes. 93 // 94 // Anonymous struct fields are usually marshaled as if their inner exported fields 95 // were fields in the outer struct, subject to the usual Go visibility rules amended 96 // as described in the next paragraph. 97 // An anonymous struct field with a name given in its JSON tag is treated as 98 // having that name, rather than being anonymous. 99 // An anonymous struct field of interface type is treated the same as having 100 // that type as its name, rather than being anonymous. 101 // 102 // The Go visibility rules for struct fields are amended for JSON when 103 // deciding which field to marshal or unmarshal. If there are 104 // multiple fields at the same level, and that level is the least 105 // nested (and would therefore be the nesting level selected by the 106 // usual Go rules), the following extra rules apply: 107 // 108 // 1) Of those fields, if any are JSON-tagged, only tagged fields are considered, 109 // even if there are multiple untagged fields that would otherwise conflict. 110 // 2) If there is exactly one field (tagged or not according to the first rule), that is selected. 111 // 3) Otherwise there are multiple fields, and all are ignored; no error occurs. 112 // 113 // Handling of anonymous struct fields is new in Go 1.1. 114 // Prior to Go 1.1, anonymous struct fields were ignored. To force ignoring of 115 // an anonymous struct field in both current and earlier versions, give the field 116 // a JSON tag of "-". 117 // 118 // Map values encode as JSON objects. 119 // The map's key type must be string; the map keys are used as JSON object 120 // keys, subject to the UTF-8 coercion described for string values above. 121 // 122 // Pointer values encode as the value pointed to. 123 // A nil pointer encodes as the null JSON object. 124 // 125 // Interface values encode as the value contained in the interface. 126 // A nil interface value encodes as the null JSON object. 127 // 128 // Channel, complex, and function values cannot be encoded in JSON. 129 // Attempting to encode such a value causes Marshal to return 130 // an UnsupportedTypeError. 131 // 132 // JSON cannot represent cyclic data structures and Marshal does not 133 // handle them. Passing cyclic structures to Marshal will result in 134 // an infinite recursion. 135 // 136 func Marshal(v interface{}) ([]byte, error) { 137 e := &encodeState{} 138 err := e.marshal(v) 139 if err != nil { 140 return nil, err 141 } 142 return e.Bytes(), nil 143 } 144 145 // MarshalIndent is like Marshal but applies Indent to format the output. 146 func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) { 147 b, err := Marshal(v) 148 if err != nil { 149 return nil, err 150 } 151 var buf bytes.Buffer 152 err = Indent(&buf, b, prefix, indent) 153 if err != nil { 154 return nil, err 155 } 156 return buf.Bytes(), nil 157 } 158 159 // HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029 160 // characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029 161 // so that the JSON will be safe to embed inside HTML <script> tags. 162 // For historical reasons, web browsers don't honor standard HTML 163 // escaping within <script> tags, so an alternative JSON encoding must 164 // be used. 165 func HTMLEscape(dst *bytes.Buffer, src []byte) { 166 // The characters can only appear in string literals, 167 // so just scan the string one byte at a time. 168 start := 0 169 for i, c := range src { 170 if c == '<' || c == '>' || c == '&' { 171 if start < i { 172 dst.Write(src[start:i]) 173 } 174 dst.WriteString(`\u00`) 175 dst.WriteByte(hex[c>>4]) 176 dst.WriteByte(hex[c&0xF]) 177 start = i + 1 178 } 179 // Convert U+2028 and U+2029 (E2 80 A8 and E2 80 A9). 180 if c == 0xE2 && i+2 < len(src) && src[i+1] == 0x80 && src[i+2]&^1 == 0xA8 { 181 if start < i { 182 dst.Write(src[start:i]) 183 } 184 dst.WriteString(`\u202`) 185 dst.WriteByte(hex[src[i+2]&0xF]) 186 start = i + 3 187 } 188 } 189 if start < len(src) { 190 dst.Write(src[start:]) 191 } 192 } 193 194 // Marshaler is the interface implemented by objects that 195 // can marshal themselves into valid JSON. 196 type Marshaler interface { 197 MarshalJSON() ([]byte, error) 198 } 199 200 // An UnsupportedTypeError is returned by Marshal when attempting 201 // to encode an unsupported value type. 202 type UnsupportedTypeError struct { 203 Type reflect.Type 204 } 205 206 func (e *UnsupportedTypeError) Error() string { 207 return "json: unsupported type: " + e.Type.String() 208 } 209 210 type UnsupportedValueError struct { 211 Value reflect.Value 212 Str string 213 } 214 215 func (e *UnsupportedValueError) Error() string { 216 return "json: unsupported value: " + e.Str 217 } 218 219 // Before Go 1.2, an InvalidUTF8Error was returned by Marshal when 220 // attempting to encode a string value with invalid UTF-8 sequences. 221 // As of Go 1.2, Marshal instead coerces the string to valid UTF-8 by 222 // replacing invalid bytes with the Unicode replacement rune U+FFFD. 223 // This error is no longer generated but is kept for backwards compatibility 224 // with programs that might mention it. 225 type InvalidUTF8Error struct { 226 S string // the whole string value that caused the error 227 } 228 229 func (e *InvalidUTF8Error) Error() string { 230 return "json: invalid UTF-8 in string: " + strconv.Quote(e.S) 231 } 232 233 type MarshalerError struct { 234 Type reflect.Type 235 Err error 236 } 237 238 func (e *MarshalerError) Error() string { 239 return "json: error calling MarshalJSON for type " + e.Type.String() + ": " + e.Err.Error() 240 } 241 242 var hex = "0123456789abcdef" 243 244 // An encodeState encodes JSON into a bytes.Buffer. 245 type encodeState struct { 246 bytes.Buffer // accumulated output 247 scratch [64]byte 248 } 249 250 var encodeStatePool sync.Pool 251 252 func newEncodeState() *encodeState { 253 if v := encodeStatePool.Get(); v != nil { 254 e := v.(*encodeState) 255 e.Reset() 256 return e 257 } 258 return new(encodeState) 259 } 260 261 func (e *encodeState) marshal(v interface{}) (err error) { 262 defer func() { 263 if r := recover(); r != nil { 264 if _, ok := r.(runtime.Error); ok { 265 panic(r) 266 } 267 if s, ok := r.(string); ok { 268 panic(s) 269 } 270 err = r.(error) 271 } 272 }() 273 e.reflectValue(reflect.ValueOf(v)) 274 return nil 275 } 276 277 func (e *encodeState) error(err error) { 278 panic(err) 279 } 280 281 func isEmptyValue(v reflect.Value) bool { 282 switch v.Kind() { 283 case reflect.Array, reflect.Map, reflect.Slice, reflect.String: 284 return v.Len() == 0 285 case reflect.Bool: 286 return !v.Bool() 287 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 288 return v.Int() == 0 289 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 290 return v.Uint() == 0 291 case reflect.Float32, reflect.Float64: 292 return v.Float() == 0 293 case reflect.Interface, reflect.Ptr: 294 return v.IsNil() 295 } 296 return false 297 } 298 299 func (e *encodeState) reflectValue(v reflect.Value) { 300 valueEncoder(v)(e, v, false) 301 } 302 303 type encoderFunc func(e *encodeState, v reflect.Value, quoted bool) 304 305 var encoderCache struct { 306 sync.RWMutex 307 m map[reflect.Type]encoderFunc 308 } 309 310 func valueEncoder(v reflect.Value) encoderFunc { 311 if !v.IsValid() { 312 return invalidValueEncoder 313 } 314 return typeEncoder(v.Type()) 315 } 316 317 func typeEncoder(t reflect.Type) encoderFunc { 318 encoderCache.RLock() 319 f := encoderCache.m[t] 320 encoderCache.RUnlock() 321 if f != nil { 322 return f 323 } 324 325 // To deal with recursive types, populate the map with an 326 // indirect func before we build it. This type waits on the 327 // real func (f) to be ready and then calls it. This indirect 328 // func is only used for recursive types. 329 encoderCache.Lock() 330 if encoderCache.m == nil { 331 encoderCache.m = make(map[reflect.Type]encoderFunc) 332 } 333 var wg sync.WaitGroup 334 wg.Add(1) 335 encoderCache.m[t] = func(e *encodeState, v reflect.Value, quoted bool) { 336 wg.Wait() 337 f(e, v, quoted) 338 } 339 encoderCache.Unlock() 340 341 // Compute fields without lock. 342 // Might duplicate effort but won't hold other computations back. 343 f = newTypeEncoder(t, true) 344 wg.Done() 345 encoderCache.Lock() 346 encoderCache.m[t] = f 347 encoderCache.Unlock() 348 return f 349 } 350 351 var ( 352 marshalerType = reflect.TypeOf(new(Marshaler)).Elem() 353 textMarshalerType = reflect.TypeOf(new(encoding.TextMarshaler)).Elem() 354 ) 355 356 // newTypeEncoder constructs an encoderFunc for a type. 357 // The returned encoder only checks CanAddr when allowAddr is true. 358 func newTypeEncoder(t reflect.Type, allowAddr bool) encoderFunc { 359 if t.Implements(marshalerType) { 360 return marshalerEncoder 361 } 362 if t.Kind() != reflect.Ptr && allowAddr { 363 if reflect.PtrTo(t).Implements(marshalerType) { 364 return newCondAddrEncoder(addrMarshalerEncoder, newTypeEncoder(t, false)) 365 } 366 } 367 368 if t.Implements(textMarshalerType) { 369 return textMarshalerEncoder 370 } 371 if t.Kind() != reflect.Ptr && allowAddr { 372 if reflect.PtrTo(t).Implements(textMarshalerType) { 373 return newCondAddrEncoder(addrTextMarshalerEncoder, newTypeEncoder(t, false)) 374 } 375 } 376 377 switch t.Kind() { 378 case reflect.Bool: 379 return boolEncoder 380 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 381 return intEncoder 382 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 383 return uintEncoder 384 case reflect.Float32: 385 return float32Encoder 386 case reflect.Float64: 387 return float64Encoder 388 case reflect.String: 389 return stringEncoder 390 case reflect.Interface: 391 return interfaceEncoder 392 case reflect.Struct: 393 return newStructEncoder(t) 394 case reflect.Map: 395 return newMapEncoder(t) 396 case reflect.Slice: 397 return newSliceEncoder(t) 398 case reflect.Array: 399 return newArrayEncoder(t) 400 case reflect.Ptr: 401 return newPtrEncoder(t) 402 default: 403 return unsupportedTypeEncoder 404 } 405 } 406 407 func invalidValueEncoder(e *encodeState, v reflect.Value, quoted bool) { 408 e.WriteString("null") 409 } 410 411 func marshalerEncoder(e *encodeState, v reflect.Value, quoted bool) { 412 if v.Kind() == reflect.Ptr && v.IsNil() { 413 e.WriteString("null") 414 return 415 } 416 m := v.Interface().(Marshaler) 417 b, err := m.MarshalJSON() 418 if err == nil { 419 // copy JSON into buffer, checking validity. 420 err = compact(&e.Buffer, b, true) 421 } 422 if err != nil { 423 e.error(&MarshalerError{v.Type(), err}) 424 } 425 } 426 427 func addrMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) { 428 va := v.Addr() 429 if va.IsNil() { 430 e.WriteString("null") 431 return 432 } 433 m := va.Interface().(Marshaler) 434 b, err := m.MarshalJSON() 435 if err == nil { 436 // copy JSON into buffer, checking validity. 437 err = compact(&e.Buffer, b, true) 438 } 439 if err != nil { 440 e.error(&MarshalerError{v.Type(), err}) 441 } 442 } 443 444 func textMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) { 445 if v.Kind() == reflect.Ptr && v.IsNil() { 446 e.WriteString("null") 447 return 448 } 449 m := v.Interface().(encoding.TextMarshaler) 450 b, err := m.MarshalText() 451 if err != nil { 452 e.error(&MarshalerError{v.Type(), err}) 453 } 454 e.stringBytes(b) 455 } 456 457 func addrTextMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) { 458 va := v.Addr() 459 if va.IsNil() { 460 e.WriteString("null") 461 return 462 } 463 m := va.Interface().(encoding.TextMarshaler) 464 b, err := m.MarshalText() 465 if err != nil { 466 e.error(&MarshalerError{v.Type(), err}) 467 } 468 e.stringBytes(b) 469 } 470 471 func boolEncoder(e *encodeState, v reflect.Value, quoted bool) { 472 if quoted { 473 e.WriteByte('"') 474 } 475 if v.Bool() { 476 e.WriteString("true") 477 } else { 478 e.WriteString("false") 479 } 480 if quoted { 481 e.WriteByte('"') 482 } 483 } 484 485 func intEncoder(e *encodeState, v reflect.Value, quoted bool) { 486 b := strconv.AppendInt(e.scratch[:0], v.Int(), 10) 487 if quoted { 488 e.WriteByte('"') 489 } 490 e.Write(b) 491 if quoted { 492 e.WriteByte('"') 493 } 494 } 495 496 func uintEncoder(e *encodeState, v reflect.Value, quoted bool) { 497 b := strconv.AppendUint(e.scratch[:0], v.Uint(), 10) 498 if quoted { 499 e.WriteByte('"') 500 } 501 e.Write(b) 502 if quoted { 503 e.WriteByte('"') 504 } 505 } 506 507 type floatEncoder int // number of bits 508 509 func (bits floatEncoder) encode(e *encodeState, v reflect.Value, quoted bool) { 510 f := v.Float() 511 if math.IsInf(f, 0) || math.IsNaN(f) { 512 e.error(&UnsupportedValueError{v, strconv.FormatFloat(f, 'g', -1, int(bits))}) 513 } 514 b := strconv.AppendFloat(e.scratch[:0], f, 'g', -1, int(bits)) 515 if quoted { 516 e.WriteByte('"') 517 } 518 e.Write(b) 519 if quoted { 520 e.WriteByte('"') 521 } 522 } 523 524 var ( 525 float32Encoder = (floatEncoder(32)).encode 526 float64Encoder = (floatEncoder(64)).encode 527 ) 528 529 func stringEncoder(e *encodeState, v reflect.Value, quoted bool) { 530 if v.Type() == numberType { 531 numStr := v.String() 532 if numStr == "" { 533 numStr = "0" // Number's zero-val 534 } 535 e.WriteString(numStr) 536 return 537 } 538 if quoted { 539 sb, err := Marshal(v.String()) 540 if err != nil { 541 e.error(err) 542 } 543 e.string(string(sb)) 544 } else { 545 e.string(v.String()) 546 } 547 } 548 549 func interfaceEncoder(e *encodeState, v reflect.Value, quoted bool) { 550 if v.IsNil() { 551 e.WriteString("null") 552 return 553 } 554 e.reflectValue(v.Elem()) 555 } 556 557 func unsupportedTypeEncoder(e *encodeState, v reflect.Value, quoted bool) { 558 e.error(&UnsupportedTypeError{v.Type()}) 559 } 560 561 type structEncoder struct { 562 fields []field 563 fieldEncs []encoderFunc 564 } 565 566 func (se *structEncoder) encode(e *encodeState, v reflect.Value, quoted bool) { 567 e.WriteByte('{') 568 first := true 569 for i, f := range se.fields { 570 fv := fieldByIndex(v, f.index) 571 if !fv.IsValid() || f.omitEmpty && isEmptyValue(fv) { 572 continue 573 } 574 if first { 575 first = false 576 } else { 577 e.WriteByte(',') 578 } 579 e.string(f.name) 580 e.WriteByte(':') 581 se.fieldEncs[i](e, fv, f.quoted) 582 } 583 e.WriteByte('}') 584 } 585 586 func newStructEncoder(t reflect.Type) encoderFunc { 587 fields := cachedTypeFields(t) 588 se := &structEncoder{ 589 fields: fields, 590 fieldEncs: make([]encoderFunc, len(fields)), 591 } 592 for i, f := range fields { 593 se.fieldEncs[i] = typeEncoder(typeByIndex(t, f.index)) 594 } 595 return se.encode 596 } 597 598 type mapEncoder struct { 599 elemEnc encoderFunc 600 } 601 602 func (me *mapEncoder) encode(e *encodeState, v reflect.Value, _ bool) { 603 if v.IsNil() { 604 e.WriteString("null") 605 return 606 } 607 e.WriteByte('{') 608 var sv stringValues = v.MapKeys() 609 sort.Sort(sv) 610 for i, k := range sv { 611 if i > 0 { 612 e.WriteByte(',') 613 } 614 e.string(k.String()) 615 e.WriteByte(':') 616 me.elemEnc(e, v.MapIndex(k), false) 617 } 618 e.WriteByte('}') 619 } 620 621 func newMapEncoder(t reflect.Type) encoderFunc { 622 if t.Key().Kind() != reflect.String { 623 return unsupportedTypeEncoder 624 } 625 me := &mapEncoder{typeEncoder(t.Elem())} 626 return me.encode 627 } 628 629 func encodeByteSlice(e *encodeState, v reflect.Value, _ bool) { 630 if v.IsNil() { 631 e.WriteString("null") 632 return 633 } 634 s := v.Bytes() 635 e.WriteByte('"') 636 if len(s) < 1024 { 637 // for small buffers, using Encode directly is much faster. 638 dst := make([]byte, base64.StdEncoding.EncodedLen(len(s))) 639 base64.StdEncoding.Encode(dst, s) 640 e.Write(dst) 641 } else { 642 // for large buffers, avoid unnecessary extra temporary 643 // buffer space. 644 enc := base64.NewEncoder(base64.StdEncoding, e) 645 enc.Write(s) 646 enc.Close() 647 } 648 e.WriteByte('"') 649 } 650 651 // sliceEncoder just wraps an arrayEncoder, checking to make sure the value isn't nil. 652 type sliceEncoder struct { 653 arrayEnc encoderFunc 654 } 655 656 func (se *sliceEncoder) encode(e *encodeState, v reflect.Value, _ bool) { 657 if v.IsNil() { 658 e.WriteString("null") 659 return 660 } 661 se.arrayEnc(e, v, false) 662 } 663 664 func newSliceEncoder(t reflect.Type) encoderFunc { 665 // Byte slices get special treatment; arrays don't. 666 if t.Elem().Kind() == reflect.Uint8 { 667 return encodeByteSlice 668 } 669 enc := &sliceEncoder{newArrayEncoder(t)} 670 return enc.encode 671 } 672 673 type arrayEncoder struct { 674 elemEnc encoderFunc 675 } 676 677 func (ae *arrayEncoder) encode(e *encodeState, v reflect.Value, _ bool) { 678 e.WriteByte('[') 679 n := v.Len() 680 for i := 0; i < n; i++ { 681 if i > 0 { 682 e.WriteByte(',') 683 } 684 ae.elemEnc(e, v.Index(i), false) 685 } 686 e.WriteByte(']') 687 } 688 689 func newArrayEncoder(t reflect.Type) encoderFunc { 690 enc := &arrayEncoder{typeEncoder(t.Elem())} 691 return enc.encode 692 } 693 694 type ptrEncoder struct { 695 elemEnc encoderFunc 696 } 697 698 func (pe *ptrEncoder) encode(e *encodeState, v reflect.Value, quoted bool) { 699 if v.IsNil() { 700 e.WriteString("null") 701 return 702 } 703 pe.elemEnc(e, v.Elem(), quoted) 704 } 705 706 func newPtrEncoder(t reflect.Type) encoderFunc { 707 enc := &ptrEncoder{typeEncoder(t.Elem())} 708 return enc.encode 709 } 710 711 type condAddrEncoder struct { 712 canAddrEnc, elseEnc encoderFunc 713 } 714 715 func (ce *condAddrEncoder) encode(e *encodeState, v reflect.Value, quoted bool) { 716 if v.CanAddr() { 717 ce.canAddrEnc(e, v, quoted) 718 } else { 719 ce.elseEnc(e, v, quoted) 720 } 721 } 722 723 // newCondAddrEncoder returns an encoder that checks whether its value 724 // CanAddr and delegates to canAddrEnc if so, else to elseEnc. 725 func newCondAddrEncoder(canAddrEnc, elseEnc encoderFunc) encoderFunc { 726 enc := &condAddrEncoder{canAddrEnc: canAddrEnc, elseEnc: elseEnc} 727 return enc.encode 728 } 729 730 func isValidTag(s string) bool { 731 if s == "" { 732 return false 733 } 734 for _, c := range s { 735 switch { 736 case strings.ContainsRune("!#$%&()*+-./:<=>?@[]^_{|}~ ", c): 737 // Backslash and quote chars are reserved, but 738 // otherwise any punctuation chars are allowed 739 // in a tag name. 740 default: 741 if !unicode.IsLetter(c) && !unicode.IsDigit(c) { 742 return false 743 } 744 } 745 } 746 return true 747 } 748 749 func fieldByIndex(v reflect.Value, index []int) reflect.Value { 750 for _, i := range index { 751 if v.Kind() == reflect.Ptr { 752 if v.IsNil() { 753 return reflect.Value{} 754 } 755 v = v.Elem() 756 } 757 v = v.Field(i) 758 } 759 return v 760 } 761 762 func typeByIndex(t reflect.Type, index []int) reflect.Type { 763 for _, i := range index { 764 if t.Kind() == reflect.Ptr { 765 t = t.Elem() 766 } 767 t = t.Field(i).Type 768 } 769 return t 770 } 771 772 // stringValues is a slice of reflect.Value holding *reflect.StringValue. 773 // It implements the methods to sort by string. 774 type stringValues []reflect.Value 775 776 func (sv stringValues) Len() int { return len(sv) } 777 func (sv stringValues) Swap(i, j int) { sv[i], sv[j] = sv[j], sv[i] } 778 func (sv stringValues) Less(i, j int) bool { return sv.get(i) < sv.get(j) } 779 func (sv stringValues) get(i int) string { return sv[i].String() } 780 781 // NOTE: keep in sync with stringBytes below. 782 func (e *encodeState) string(s string) int { 783 len0 := e.Len() 784 e.WriteByte('"') 785 start := 0 786 for i := 0; i < len(s); { 787 if b := s[i]; b < utf8.RuneSelf { 788 if 0x20 <= b && b != '\\' && b != '"' && b != '<' && b != '>' && b != '&' { 789 i++ 790 continue 791 } 792 if start < i { 793 e.WriteString(s[start:i]) 794 } 795 switch b { 796 case '\\', '"': 797 e.WriteByte('\\') 798 e.WriteByte(b) 799 case '\n': 800 e.WriteByte('\\') 801 e.WriteByte('n') 802 case '\r': 803 e.WriteByte('\\') 804 e.WriteByte('r') 805 case '\t': 806 e.WriteByte('\\') 807 e.WriteByte('t') 808 default: 809 // This encodes bytes < 0x20 except for \n and \r, 810 // as well as <, > and &. The latter are escaped because they 811 // can lead to security holes when user-controlled strings 812 // are rendered into JSON and served to some browsers. 813 e.WriteString(`\u00`) 814 e.WriteByte(hex[b>>4]) 815 e.WriteByte(hex[b&0xF]) 816 } 817 i++ 818 start = i 819 continue 820 } 821 c, size := utf8.DecodeRuneInString(s[i:]) 822 if c == utf8.RuneError && size == 1 { 823 if start < i { 824 e.WriteString(s[start:i]) 825 } 826 e.WriteString(`\ufffd`) 827 i += size 828 start = i 829 continue 830 } 831 // U+2028 is LINE SEPARATOR. 832 // U+2029 is PARAGRAPH SEPARATOR. 833 // They are both technically valid characters in JSON strings, 834 // but don't work in JSONP, which has to be evaluated as JavaScript, 835 // and can lead to security holes there. It is valid JSON to 836 // escape them, so we do so unconditionally. 837 // See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion. 838 if c == '\u2028' || c == '\u2029' { 839 if start < i { 840 e.WriteString(s[start:i]) 841 } 842 e.WriteString(`\u202`) 843 e.WriteByte(hex[c&0xF]) 844 i += size 845 start = i 846 continue 847 } 848 i += size 849 } 850 if start < len(s) { 851 e.WriteString(s[start:]) 852 } 853 e.WriteByte('"') 854 return e.Len() - len0 855 } 856 857 // NOTE: keep in sync with string above. 858 func (e *encodeState) stringBytes(s []byte) int { 859 len0 := e.Len() 860 e.WriteByte('"') 861 start := 0 862 for i := 0; i < len(s); { 863 if b := s[i]; b < utf8.RuneSelf { 864 if 0x20 <= b && b != '\\' && b != '"' && b != '<' && b != '>' && b != '&' { 865 i++ 866 continue 867 } 868 if start < i { 869 e.Write(s[start:i]) 870 } 871 switch b { 872 case '\\', '"': 873 e.WriteByte('\\') 874 e.WriteByte(b) 875 case '\n': 876 e.WriteByte('\\') 877 e.WriteByte('n') 878 case '\r': 879 e.WriteByte('\\') 880 e.WriteByte('r') 881 case '\t': 882 e.WriteByte('\\') 883 e.WriteByte('t') 884 default: 885 // This encodes bytes < 0x20 except for \n and \r, 886 // as well as <, >, and &. The latter are escaped because they 887 // can lead to security holes when user-controlled strings 888 // are rendered into JSON and served to some browsers. 889 e.WriteString(`\u00`) 890 e.WriteByte(hex[b>>4]) 891 e.WriteByte(hex[b&0xF]) 892 } 893 i++ 894 start = i 895 continue 896 } 897 c, size := utf8.DecodeRune(s[i:]) 898 if c == utf8.RuneError && size == 1 { 899 if start < i { 900 e.Write(s[start:i]) 901 } 902 e.WriteString(`\ufffd`) 903 i += size 904 start = i 905 continue 906 } 907 // U+2028 is LINE SEPARATOR. 908 // U+2029 is PARAGRAPH SEPARATOR. 909 // They are both technically valid characters in JSON strings, 910 // but don't work in JSONP, which has to be evaluated as JavaScript, 911 // and can lead to security holes there. It is valid JSON to 912 // escape them, so we do so unconditionally. 913 // See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion. 914 if c == '\u2028' || c == '\u2029' { 915 if start < i { 916 e.Write(s[start:i]) 917 } 918 e.WriteString(`\u202`) 919 e.WriteByte(hex[c&0xF]) 920 i += size 921 start = i 922 continue 923 } 924 i += size 925 } 926 if start < len(s) { 927 e.Write(s[start:]) 928 } 929 e.WriteByte('"') 930 return e.Len() - len0 931 } 932 933 // A field represents a single field found in a struct. 934 type field struct { 935 name string 936 nameBytes []byte // []byte(name) 937 equalFold func(s, t []byte) bool // bytes.EqualFold or equivalent 938 939 tag bool 940 index []int 941 typ reflect.Type 942 omitEmpty bool 943 quoted bool 944 } 945 946 func fillField(f field) field { 947 f.nameBytes = []byte(f.name) 948 f.equalFold = foldFunc(f.nameBytes) 949 return f 950 } 951 952 // byName sorts field by name, breaking ties with depth, 953 // then breaking ties with "name came from json tag", then 954 // breaking ties with index sequence. 955 type byName []field 956 957 func (x byName) Len() int { return len(x) } 958 959 func (x byName) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 960 961 func (x byName) Less(i, j int) bool { 962 if x[i].name != x[j].name { 963 return x[i].name < x[j].name 964 } 965 if len(x[i].index) != len(x[j].index) { 966 return len(x[i].index) < len(x[j].index) 967 } 968 if x[i].tag != x[j].tag { 969 return x[i].tag 970 } 971 return byIndex(x).Less(i, j) 972 } 973 974 // byIndex sorts field by index sequence. 975 type byIndex []field 976 977 func (x byIndex) Len() int { return len(x) } 978 979 func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 980 981 func (x byIndex) Less(i, j int) bool { 982 for k, xik := range x[i].index { 983 if k >= len(x[j].index) { 984 return false 985 } 986 if xik != x[j].index[k] { 987 return xik < x[j].index[k] 988 } 989 } 990 return len(x[i].index) < len(x[j].index) 991 } 992 993 // typeFields returns a list of fields that JSON should recognize for the given type. 994 // The algorithm is breadth-first search over the set of structs to include - the top struct 995 // and then any reachable anonymous structs. 996 func typeFields(t reflect.Type) []field { 997 // Anonymous fields to explore at the current level and the next. 998 current := []field{} 999 next := []field{{typ: t}} 1000 1001 // Count of queued names for current level and the next. 1002 count := map[reflect.Type]int{} 1003 nextCount := map[reflect.Type]int{} 1004 1005 // Types already visited at an earlier level. 1006 visited := map[reflect.Type]bool{} 1007 1008 // Fields found. 1009 var fields []field 1010 1011 for len(next) > 0 { 1012 current, next = next, current[:0] 1013 count, nextCount = nextCount, map[reflect.Type]int{} 1014 1015 for _, f := range current { 1016 if visited[f.typ] { 1017 continue 1018 } 1019 visited[f.typ] = true 1020 1021 // Scan f.typ for fields to include. 1022 for i := 0; i < f.typ.NumField(); i++ { 1023 sf := f.typ.Field(i) 1024 if sf.PkgPath != "" && !sf.Anonymous { // unexported 1025 continue 1026 } 1027 tag := sf.Tag.Get("json") 1028 if tag == "-" { 1029 continue 1030 } 1031 name, opts := parseTag(tag) 1032 if !isValidTag(name) { 1033 name = "" 1034 } 1035 index := make([]int, len(f.index)+1) 1036 copy(index, f.index) 1037 index[len(f.index)] = i 1038 1039 ft := sf.Type 1040 if ft.Name() == "" && ft.Kind() == reflect.Ptr { 1041 // Follow pointer. 1042 ft = ft.Elem() 1043 } 1044 1045 // Only strings, floats, integers, and booleans can be quoted. 1046 quoted := false 1047 if opts.Contains("string") { 1048 switch ft.Kind() { 1049 case reflect.Bool, 1050 reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, 1051 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, 1052 reflect.Float32, reflect.Float64, 1053 reflect.String: 1054 quoted = true 1055 } 1056 } 1057 1058 // Record found field and index sequence. 1059 if name != "" || !sf.Anonymous || ft.Kind() != reflect.Struct { 1060 tagged := name != "" 1061 if name == "" { 1062 name = sf.Name 1063 } 1064 fields = append(fields, fillField(field{ 1065 name: name, 1066 tag: tagged, 1067 index: index, 1068 typ: ft, 1069 omitEmpty: opts.Contains("omitempty"), 1070 quoted: quoted, 1071 })) 1072 if count[f.typ] > 1 { 1073 // If there were multiple instances, add a second, 1074 // so that the annihilation code will see a duplicate. 1075 // It only cares about the distinction between 1 or 2, 1076 // so don't bother generating any more copies. 1077 fields = append(fields, fields[len(fields)-1]) 1078 } 1079 continue 1080 } 1081 1082 // Record new anonymous struct to explore in next round. 1083 nextCount[ft]++ 1084 if nextCount[ft] == 1 { 1085 next = append(next, fillField(field{name: ft.Name(), index: index, typ: ft})) 1086 } 1087 } 1088 } 1089 } 1090 1091 sort.Sort(byName(fields)) 1092 1093 // Delete all fields that are hidden by the Go rules for embedded fields, 1094 // except that fields with JSON tags are promoted. 1095 1096 // The fields are sorted in primary order of name, secondary order 1097 // of field index length. Loop over names; for each name, delete 1098 // hidden fields by choosing the one dominant field that survives. 1099 out := fields[:0] 1100 for advance, i := 0, 0; i < len(fields); i += advance { 1101 // One iteration per name. 1102 // Find the sequence of fields with the name of this first field. 1103 fi := fields[i] 1104 name := fi.name 1105 for advance = 1; i+advance < len(fields); advance++ { 1106 fj := fields[i+advance] 1107 if fj.name != name { 1108 break 1109 } 1110 } 1111 if advance == 1 { // Only one field with this name 1112 out = append(out, fi) 1113 continue 1114 } 1115 dominant, ok := dominantField(fields[i : i+advance]) 1116 if ok { 1117 out = append(out, dominant) 1118 } 1119 } 1120 1121 fields = out 1122 sort.Sort(byIndex(fields)) 1123 1124 return fields 1125 } 1126 1127 // dominantField looks through the fields, all of which are known to 1128 // have the same name, to find the single field that dominates the 1129 // others using Go's embedding rules, modified by the presence of 1130 // JSON tags. If there are multiple top-level fields, the boolean 1131 // will be false: This condition is an error in Go and we skip all 1132 // the fields. 1133 func dominantField(fields []field) (field, bool) { 1134 // The fields are sorted in increasing index-length order. The winner 1135 // must therefore be one with the shortest index length. Drop all 1136 // longer entries, which is easy: just truncate the slice. 1137 length := len(fields[0].index) 1138 tagged := -1 // Index of first tagged field. 1139 for i, f := range fields { 1140 if len(f.index) > length { 1141 fields = fields[:i] 1142 break 1143 } 1144 if f.tag { 1145 if tagged >= 0 { 1146 // Multiple tagged fields at the same level: conflict. 1147 // Return no field. 1148 return field{}, false 1149 } 1150 tagged = i 1151 } 1152 } 1153 if tagged >= 0 { 1154 return fields[tagged], true 1155 } 1156 // All remaining fields have the same length. If there's more than one, 1157 // we have a conflict (two fields named "X" at the same level) and we 1158 // return no field. 1159 if len(fields) > 1 { 1160 return field{}, false 1161 } 1162 return fields[0], true 1163 } 1164 1165 var fieldCache struct { 1166 sync.RWMutex 1167 m map[reflect.Type][]field 1168 } 1169 1170 // cachedTypeFields is like typeFields but uses a cache to avoid repeated work. 1171 func cachedTypeFields(t reflect.Type) []field { 1172 fieldCache.RLock() 1173 f := fieldCache.m[t] 1174 fieldCache.RUnlock() 1175 if f != nil { 1176 return f 1177 } 1178 1179 // Compute fields without lock. 1180 // Might duplicate effort but won't hold other computations back. 1181 f = typeFields(t) 1182 if f == nil { 1183 f = []field{} 1184 } 1185 1186 fieldCache.Lock() 1187 if fieldCache.m == nil { 1188 fieldCache.m = map[reflect.Type][]field{} 1189 } 1190 fieldCache.m[t] = f 1191 fieldCache.Unlock() 1192 return f 1193 }