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