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