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