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