github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/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. 120 // The map's key type must be string; 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 } else if !Number(numStr).IsValid() { 538 e.error(fmt.Errorf("json: invalid number literal, trying to marshal %s", v.String())) 539 } 540 e.WriteString(numStr) 541 return 542 } 543 if quoted { 544 sb, err := Marshal(v.String()) 545 if err != nil { 546 e.error(err) 547 } 548 e.string(string(sb)) 549 } else { 550 e.string(v.String()) 551 } 552 } 553 554 func interfaceEncoder(e *encodeState, v reflect.Value, quoted bool) { 555 if v.IsNil() { 556 e.WriteString("null") 557 return 558 } 559 e.reflectValue(v.Elem()) 560 } 561 562 func unsupportedTypeEncoder(e *encodeState, v reflect.Value, quoted bool) { 563 e.error(&UnsupportedTypeError{v.Type()}) 564 } 565 566 type structEncoder struct { 567 fields []field 568 fieldEncs []encoderFunc 569 } 570 571 func (se *structEncoder) encode(e *encodeState, v reflect.Value, quoted bool) { 572 e.WriteByte('{') 573 first := true 574 for i, f := range se.fields { 575 fv := fieldByIndex(v, f.index) 576 if !fv.IsValid() || f.omitEmpty && isEmptyValue(fv) { 577 continue 578 } 579 if first { 580 first = false 581 } else { 582 e.WriteByte(',') 583 } 584 e.string(f.name) 585 e.WriteByte(':') 586 se.fieldEncs[i](e, fv, f.quoted) 587 } 588 e.WriteByte('}') 589 } 590 591 func newStructEncoder(t reflect.Type) encoderFunc { 592 fields := cachedTypeFields(t) 593 se := &structEncoder{ 594 fields: fields, 595 fieldEncs: make([]encoderFunc, len(fields)), 596 } 597 for i, f := range fields { 598 se.fieldEncs[i] = typeEncoder(typeByIndex(t, f.index)) 599 } 600 return se.encode 601 } 602 603 type mapEncoder struct { 604 elemEnc encoderFunc 605 } 606 607 func (me *mapEncoder) encode(e *encodeState, v reflect.Value, _ bool) { 608 if v.IsNil() { 609 e.WriteString("null") 610 return 611 } 612 e.WriteByte('{') 613 var sv stringValues = v.MapKeys() 614 sort.Sort(sv) 615 for i, k := range sv { 616 if i > 0 { 617 e.WriteByte(',') 618 } 619 e.string(k.String()) 620 e.WriteByte(':') 621 me.elemEnc(e, v.MapIndex(k), false) 622 } 623 e.WriteByte('}') 624 } 625 626 func newMapEncoder(t reflect.Type) encoderFunc { 627 if t.Key().Kind() != reflect.String { 628 return unsupportedTypeEncoder 629 } 630 me := &mapEncoder{typeEncoder(t.Elem())} 631 return me.encode 632 } 633 634 func encodeByteSlice(e *encodeState, v reflect.Value, _ bool) { 635 if v.IsNil() { 636 e.WriteString("null") 637 return 638 } 639 s := v.Bytes() 640 e.WriteByte('"') 641 if len(s) < 1024 { 642 // for small buffers, using Encode directly is much faster. 643 dst := make([]byte, base64.StdEncoding.EncodedLen(len(s))) 644 base64.StdEncoding.Encode(dst, s) 645 e.Write(dst) 646 } else { 647 // for large buffers, avoid unnecessary extra temporary 648 // buffer space. 649 enc := base64.NewEncoder(base64.StdEncoding, e) 650 enc.Write(s) 651 enc.Close() 652 } 653 e.WriteByte('"') 654 } 655 656 // sliceEncoder just wraps an arrayEncoder, checking to make sure the value isn't nil. 657 type sliceEncoder struct { 658 arrayEnc encoderFunc 659 } 660 661 func (se *sliceEncoder) encode(e *encodeState, v reflect.Value, _ bool) { 662 if v.IsNil() { 663 e.WriteString("null") 664 return 665 } 666 se.arrayEnc(e, v, false) 667 } 668 669 func newSliceEncoder(t reflect.Type) encoderFunc { 670 // Byte slices get special treatment; arrays don't. 671 if t.Elem().Kind() == reflect.Uint8 { 672 return encodeByteSlice 673 } 674 enc := &sliceEncoder{newArrayEncoder(t)} 675 return enc.encode 676 } 677 678 type arrayEncoder struct { 679 elemEnc encoderFunc 680 } 681 682 func (ae *arrayEncoder) encode(e *encodeState, v reflect.Value, _ bool) { 683 e.WriteByte('[') 684 n := v.Len() 685 for i := 0; i < n; i++ { 686 if i > 0 { 687 e.WriteByte(',') 688 } 689 ae.elemEnc(e, v.Index(i), false) 690 } 691 e.WriteByte(']') 692 } 693 694 func newArrayEncoder(t reflect.Type) encoderFunc { 695 enc := &arrayEncoder{typeEncoder(t.Elem())} 696 return enc.encode 697 } 698 699 type ptrEncoder struct { 700 elemEnc encoderFunc 701 } 702 703 func (pe *ptrEncoder) encode(e *encodeState, v reflect.Value, quoted bool) { 704 if v.IsNil() { 705 e.WriteString("null") 706 return 707 } 708 pe.elemEnc(e, v.Elem(), quoted) 709 } 710 711 func newPtrEncoder(t reflect.Type) encoderFunc { 712 enc := &ptrEncoder{typeEncoder(t.Elem())} 713 return enc.encode 714 } 715 716 type condAddrEncoder struct { 717 canAddrEnc, elseEnc encoderFunc 718 } 719 720 func (ce *condAddrEncoder) encode(e *encodeState, v reflect.Value, quoted bool) { 721 if v.CanAddr() { 722 ce.canAddrEnc(e, v, quoted) 723 } else { 724 ce.elseEnc(e, v, quoted) 725 } 726 } 727 728 // newCondAddrEncoder returns an encoder that checks whether its value 729 // CanAddr and delegates to canAddrEnc if so, else to elseEnc. 730 func newCondAddrEncoder(canAddrEnc, elseEnc encoderFunc) encoderFunc { 731 enc := &condAddrEncoder{canAddrEnc: canAddrEnc, elseEnc: elseEnc} 732 return enc.encode 733 } 734 735 func isValidTag(s string) bool { 736 if s == "" { 737 return false 738 } 739 for _, c := range s { 740 switch { 741 case strings.ContainsRune("!#$%&()*+-./:<=>?@[]^_{|}~ ", c): 742 // Backslash and quote chars are reserved, but 743 // otherwise any punctuation chars are allowed 744 // in a tag name. 745 default: 746 if !unicode.IsLetter(c) && !unicode.IsDigit(c) { 747 return false 748 } 749 } 750 } 751 return true 752 } 753 754 func fieldByIndex(v reflect.Value, index []int) reflect.Value { 755 for _, i := range index { 756 if v.Kind() == reflect.Ptr { 757 if v.IsNil() { 758 return reflect.Value{} 759 } 760 v = v.Elem() 761 } 762 v = v.Field(i) 763 } 764 return v 765 } 766 767 func typeByIndex(t reflect.Type, index []int) reflect.Type { 768 for _, i := range index { 769 if t.Kind() == reflect.Ptr { 770 t = t.Elem() 771 } 772 t = t.Field(i).Type 773 } 774 return t 775 } 776 777 // stringValues is a slice of reflect.Value holding *reflect.StringValue. 778 // It implements the methods to sort by string. 779 type stringValues []reflect.Value 780 781 func (sv stringValues) Len() int { return len(sv) } 782 func (sv stringValues) Swap(i, j int) { sv[i], sv[j] = sv[j], sv[i] } 783 func (sv stringValues) Less(i, j int) bool { return sv.get(i) < sv.get(j) } 784 func (sv stringValues) get(i int) string { return sv[i].String() } 785 786 // NOTE: keep in sync with stringBytes below. 787 func (e *encodeState) string(s string) int { 788 len0 := e.Len() 789 e.WriteByte('"') 790 start := 0 791 for i := 0; i < len(s); { 792 if b := s[i]; b < utf8.RuneSelf { 793 if 0x20 <= b && b != '\\' && b != '"' && b != '<' && b != '>' && b != '&' { 794 i++ 795 continue 796 } 797 if start < i { 798 e.WriteString(s[start:i]) 799 } 800 switch b { 801 case '\\', '"': 802 e.WriteByte('\\') 803 e.WriteByte(b) 804 case '\n': 805 e.WriteByte('\\') 806 e.WriteByte('n') 807 case '\r': 808 e.WriteByte('\\') 809 e.WriteByte('r') 810 case '\t': 811 e.WriteByte('\\') 812 e.WriteByte('t') 813 default: 814 // This encodes bytes < 0x20 except for \n and \r, 815 // as well as <, > and &. The latter are escaped because they 816 // can lead to security holes when user-controlled strings 817 // are rendered into JSON and served to some browsers. 818 e.WriteString(`\u00`) 819 e.WriteByte(hex[b>>4]) 820 e.WriteByte(hex[b&0xF]) 821 } 822 i++ 823 start = i 824 continue 825 } 826 c, size := utf8.DecodeRuneInString(s[i:]) 827 if c == utf8.RuneError && size == 1 { 828 if start < i { 829 e.WriteString(s[start:i]) 830 } 831 e.WriteString(`\ufffd`) 832 i += size 833 start = i 834 continue 835 } 836 // U+2028 is LINE SEPARATOR. 837 // U+2029 is PARAGRAPH SEPARATOR. 838 // They are both technically valid characters in JSON strings, 839 // but don't work in JSONP, which has to be evaluated as JavaScript, 840 // and can lead to security holes there. It is valid JSON to 841 // escape them, so we do so unconditionally. 842 // See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion. 843 if c == '\u2028' || c == '\u2029' { 844 if start < i { 845 e.WriteString(s[start:i]) 846 } 847 e.WriteString(`\u202`) 848 e.WriteByte(hex[c&0xF]) 849 i += size 850 start = i 851 continue 852 } 853 i += size 854 } 855 if start < len(s) { 856 e.WriteString(s[start:]) 857 } 858 e.WriteByte('"') 859 return e.Len() - len0 860 } 861 862 // NOTE: keep in sync with string above. 863 func (e *encodeState) stringBytes(s []byte) int { 864 len0 := e.Len() 865 e.WriteByte('"') 866 start := 0 867 for i := 0; i < len(s); { 868 if b := s[i]; b < utf8.RuneSelf { 869 if 0x20 <= b && b != '\\' && b != '"' && b != '<' && b != '>' && b != '&' { 870 i++ 871 continue 872 } 873 if start < i { 874 e.Write(s[start:i]) 875 } 876 switch b { 877 case '\\', '"': 878 e.WriteByte('\\') 879 e.WriteByte(b) 880 case '\n': 881 e.WriteByte('\\') 882 e.WriteByte('n') 883 case '\r': 884 e.WriteByte('\\') 885 e.WriteByte('r') 886 case '\t': 887 e.WriteByte('\\') 888 e.WriteByte('t') 889 default: 890 // This encodes bytes < 0x20 except for \n and \r, 891 // as well as <, >, and &. The latter are escaped because they 892 // can lead to security holes when user-controlled strings 893 // are rendered into JSON and served to some browsers. 894 e.WriteString(`\u00`) 895 e.WriteByte(hex[b>>4]) 896 e.WriteByte(hex[b&0xF]) 897 } 898 i++ 899 start = i 900 continue 901 } 902 c, size := utf8.DecodeRune(s[i:]) 903 if c == utf8.RuneError && size == 1 { 904 if start < i { 905 e.Write(s[start:i]) 906 } 907 e.WriteString(`\ufffd`) 908 i += size 909 start = i 910 continue 911 } 912 // U+2028 is LINE SEPARATOR. 913 // U+2029 is PARAGRAPH SEPARATOR. 914 // They are both technically valid characters in JSON strings, 915 // but don't work in JSONP, which has to be evaluated as JavaScript, 916 // and can lead to security holes there. It is valid JSON to 917 // escape them, so we do so unconditionally. 918 // See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion. 919 if c == '\u2028' || c == '\u2029' { 920 if start < i { 921 e.Write(s[start:i]) 922 } 923 e.WriteString(`\u202`) 924 e.WriteByte(hex[c&0xF]) 925 i += size 926 start = i 927 continue 928 } 929 i += size 930 } 931 if start < len(s) { 932 e.Write(s[start:]) 933 } 934 e.WriteByte('"') 935 return e.Len() - len0 936 } 937 938 // A field represents a single field found in a struct. 939 type field struct { 940 name string 941 nameBytes []byte // []byte(name) 942 equalFold func(s, t []byte) bool // bytes.EqualFold or equivalent 943 944 tag bool 945 index []int 946 typ reflect.Type 947 omitEmpty bool 948 quoted bool 949 } 950 951 func fillField(f field) field { 952 f.nameBytes = []byte(f.name) 953 f.equalFold = foldFunc(f.nameBytes) 954 return f 955 } 956 957 // byName sorts field by name, breaking ties with depth, 958 // then breaking ties with "name came from json tag", then 959 // breaking ties with index sequence. 960 type byName []field 961 962 func (x byName) Len() int { return len(x) } 963 964 func (x byName) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 965 966 func (x byName) Less(i, j int) bool { 967 if x[i].name != x[j].name { 968 return x[i].name < x[j].name 969 } 970 if len(x[i].index) != len(x[j].index) { 971 return len(x[i].index) < len(x[j].index) 972 } 973 if x[i].tag != x[j].tag { 974 return x[i].tag 975 } 976 return byIndex(x).Less(i, j) 977 } 978 979 // byIndex sorts field by index sequence. 980 type byIndex []field 981 982 func (x byIndex) Len() int { return len(x) } 983 984 func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 985 986 func (x byIndex) Less(i, j int) bool { 987 for k, xik := range x[i].index { 988 if k >= len(x[j].index) { 989 return false 990 } 991 if xik != x[j].index[k] { 992 return xik < x[j].index[k] 993 } 994 } 995 return len(x[i].index) < len(x[j].index) 996 } 997 998 // typeFields returns a list of fields that JSON should recognize for the given type. 999 // The algorithm is breadth-first search over the set of structs to include - the top struct 1000 // and then any reachable anonymous structs. 1001 func typeFields(t reflect.Type) []field { 1002 // Anonymous fields to explore at the current level and the next. 1003 current := []field{} 1004 next := []field{{typ: t}} 1005 1006 // Count of queued names for current level and the next. 1007 count := map[reflect.Type]int{} 1008 nextCount := map[reflect.Type]int{} 1009 1010 // Types already visited at an earlier level. 1011 visited := map[reflect.Type]bool{} 1012 1013 // Fields found. 1014 var fields []field 1015 1016 for len(next) > 0 { 1017 current, next = next, current[:0] 1018 count, nextCount = nextCount, map[reflect.Type]int{} 1019 1020 for _, f := range current { 1021 if visited[f.typ] { 1022 continue 1023 } 1024 visited[f.typ] = true 1025 1026 // Scan f.typ for fields to include. 1027 for i := 0; i < f.typ.NumField(); i++ { 1028 sf := f.typ.Field(i) 1029 if sf.PkgPath != "" && !sf.Anonymous { // unexported 1030 continue 1031 } 1032 tag := sf.Tag.Get("json") 1033 if tag == "-" { 1034 continue 1035 } 1036 name, opts := parseTag(tag) 1037 if !isValidTag(name) { 1038 name = "" 1039 } 1040 index := make([]int, len(f.index)+1) 1041 copy(index, f.index) 1042 index[len(f.index)] = i 1043 1044 ft := sf.Type 1045 if ft.Name() == "" && ft.Kind() == reflect.Ptr { 1046 // Follow pointer. 1047 ft = ft.Elem() 1048 } 1049 1050 // Only strings, floats, integers, and booleans can be quoted. 1051 quoted := false 1052 if opts.Contains("string") { 1053 switch ft.Kind() { 1054 case reflect.Bool, 1055 reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, 1056 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, 1057 reflect.Float32, reflect.Float64, 1058 reflect.String: 1059 quoted = true 1060 } 1061 } 1062 1063 // Record found field and index sequence. 1064 if name != "" || !sf.Anonymous || ft.Kind() != reflect.Struct { 1065 tagged := name != "" 1066 if name == "" { 1067 name = sf.Name 1068 } 1069 fields = append(fields, fillField(field{ 1070 name: name, 1071 tag: tagged, 1072 index: index, 1073 typ: ft, 1074 omitEmpty: opts.Contains("omitempty"), 1075 quoted: quoted, 1076 })) 1077 if count[f.typ] > 1 { 1078 // If there were multiple instances, add a second, 1079 // so that the annihilation code will see a duplicate. 1080 // It only cares about the distinction between 1 or 2, 1081 // so don't bother generating any more copies. 1082 fields = append(fields, fields[len(fields)-1]) 1083 } 1084 continue 1085 } 1086 1087 // Record new anonymous struct to explore in next round. 1088 nextCount[ft]++ 1089 if nextCount[ft] == 1 { 1090 next = append(next, fillField(field{name: ft.Name(), index: index, typ: ft})) 1091 } 1092 } 1093 } 1094 } 1095 1096 sort.Sort(byName(fields)) 1097 1098 // Delete all fields that are hidden by the Go rules for embedded fields, 1099 // except that fields with JSON tags are promoted. 1100 1101 // The fields are sorted in primary order of name, secondary order 1102 // of field index length. Loop over names; for each name, delete 1103 // hidden fields by choosing the one dominant field that survives. 1104 out := fields[:0] 1105 for advance, i := 0, 0; i < len(fields); i += advance { 1106 // One iteration per name. 1107 // Find the sequence of fields with the name of this first field. 1108 fi := fields[i] 1109 name := fi.name 1110 for advance = 1; i+advance < len(fields); advance++ { 1111 fj := fields[i+advance] 1112 if fj.name != name { 1113 break 1114 } 1115 } 1116 if advance == 1 { // Only one field with this name 1117 out = append(out, fi) 1118 continue 1119 } 1120 dominant, ok := dominantField(fields[i : i+advance]) 1121 if ok { 1122 out = append(out, dominant) 1123 } 1124 } 1125 1126 fields = out 1127 sort.Sort(byIndex(fields)) 1128 1129 return fields 1130 } 1131 1132 // dominantField looks through the fields, all of which are known to 1133 // have the same name, to find the single field that dominates the 1134 // others using Go's embedding rules, modified by the presence of 1135 // JSON tags. If there are multiple top-level fields, the boolean 1136 // will be false: This condition is an error in Go and we skip all 1137 // the fields. 1138 func dominantField(fields []field) (field, bool) { 1139 // The fields are sorted in increasing index-length order. The winner 1140 // must therefore be one with the shortest index length. Drop all 1141 // longer entries, which is easy: just truncate the slice. 1142 length := len(fields[0].index) 1143 tagged := -1 // Index of first tagged field. 1144 for i, f := range fields { 1145 if len(f.index) > length { 1146 fields = fields[:i] 1147 break 1148 } 1149 if f.tag { 1150 if tagged >= 0 { 1151 // Multiple tagged fields at the same level: conflict. 1152 // Return no field. 1153 return field{}, false 1154 } 1155 tagged = i 1156 } 1157 } 1158 if tagged >= 0 { 1159 return fields[tagged], true 1160 } 1161 // All remaining fields have the same length. If there's more than one, 1162 // we have a conflict (two fields named "X" at the same level) and we 1163 // return no field. 1164 if len(fields) > 1 { 1165 return field{}, false 1166 } 1167 return fields[0], true 1168 } 1169 1170 var fieldCache struct { 1171 sync.RWMutex 1172 m map[reflect.Type][]field 1173 } 1174 1175 // cachedTypeFields is like typeFields but uses a cache to avoid repeated work. 1176 func cachedTypeFields(t reflect.Type) []field { 1177 fieldCache.RLock() 1178 f := fieldCache.m[t] 1179 fieldCache.RUnlock() 1180 if f != nil { 1181 return f 1182 } 1183 1184 // Compute fields without lock. 1185 // Might duplicate effort but won't hold other computations back. 1186 f = typeFields(t) 1187 if f == nil { 1188 f = []field{} 1189 } 1190 1191 fieldCache.Lock() 1192 if fieldCache.m == nil { 1193 fieldCache.m = map[reflect.Type][]field{} 1194 } 1195 fieldCache.m[t] = f 1196 fieldCache.Unlock() 1197 return f 1198 }