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