1package json 2 3import ( 4 "encoding" 5 "encoding/json" 6 "fmt" 7 "reflect" 8 "sort" 9 "strconv" 10 "strings" 11 "sync/atomic" 12 "time" 13 "unicode" 14 "unsafe" 15) 16 17type codec struct { 18 encode encodeFunc 19 decode decodeFunc 20} 21 22type encoder struct{ flags AppendFlags } 23type decoder struct{ flags ParseFlags } 24 25type encodeFunc func(encoder, []byte, unsafe.Pointer) ([]byte, error) 26type decodeFunc func(decoder, []byte, unsafe.Pointer) ([]byte, error) 27 28type emptyFunc func(unsafe.Pointer) bool 29type sortFunc func([]reflect.Value) 30 31var ( 32 // Eventually consistent cache mapping go types to dynamically generated 33 // codecs. 34 // 35 // Note: using a uintptr as key instead of reflect.Type shaved ~15ns off of 36 // the ~30ns Marhsal/Unmarshal functions which were dominated by the map 37 // lookup time for simple types like bool, int, etc.. 38 cache unsafe.Pointer // map[unsafe.Pointer]codec 39) 40 41func cacheLoad() map[unsafe.Pointer]codec { 42 p := atomic.LoadPointer(&cache) 43 return *(*map[unsafe.Pointer]codec)(unsafe.Pointer(&p)) 44} 45 46func cacheStore(typ reflect.Type, cod codec, oldCodecs map[unsafe.Pointer]codec) { 47 newCodecs := make(map[unsafe.Pointer]codec, len(oldCodecs)+1) 48 newCodecs[typeid(typ)] = cod 49 50 for t, c := range oldCodecs { 51 newCodecs[t] = c 52 } 53 54 atomic.StorePointer(&cache, *(*unsafe.Pointer)(unsafe.Pointer(&newCodecs))) 55} 56 57func typeid(t reflect.Type) unsafe.Pointer { 58 return (*iface)(unsafe.Pointer(&t)).ptr 59} 60 61func constructCachedCodec(t reflect.Type, cache map[unsafe.Pointer]codec) codec { 62 c := constructCodec(t, map[reflect.Type]*structType{}, t.Kind() == reflect.Ptr) 63 64 if inlined(t) { 65 c.encode = constructInlineValueEncodeFunc(c.encode) 66 } 67 68 cacheStore(t, c, cache) 69 return c 70} 71 72func constructCodec(t reflect.Type, seen map[reflect.Type]*structType, canAddr bool) (c codec) { 73 switch t { 74 case nullType, nil: 75 c = codec{encode: encoder.encodeNull, decode: decoder.decodeNull} 76 77 case numberType: 78 c = codec{encode: encoder.encodeNumber, decode: decoder.decodeNumber} 79 80 case bytesType: 81 c = codec{encode: encoder.encodeBytes, decode: decoder.decodeBytes} 82 83 case durationType: 84 c = codec{encode: encoder.encodeDuration, decode: decoder.decodeDuration} 85 86 case timeType: 87 c = codec{encode: encoder.encodeTime, decode: decoder.decodeTime} 88 89 case interfaceType: 90 c = codec{encode: encoder.encodeInterface, decode: decoder.decodeInterface} 91 92 case rawMessageType: 93 c = codec{encode: encoder.encodeRawMessage, decode: decoder.decodeRawMessage} 94 95 case numberPtrType: 96 c = constructPointerCodec(numberPtrType, nil) 97 98 case durationPtrType: 99 c = constructPointerCodec(durationPtrType, nil) 100 101 case timePtrType: 102 c = constructPointerCodec(timePtrType, nil) 103 104 case rawMessagePtrType: 105 c = constructPointerCodec(rawMessagePtrType, nil) 106 } 107 108 if c.encode != nil { 109 return 110 } 111 112 switch t.Kind() { 113 case reflect.Bool: 114 c = codec{encode: encoder.encodeBool, decode: decoder.decodeBool} 115 116 case reflect.Int: 117 c = codec{encode: encoder.encodeInt, decode: decoder.decodeInt} 118 119 case reflect.Int8: 120 c = codec{encode: encoder.encodeInt8, decode: decoder.decodeInt8} 121 122 case reflect.Int16: 123 c = codec{encode: encoder.encodeInt16, decode: decoder.decodeInt16} 124 125 case reflect.Int32: 126 c = codec{encode: encoder.encodeInt32, decode: decoder.decodeInt32} 127 128 case reflect.Int64: 129 c = codec{encode: encoder.encodeInt64, decode: decoder.decodeInt64} 130 131 case reflect.Uint: 132 c = codec{encode: encoder.encodeUint, decode: decoder.decodeUint} 133 134 case reflect.Uintptr: 135 c = codec{encode: encoder.encodeUintptr, decode: decoder.decodeUintptr} 136 137 case reflect.Uint8: 138 c = codec{encode: encoder.encodeUint8, decode: decoder.decodeUint8} 139 140 case reflect.Uint16: 141 c = codec{encode: encoder.encodeUint16, decode: decoder.decodeUint16} 142 143 case reflect.Uint32: 144 c = codec{encode: encoder.encodeUint32, decode: decoder.decodeUint32} 145 146 case reflect.Uint64: 147 c = codec{encode: encoder.encodeUint64, decode: decoder.decodeUint64} 148 149 case reflect.Float32: 150 c = codec{encode: encoder.encodeFloat32, decode: decoder.decodeFloat32} 151 152 case reflect.Float64: 153 c = codec{encode: encoder.encodeFloat64, decode: decoder.decodeFloat64} 154 155 case reflect.String: 156 c = codec{encode: encoder.encodeString, decode: decoder.decodeString} 157 158 case reflect.Interface: 159 c = constructInterfaceCodec(t) 160 161 case reflect.Array: 162 c = constructArrayCodec(t, seen, canAddr) 163 164 case reflect.Slice: 165 c = constructSliceCodec(t, seen) 166 167 case reflect.Map: 168 c = constructMapCodec(t, seen) 169 170 case reflect.Struct: 171 c = constructStructCodec(t, seen, canAddr) 172 173 case reflect.Ptr: 174 c = constructPointerCodec(t, seen) 175 176 default: 177 c = constructUnsupportedTypeCodec(t) 178 } 179 180 p := reflect.PtrTo(t) 181 182 if canAddr { 183 switch { 184 case p.Implements(jsonMarshalerType): 185 c.encode = constructJSONMarshalerEncodeFunc(t, true) 186 case p.Implements(textMarshalerType): 187 c.encode = constructTextMarshalerEncodeFunc(t, true) 188 } 189 } 190 191 switch { 192 case t.Implements(jsonMarshalerType): 193 c.encode = constructJSONMarshalerEncodeFunc(t, false) 194 case t.Implements(textMarshalerType): 195 c.encode = constructTextMarshalerEncodeFunc(t, false) 196 } 197 198 switch { 199 case p.Implements(jsonUnmarshalerType): 200 c.decode = constructJSONUnmarshalerDecodeFunc(t, true) 201 case p.Implements(textUnmarshalerType): 202 c.decode = constructTextUnmarshalerDecodeFunc(t, true) 203 } 204 205 return 206} 207 208func constructStringCodec(t reflect.Type, seen map[reflect.Type]*structType, canAddr bool) codec { 209 c := constructCodec(t, seen, canAddr) 210 return codec{ 211 encode: constructStringEncodeFunc(c.encode), 212 decode: constructStringDecodeFunc(c.decode), 213 } 214} 215 216func constructStringEncodeFunc(encode encodeFunc) encodeFunc { 217 return func(e encoder, b []byte, p unsafe.Pointer) ([]byte, error) { 218 return e.encodeToString(b, p, encode) 219 } 220} 221 222func constructStringDecodeFunc(decode decodeFunc) decodeFunc { 223 return func(d decoder, b []byte, p unsafe.Pointer) ([]byte, error) { 224 return d.decodeFromString(b, p, decode) 225 } 226} 227 228func constructStringToIntDecodeFunc(t reflect.Type, decode decodeFunc) decodeFunc { 229 return func(d decoder, b []byte, p unsafe.Pointer) ([]byte, error) { 230 return d.decodeFromStringToInt(b, p, t, decode) 231 } 232} 233 234func constructArrayCodec(t reflect.Type, seen map[reflect.Type]*structType, canAddr bool) codec { 235 e := t.Elem() 236 c := constructCodec(e, seen, canAddr) 237 s := alignedSize(e) 238 return codec{ 239 encode: constructArrayEncodeFunc(s, t, c.encode), 240 decode: constructArrayDecodeFunc(s, t, c.decode), 241 } 242} 243 244func constructArrayEncodeFunc(size uintptr, t reflect.Type, encode encodeFunc) encodeFunc { 245 n := t.Len() 246 return func(e encoder, b []byte, p unsafe.Pointer) ([]byte, error) { 247 return e.encodeArray(b, p, n, size, t, encode) 248 } 249} 250 251func constructArrayDecodeFunc(size uintptr, t reflect.Type, decode decodeFunc) decodeFunc { 252 n := t.Len() 253 return func(d decoder, b []byte, p unsafe.Pointer) ([]byte, error) { 254 return d.decodeArray(b, p, n, size, t, decode) 255 } 256} 257 258func constructSliceCodec(t reflect.Type, seen map[reflect.Type]*structType) codec { 259 e := t.Elem() 260 s := alignedSize(e) 261 262 if e.Kind() == reflect.Uint8 { 263 // Go 1.7+ behavior: slices of byte types (and aliases) may override the 264 // default encoding and decoding behaviors by implementing marshaler and 265 // unmarshaler interfaces. 266 p := reflect.PtrTo(e) 267 c := codec{} 268 269 switch { 270 case e.Implements(jsonMarshalerType): 271 c.encode = constructJSONMarshalerEncodeFunc(e, false) 272 case e.Implements(textMarshalerType): 273 c.encode = constructTextMarshalerEncodeFunc(e, false) 274 case p.Implements(jsonMarshalerType): 275 c.encode = constructJSONMarshalerEncodeFunc(e, true) 276 case p.Implements(textMarshalerType): 277 c.encode = constructTextMarshalerEncodeFunc(e, true) 278 } 279 280 switch { 281 case e.Implements(jsonUnmarshalerType): 282 c.decode = constructJSONUnmarshalerDecodeFunc(e, false) 283 case e.Implements(textUnmarshalerType): 284 c.decode = constructTextUnmarshalerDecodeFunc(e, false) 285 case p.Implements(jsonUnmarshalerType): 286 c.decode = constructJSONUnmarshalerDecodeFunc(e, true) 287 case p.Implements(textUnmarshalerType): 288 c.decode = constructTextUnmarshalerDecodeFunc(e, true) 289 } 290 291 if c.encode != nil { 292 c.encode = constructSliceEncodeFunc(s, t, c.encode) 293 } else { 294 c.encode = encoder.encodeBytes 295 } 296 297 if c.decode != nil { 298 c.decode = constructSliceDecodeFunc(s, t, c.decode) 299 } else { 300 c.decode = decoder.decodeBytes 301 } 302 303 return c 304 } 305 306 c := constructCodec(e, seen, true) 307 return codec{ 308 encode: constructSliceEncodeFunc(s, t, c.encode), 309 decode: constructSliceDecodeFunc(s, t, c.decode), 310 } 311} 312 313func constructSliceEncodeFunc(size uintptr, t reflect.Type, encode encodeFunc) encodeFunc { 314 return func(e encoder, b []byte, p unsafe.Pointer) ([]byte, error) { 315 return e.encodeSlice(b, p, size, t, encode) 316 } 317} 318 319func constructSliceDecodeFunc(size uintptr, t reflect.Type, decode decodeFunc) decodeFunc { 320 return func(d decoder, b []byte, p unsafe.Pointer) ([]byte, error) { 321 return d.decodeSlice(b, p, size, t, decode) 322 } 323} 324 325func constructMapCodec(t reflect.Type, seen map[reflect.Type]*structType) codec { 326 var sortKeys sortFunc 327 k := t.Key() 328 v := t.Elem() 329 330 // Faster implementations for some common cases. 331 switch { 332 case k == stringType && v == interfaceType: 333 return codec{ 334 encode: encoder.encodeMapStringInterface, 335 decode: decoder.decodeMapStringInterface, 336 } 337 338 case k == stringType && v == rawMessageType: 339 return codec{ 340 encode: encoder.encodeMapStringRawMessage, 341 decode: decoder.decodeMapStringRawMessage, 342 } 343 344 case k == stringType && v == stringType: 345 return codec{ 346 encode: encoder.encodeMapStringString, 347 decode: decoder.decodeMapStringString, 348 } 349 350 case k == stringType && v == stringsType: 351 return codec{ 352 encode: encoder.encodeMapStringStringSlice, 353 decode: decoder.decodeMapStringStringSlice, 354 } 355 356 case k == stringType && v == boolType: 357 return codec{ 358 encode: encoder.encodeMapStringBool, 359 decode: decoder.decodeMapStringBool, 360 } 361 } 362 363 kc := codec{} 364 vc := constructCodec(v, seen, false) 365 366 if k.Implements(textMarshalerType) || reflect.PtrTo(k).Implements(textUnmarshalerType) { 367 kc.encode = constructTextMarshalerEncodeFunc(k, false) 368 kc.decode = constructTextUnmarshalerDecodeFunc(k, true) 369 370 sortKeys = func(keys []reflect.Value) { 371 sort.Slice(keys, func(i, j int) bool { 372 // This is a performance abomination but the use case is rare 373 // enough that it shouldn't be a problem in practice. 374 k1, _ := keys[i].Interface().(encoding.TextMarshaler).MarshalText() 375 k2, _ := keys[j].Interface().(encoding.TextMarshaler).MarshalText() 376 return string(k1) < string(k2) 377 }) 378 } 379 } else { 380 switch k.Kind() { 381 case reflect.String: 382 kc.encode = encoder.encodeString 383 kc.decode = decoder.decodeString 384 385 sortKeys = func(keys []reflect.Value) { 386 sort.Slice(keys, func(i, j int) bool { return keys[i].String() < keys[j].String() }) 387 } 388 389 case reflect.Int, 390 reflect.Int8, 391 reflect.Int16, 392 reflect.Int32, 393 reflect.Int64: 394 kc = constructStringCodec(k, seen, false) 395 396 sortKeys = func(keys []reflect.Value) { 397 sort.Slice(keys, func(i, j int) bool { return intStringsAreSorted(keys[i].Int(), keys[j].Int()) }) 398 } 399 400 case reflect.Uint, 401 reflect.Uintptr, 402 reflect.Uint8, 403 reflect.Uint16, 404 reflect.Uint32, 405 reflect.Uint64: 406 kc = constructStringCodec(k, seen, false) 407 408 sortKeys = func(keys []reflect.Value) { 409 sort.Slice(keys, func(i, j int) bool { return uintStringsAreSorted(keys[i].Uint(), keys[j].Uint()) }) 410 } 411 412 default: 413 return constructUnsupportedTypeCodec(t) 414 } 415 } 416 417 if inlined(v) { 418 vc.encode = constructInlineValueEncodeFunc(vc.encode) 419 } 420 421 return codec{ 422 encode: constructMapEncodeFunc(t, kc.encode, vc.encode, sortKeys), 423 decode: constructMapDecodeFunc(t, kc.decode, vc.decode), 424 } 425} 426 427func constructMapEncodeFunc(t reflect.Type, encodeKey, encodeValue encodeFunc, sortKeys sortFunc) encodeFunc { 428 return func(e encoder, b []byte, p unsafe.Pointer) ([]byte, error) { 429 return e.encodeMap(b, p, t, encodeKey, encodeValue, sortKeys) 430 } 431} 432 433func constructMapDecodeFunc(t reflect.Type, decodeKey, decodeValue decodeFunc) decodeFunc { 434 kt := t.Key() 435 vt := t.Elem() 436 kz := reflect.Zero(kt) 437 vz := reflect.Zero(vt) 438 return func(d decoder, b []byte, p unsafe.Pointer) ([]byte, error) { 439 return d.decodeMap(b, p, t, kt, vt, kz, vz, decodeKey, decodeValue) 440 } 441} 442 443func constructStructCodec(t reflect.Type, seen map[reflect.Type]*structType, canAddr bool) codec { 444 st := constructStructType(t, seen, canAddr) 445 return codec{ 446 encode: constructStructEncodeFunc(st), 447 decode: constructStructDecodeFunc(st), 448 } 449} 450 451func constructStructType(t reflect.Type, seen map[reflect.Type]*structType, canAddr bool) *structType { 452 // Used for preventing infinite recursion on types that have pointers to 453 // themselves. 454 st := seen[t] 455 456 if st == nil { 457 st = &structType{ 458 fields: make([]structField, 0, t.NumField()), 459 fieldsIndex: make(map[string]*structField), 460 ficaseIndex: make(map[string]*structField), 461 typ: t, 462 } 463 464 seen[t] = st 465 st.fields = appendStructFields(st.fields, t, 0, seen, canAddr) 466 467 for i := range st.fields { 468 f := &st.fields[i] 469 s := strings.ToLower(f.name) 470 st.fieldsIndex[f.name] = f 471 // When there is ambiguity because multiple fields have the same 472 // case-insensitive representation, the first field must win. 473 if _, exists := st.ficaseIndex[s]; !exists { 474 st.ficaseIndex[s] = f 475 } 476 } 477 } 478 479 return st 480} 481 482func constructStructEncodeFunc(st *structType) encodeFunc { 483 return func(e encoder, b []byte, p unsafe.Pointer) ([]byte, error) { 484 return e.encodeStruct(b, p, st) 485 } 486} 487 488func constructStructDecodeFunc(st *structType) decodeFunc { 489 return func(d decoder, b []byte, p unsafe.Pointer) ([]byte, error) { 490 return d.decodeStruct(b, p, st) 491 } 492} 493 494func constructEmbeddedStructPointerCodec(t reflect.Type, unexported bool, offset uintptr, field codec) codec { 495 return codec{ 496 encode: constructEmbeddedStructPointerEncodeFunc(t, unexported, offset, field.encode), 497 decode: constructEmbeddedStructPointerDecodeFunc(t, unexported, offset, field.decode), 498 } 499} 500 501func constructEmbeddedStructPointerEncodeFunc(t reflect.Type, unexported bool, offset uintptr, encode encodeFunc) encodeFunc { 502 return func(e encoder, b []byte, p unsafe.Pointer) ([]byte, error) { 503 return e.encodeEmbeddedStructPointer(b, p, t, unexported, offset, encode) 504 } 505} 506 507func constructEmbeddedStructPointerDecodeFunc(t reflect.Type, unexported bool, offset uintptr, decode decodeFunc) decodeFunc { 508 return func(d decoder, b []byte, p unsafe.Pointer) ([]byte, error) { 509 return d.decodeEmbeddedStructPointer(b, p, t, unexported, offset, decode) 510 } 511} 512 513func appendStructFields(fields []structField, t reflect.Type, offset uintptr, seen map[reflect.Type]*structType, canAddr bool) []structField { 514 type embeddedField struct { 515 index int 516 offset uintptr 517 pointer bool 518 unexported bool 519 subtype *structType 520 subfield *structField 521 } 522 523 names := make(map[string]struct{}) 524 embedded := make([]embeddedField, 0, 10) 525 526 for i, n := 0, t.NumField(); i < n; i++ { 527 f := t.Field(i) 528 529 var ( 530 name = f.Name 531 anonymous = f.Anonymous 532 tag = false 533 omitempty = false 534 stringify = false 535 unexported = len(f.PkgPath) != 0 536 ) 537 538 if unexported && !anonymous { // unexported 539 continue 540 } 541 542 if parts := strings.Split(f.Tag.Get("json"), ","); len(parts) != 0 { 543 if len(parts[0]) != 0 { 544 name, tag = parts[0], true 545 } 546 547 if name == "-" && len(parts) == 1 { // ignored 548 continue 549 } 550 551 if !isValidTag(name) { 552 name = f.Name 553 } 554 555 for _, tag := range parts[1:] { 556 switch tag { 557 case "omitempty": 558 omitempty = true 559 case "string": 560 stringify = true 561 } 562 } 563 } 564 565 if anonymous && !tag { // embedded 566 typ := f.Type 567 ptr := f.Type.Kind() == reflect.Ptr 568 569 if ptr { 570 typ = f.Type.Elem() 571 } 572 573 if typ.Kind() == reflect.Struct { 574 // When the embedded fields is inlined the fields can be looked 575 // up by offset from the address of the wrapping object, so we 576 // simply add the embedded struct fields to the list of fields 577 // of the current struct type. 578 subtype := constructStructType(typ, seen, canAddr) 579 580 for j := range subtype.fields { 581 embedded = append(embedded, embeddedField{ 582 index: i<<32 | j, 583 offset: offset + f.Offset, 584 pointer: ptr, 585 unexported: unexported, 586 subtype: subtype, 587 subfield: &subtype.fields[j], 588 }) 589 } 590 591 continue 592 } 593 594 if unexported { // ignore unexported non-struct types 595 continue 596 } 597 } 598 599 codec := constructCodec(f.Type, seen, canAddr) 600 601 if stringify { 602 // https://golang.org/pkg/encoding/json/#Marshal 603 // 604 // The "string" option signals that a field is stored as JSON inside 605 // a JSON-encoded string. It applies only to fields of string, 606 // floating point, integer, or boolean types. This extra level of 607 // encoding is sometimes used when communicating with JavaScript 608 // programs: 609 typ := f.Type 610 611 if typ.Kind() == reflect.Ptr { 612 typ = typ.Elem() 613 } 614 615 switch typ.Kind() { 616 case reflect.Int, 617 reflect.Int8, 618 reflect.Int16, 619 reflect.Int32, 620 reflect.Int64, 621 reflect.Uint, 622 reflect.Uintptr, 623 reflect.Uint8, 624 reflect.Uint16, 625 reflect.Uint32, 626 reflect.Uint64: 627 codec.encode = constructStringEncodeFunc(codec.encode) 628 codec.decode = constructStringToIntDecodeFunc(typ, codec.decode) 629 case reflect.Bool, 630 reflect.Float32, 631 reflect.Float64, 632 reflect.String: 633 codec.encode = constructStringEncodeFunc(codec.encode) 634 codec.decode = constructStringDecodeFunc(codec.decode) 635 } 636 } 637 638 fields = append(fields, structField{ 639 codec: codec, 640 offset: offset + f.Offset, 641 empty: emptyFuncOf(f.Type), 642 tag: tag, 643 omitempty: omitempty, 644 name: name, 645 index: i << 32, 646 typ: f.Type, 647 zero: reflect.Zero(f.Type), 648 }) 649 650 names[name] = struct{}{} 651 } 652 653 // Only unambiguous embedded fields must be serialized. 654 ambiguousNames := make(map[string]int) 655 ambiguousTags := make(map[string]int) 656 657 // Embedded types can never override a field that was already present at 658 // the top-level. 659 for name := range names { 660 ambiguousNames[name]++ 661 ambiguousTags[name]++ 662 } 663 664 for _, embfield := range embedded { 665 ambiguousNames[embfield.subfield.name]++ 666 if embfield.subfield.tag { 667 ambiguousTags[embfield.subfield.name]++ 668 } 669 } 670 671 for _, embfield := range embedded { 672 subfield := *embfield.subfield 673 674 if ambiguousNames[subfield.name] > 1 && !(subfield.tag && ambiguousTags[subfield.name] == 1) { 675 continue // ambiguous embedded field 676 } 677 678 if embfield.pointer { 679 subfield.codec = constructEmbeddedStructPointerCodec(embfield.subtype.typ, embfield.unexported, subfield.offset, subfield.codec) 680 subfield.offset = embfield.offset 681 } else { 682 subfield.offset += embfield.offset 683 } 684 685 // To prevent dominant flags more than one level below the embedded one. 686 subfield.tag = false 687 688 // To ensure the order of the fields in the output is the same is in the 689 // struct type. 690 subfield.index = embfield.index 691 692 fields = append(fields, subfield) 693 } 694 695 for i := range fields { 696 name := fields[i].name 697 fields[i].json = encodeKeyFragment(name, 0) 698 fields[i].html = encodeKeyFragment(name, EscapeHTML) 699 } 700 701 sort.Slice(fields, func(i, j int) bool { return fields[i].index < fields[j].index }) 702 return fields 703} 704 705func encodeKeyFragment(s string, flags AppendFlags) string { 706 b := make([]byte, 1, len(s)+4) 707 b[0] = ',' 708 e := encoder{flags: flags} 709 b, _ = e.encodeString(b, unsafe.Pointer(&s)) 710 b = append(b, ':') 711 return *(*string)(unsafe.Pointer(&b)) 712} 713 714func constructPointerCodec(t reflect.Type, seen map[reflect.Type]*structType) codec { 715 e := t.Elem() 716 c := constructCodec(e, seen, true) 717 return codec{ 718 encode: constructPointerEncodeFunc(e, c.encode), 719 decode: constructPointerDecodeFunc(e, c.decode), 720 } 721} 722 723func constructPointerEncodeFunc(t reflect.Type, encode encodeFunc) encodeFunc { 724 return func(e encoder, b []byte, p unsafe.Pointer) ([]byte, error) { 725 return e.encodePointer(b, p, t, encode) 726 } 727} 728 729func constructPointerDecodeFunc(t reflect.Type, decode decodeFunc) decodeFunc { 730 return func(d decoder, b []byte, p unsafe.Pointer) ([]byte, error) { 731 return d.decodePointer(b, p, t, decode) 732 } 733} 734 735func constructInterfaceCodec(t reflect.Type) codec { 736 return codec{ 737 encode: constructMaybeEmptyInterfaceEncoderFunc(t), 738 decode: constructMaybeEmptyInterfaceDecoderFunc(t), 739 } 740} 741 742func constructMaybeEmptyInterfaceEncoderFunc(t reflect.Type) encodeFunc { 743 return func(e encoder, b []byte, p unsafe.Pointer) ([]byte, error) { 744 return e.encodeMaybeEmptyInterface(b, p, t) 745 } 746} 747 748func constructMaybeEmptyInterfaceDecoderFunc(t reflect.Type) decodeFunc { 749 return func(d decoder, b []byte, p unsafe.Pointer) ([]byte, error) { 750 return d.decodeMaybeEmptyInterface(b, p, t) 751 } 752} 753 754func constructUnsupportedTypeCodec(t reflect.Type) codec { 755 return codec{ 756 encode: constructUnsupportedTypeEncodeFunc(t), 757 decode: constructUnsupportedTypeDecodeFunc(t), 758 } 759} 760 761func constructUnsupportedTypeEncodeFunc(t reflect.Type) encodeFunc { 762 return func(e encoder, b []byte, p unsafe.Pointer) ([]byte, error) { 763 return e.encodeUnsupportedTypeError(b, p, t) 764 } 765} 766 767func constructUnsupportedTypeDecodeFunc(t reflect.Type) decodeFunc { 768 return func(d decoder, b []byte, p unsafe.Pointer) ([]byte, error) { 769 return d.decodeUnmarshalTypeError(b, p, t) 770 } 771} 772 773func constructJSONMarshalerEncodeFunc(t reflect.Type, pointer bool) encodeFunc { 774 return func(e encoder, b []byte, p unsafe.Pointer) ([]byte, error) { 775 return e.encodeJSONMarshaler(b, p, t, pointer) 776 } 777} 778 779func constructJSONUnmarshalerDecodeFunc(t reflect.Type, pointer bool) decodeFunc { 780 return func(d decoder, b []byte, p unsafe.Pointer) ([]byte, error) { 781 return d.decodeJSONUnmarshaler(b, p, t, pointer) 782 } 783} 784 785func constructTextMarshalerEncodeFunc(t reflect.Type, pointer bool) encodeFunc { 786 return func(e encoder, b []byte, p unsafe.Pointer) ([]byte, error) { 787 return e.encodeTextMarshaler(b, p, t, pointer) 788 } 789} 790 791func constructTextUnmarshalerDecodeFunc(t reflect.Type, pointer bool) decodeFunc { 792 return func(d decoder, b []byte, p unsafe.Pointer) ([]byte, error) { 793 return d.decodeTextUnmarshaler(b, p, t, pointer) 794 } 795} 796 797func constructInlineValueEncodeFunc(encode encodeFunc) encodeFunc { 798 return func(e encoder, b []byte, p unsafe.Pointer) ([]byte, error) { 799 return encode(e, b, noescape(unsafe.Pointer(&p))) 800 } 801} 802 803// noescape hides a pointer from escape analysis. noescape is 804// the identity function but escape analysis doesn't think the 805// output depends on the input. noescape is inlined and currently 806// compiles down to zero instructions. 807// USE CAREFULLY! 808// This was copied from the runtime; see issues 23382 and 7921. 809//go:nosplit 810func noescape(p unsafe.Pointer) unsafe.Pointer { 811 x := uintptr(p) 812 return unsafe.Pointer(x ^ 0) 813} 814 815func alignedSize(t reflect.Type) uintptr { 816 a := t.Align() 817 s := t.Size() 818 return align(uintptr(a), uintptr(s)) 819} 820 821func align(align, size uintptr) uintptr { 822 if align != 0 && (size%align) != 0 { 823 size = ((size / align) + 1) * align 824 } 825 return size 826} 827 828func inlined(t reflect.Type) bool { 829 switch t.Kind() { 830 case reflect.Ptr: 831 return true 832 case reflect.Map: 833 return true 834 case reflect.Struct: 835 return t.NumField() == 1 && inlined(t.Field(0).Type) 836 default: 837 return false 838 } 839} 840 841func isValidTag(s string) bool { 842 if s == "" { 843 return false 844 } 845 for _, c := range s { 846 switch { 847 case strings.ContainsRune("!#$%&()*+-./:;<=>?@[]^_{|}~ ", c): 848 // Backslash and quote chars are reserved, but 849 // otherwise any punctuation chars are allowed 850 // in a tag name. 851 default: 852 if !unicode.IsLetter(c) && !unicode.IsDigit(c) { 853 return false 854 } 855 } 856 } 857 return true 858} 859 860func emptyFuncOf(t reflect.Type) emptyFunc { 861 switch t { 862 case bytesType, rawMessageType: 863 return func(p unsafe.Pointer) bool { return (*slice)(p).len == 0 } 864 } 865 866 switch t.Kind() { 867 case reflect.Array: 868 if t.Len() == 0 { 869 return func(unsafe.Pointer) bool { return true } 870 } 871 872 case reflect.Map: 873 return func(p unsafe.Pointer) bool { return reflect.NewAt(t, p).Elem().Len() == 0 } 874 875 case reflect.Slice: 876 return func(p unsafe.Pointer) bool { return (*slice)(p).len == 0 } 877 878 case reflect.String: 879 return func(p unsafe.Pointer) bool { return len(*(*string)(p)) == 0 } 880 881 case reflect.Bool: 882 return func(p unsafe.Pointer) bool { return !*(*bool)(p) } 883 884 case reflect.Int, reflect.Uint: 885 return func(p unsafe.Pointer) bool { return *(*uint)(p) == 0 } 886 887 case reflect.Uintptr: 888 return func(p unsafe.Pointer) bool { return *(*uintptr)(p) == 0 } 889 890 case reflect.Int8, reflect.Uint8: 891 return func(p unsafe.Pointer) bool { return *(*uint8)(p) == 0 } 892 893 case reflect.Int16, reflect.Uint16: 894 return func(p unsafe.Pointer) bool { return *(*uint16)(p) == 0 } 895 896 case reflect.Int32, reflect.Uint32: 897 return func(p unsafe.Pointer) bool { return *(*uint32)(p) == 0 } 898 899 case reflect.Int64, reflect.Uint64: 900 return func(p unsafe.Pointer) bool { return *(*uint64)(p) == 0 } 901 902 case reflect.Float32: 903 return func(p unsafe.Pointer) bool { return *(*float32)(p) == 0 } 904 905 case reflect.Float64: 906 return func(p unsafe.Pointer) bool { return *(*float64)(p) == 0 } 907 908 case reflect.Ptr: 909 return func(p unsafe.Pointer) bool { return *(*unsafe.Pointer)(p) == nil } 910 911 case reflect.Interface: 912 return func(p unsafe.Pointer) bool { return (*iface)(p).ptr == nil } 913 } 914 915 return func(unsafe.Pointer) bool { return false } 916} 917 918type iface struct { 919 typ unsafe.Pointer 920 ptr unsafe.Pointer 921} 922 923type slice struct { 924 data unsafe.Pointer 925 len int 926 cap int 927} 928 929type structType struct { 930 fields []structField 931 fieldsIndex map[string]*structField 932 ficaseIndex map[string]*structField 933 typ reflect.Type 934 inlined bool 935} 936 937type structField struct { 938 codec codec 939 offset uintptr 940 empty emptyFunc 941 tag bool 942 omitempty bool 943 json string 944 html string 945 name string 946 typ reflect.Type 947 zero reflect.Value 948 index int 949} 950 951func unmarshalTypeError(b []byte, t reflect.Type) error { 952 return &UnmarshalTypeError{Value: strconv.Quote(prefix(b)), Type: t} 953} 954 955func unmarshalOverflow(b []byte, t reflect.Type) error { 956 return &UnmarshalTypeError{Value: "number " + prefix(b) + " overflows", Type: t} 957} 958 959func unexpectedEOF(b []byte) error { 960 return syntaxError(b, "unexpected end of JSON input") 961} 962 963var syntaxErrorMsgOffset = ^uintptr(0) 964 965func init() { 966 t := reflect.TypeOf(SyntaxError{}) 967 for i, n := 0, t.NumField(); i < n; i++ { 968 if f := t.Field(i); f.Type.Kind() == reflect.String { 969 syntaxErrorMsgOffset = f.Offset 970 } 971 } 972} 973 974func syntaxError(b []byte, msg string, args ...interface{}) error { 975 e := new(SyntaxError) 976 i := syntaxErrorMsgOffset 977 if i != ^uintptr(0) { 978 s := "json: " + fmt.Sprintf(msg, args...) + ": " + prefix(b) 979 p := unsafe.Pointer(e) 980 // Hack to set the unexported `msg` field. 981 *(*string)(unsafe.Pointer(uintptr(p) + i)) = s 982 } 983 return e 984} 985 986func objectKeyError(b []byte, err error) ([]byte, error) { 987 if len(b) == 0 { 988 return nil, unexpectedEOF(b) 989 } 990 switch err.(type) { 991 case *UnmarshalTypeError: 992 err = syntaxError(b, "invalid character '%c' looking for beginning of object key", b[0]) 993 } 994 return b, err 995} 996 997func prefix(b []byte) string { 998 if len(b) < 32 { 999 return string(b) 1000 } 1001 return string(b[:32]) + "..." 1002} 1003 1004func intStringsAreSorted(i0, i1 int64) bool { 1005 var b0, b1 [32]byte 1006 return string(strconv.AppendInt(b0[:0], i0, 10)) < string(strconv.AppendInt(b1[:0], i1, 10)) 1007} 1008 1009func uintStringsAreSorted(u0, u1 uint64) bool { 1010 var b0, b1 [32]byte 1011 return string(strconv.AppendUint(b0[:0], u0, 10)) < string(strconv.AppendUint(b1[:0], u1, 10)) 1012} 1013 1014func stringToBytes(s string) []byte { 1015 return *(*[]byte)(unsafe.Pointer(&sliceHeader{ 1016 Data: *(*unsafe.Pointer)(unsafe.Pointer(&s)), 1017 Len: len(s), 1018 Cap: len(s), 1019 })) 1020} 1021 1022type sliceHeader struct { 1023 Data unsafe.Pointer 1024 Len int 1025 Cap int 1026} 1027 1028var ( 1029 nullType = reflect.TypeOf(nil) 1030 boolType = reflect.TypeOf(false) 1031 1032 intType = reflect.TypeOf(int(0)) 1033 int8Type = reflect.TypeOf(int8(0)) 1034 int16Type = reflect.TypeOf(int16(0)) 1035 int32Type = reflect.TypeOf(int32(0)) 1036 int64Type = reflect.TypeOf(int64(0)) 1037 1038 uintType = reflect.TypeOf(uint(0)) 1039 uint8Type = reflect.TypeOf(uint8(0)) 1040 uint16Type = reflect.TypeOf(uint16(0)) 1041 uint32Type = reflect.TypeOf(uint32(0)) 1042 uint64Type = reflect.TypeOf(uint64(0)) 1043 uintptrType = reflect.TypeOf(uintptr(0)) 1044 1045 float32Type = reflect.TypeOf(float32(0)) 1046 float64Type = reflect.TypeOf(float64(0)) 1047 1048 numberType = reflect.TypeOf(json.Number("")) 1049 stringType = reflect.TypeOf("") 1050 stringsType = reflect.TypeOf([]string(nil)) 1051 bytesType = reflect.TypeOf(([]byte)(nil)) 1052 durationType = reflect.TypeOf(time.Duration(0)) 1053 timeType = reflect.TypeOf(time.Time{}) 1054 rawMessageType = reflect.TypeOf(RawMessage(nil)) 1055 1056 numberPtrType = reflect.PtrTo(numberType) 1057 durationPtrType = reflect.PtrTo(durationType) 1058 timePtrType = reflect.PtrTo(timeType) 1059 rawMessagePtrType = reflect.PtrTo(rawMessageType) 1060 1061 sliceInterfaceType = reflect.TypeOf(([]interface{})(nil)) 1062 sliceStringType = reflect.TypeOf(([]interface{})(nil)) 1063 mapStringInterfaceType = reflect.TypeOf((map[string]interface{})(nil)) 1064 mapStringRawMessageType = reflect.TypeOf((map[string]RawMessage)(nil)) 1065 mapStringStringType = reflect.TypeOf((map[string]string)(nil)) 1066 mapStringStringSliceType = reflect.TypeOf((map[string][]string)(nil)) 1067 mapStringBoolType = reflect.TypeOf((map[string]bool)(nil)) 1068 1069 interfaceType = reflect.TypeOf((*interface{})(nil)).Elem() 1070 jsonMarshalerType = reflect.TypeOf((*Marshaler)(nil)).Elem() 1071 jsonUnmarshalerType = reflect.TypeOf((*Unmarshaler)(nil)).Elem() 1072 textMarshalerType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem() 1073 textUnmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem() 1074) 1075 1076// ============================================================================= 1077// Copyright 2009 The Go Authors. All rights reserved. 1078// Use of this source code is governed by a BSD-style 1079// license that can be found in the LICENSE file. 1080 1081// appendDuration appends a human-readable representation of d to b. 1082// 1083// The function copies the implementation of time.Duration.String but prevents 1084// Go from making a dynamic memory allocation on the returned value. 1085func appendDuration(b []byte, d time.Duration) []byte { 1086 // Largest time is 2540400h10m10.000000000s 1087 var buf [32]byte 1088 w := len(buf) 1089 1090 u := uint64(d) 1091 neg := d < 0 1092 if neg { 1093 u = -u 1094 } 1095 1096 if u < uint64(time.Second) { 1097 // Special case: if duration is smaller than a second, 1098 // use smaller units, like 1.2ms 1099 var prec int 1100 w-- 1101 buf[w] = 's' 1102 w-- 1103 switch { 1104 case u == 0: 1105 return append(b, '0', 's') 1106 case u < uint64(time.Microsecond): 1107 // print nanoseconds 1108 prec = 0 1109 buf[w] = 'n' 1110 case u < uint64(time.Millisecond): 1111 // print microseconds 1112 prec = 3 1113 // U+00B5 'µ' micro sign == 0xC2 0xB5 1114 w-- // Need room for two bytes. 1115 copy(buf[w:], "µ") 1116 default: 1117 // print milliseconds 1118 prec = 6 1119 buf[w] = 'm' 1120 } 1121 w, u = fmtFrac(buf[:w], u, prec) 1122 w = fmtInt(buf[:w], u) 1123 } else { 1124 w-- 1125 buf[w] = 's' 1126 1127 w, u = fmtFrac(buf[:w], u, 9) 1128 1129 // u is now integer seconds 1130 w = fmtInt(buf[:w], u%60) 1131 u /= 60 1132 1133 // u is now integer minutes 1134 if u > 0 { 1135 w-- 1136 buf[w] = 'm' 1137 w = fmtInt(buf[:w], u%60) 1138 u /= 60 1139 1140 // u is now integer hours 1141 // Stop at hours because days can be different lengths. 1142 if u > 0 { 1143 w-- 1144 buf[w] = 'h' 1145 w = fmtInt(buf[:w], u) 1146 } 1147 } 1148 } 1149 1150 if neg { 1151 w-- 1152 buf[w] = '-' 1153 } 1154 1155 return append(b, buf[w:]...) 1156} 1157 1158// fmtFrac formats the fraction of v/10**prec (e.g., ".12345") into the 1159// tail of buf, omitting trailing zeros. it omits the decimal 1160// point too when the fraction is 0. It returns the index where the 1161// output bytes begin and the value v/10**prec. 1162func fmtFrac(buf []byte, v uint64, prec int) (nw int, nv uint64) { 1163 // Omit trailing zeros up to and including decimal point. 1164 w := len(buf) 1165 print := false 1166 for i := 0; i < prec; i++ { 1167 digit := v % 10 1168 print = print || digit != 0 1169 if print { 1170 w-- 1171 buf[w] = byte(digit) + '0' 1172 } 1173 v /= 10 1174 } 1175 if print { 1176 w-- 1177 buf[w] = '.' 1178 } 1179 return w, v 1180} 1181 1182// fmtInt formats v into the tail of buf. 1183// It returns the index where the output begins. 1184func fmtInt(buf []byte, v uint64) int { 1185 w := len(buf) 1186 if v == 0 { 1187 w-- 1188 buf[w] = '0' 1189 } else { 1190 for v > 0 { 1191 w-- 1192 buf[w] = byte(v%10) + '0' 1193 v /= 10 1194 } 1195 } 1196 return w 1197} 1198 1199// ============================================================================= 1200