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