1
2
3
4
5
6
7
8 package json
9
10 import (
11 "encoding"
12 "encoding/base64"
13 "fmt"
14 "reflect"
15 "strconv"
16 "strings"
17 "unicode"
18 "unicode/utf16"
19 "unicode/utf8"
20 _ "unsafe"
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 func Unmarshal(data []byte, v any) error {
98
99
100
101 var d decodeState
102 err := checkValid(data, &d.scan)
103 if err != nil {
104 return err
105 }
106
107 d.init(data)
108 return d.unmarshal(v)
109 }
110
111
112
113
114
115
116
117
118
119 type Unmarshaler interface {
120 UnmarshalJSON([]byte) error
121 }
122
123
124
125 type UnmarshalTypeError struct {
126 Value string
127 Type reflect.Type
128 Offset int64
129 Struct string
130 Field string
131 }
132
133 func (e *UnmarshalTypeError) Error() string {
134 if e.Struct != "" || e.Field != "" {
135 return "json: cannot unmarshal " + e.Value + " into Go struct field " + e.Struct + "." + e.Field + " of type " + e.Type.String()
136 }
137 return "json: cannot unmarshal " + e.Value + " into Go value of type " + e.Type.String()
138 }
139
140
141
142
143
144 type UnmarshalFieldError struct {
145 Key string
146 Type reflect.Type
147 Field reflect.StructField
148 }
149
150 func (e *UnmarshalFieldError) Error() string {
151 return "json: cannot unmarshal object key " + strconv.Quote(e.Key) + " into unexported field " + e.Field.Name + " of type " + e.Type.String()
152 }
153
154
155
156 type InvalidUnmarshalError struct {
157 Type reflect.Type
158 }
159
160 func (e *InvalidUnmarshalError) Error() string {
161 if e.Type == nil {
162 return "json: Unmarshal(nil)"
163 }
164
165 if e.Type.Kind() != reflect.Pointer {
166 return "json: Unmarshal(non-pointer " + e.Type.String() + ")"
167 }
168 return "json: Unmarshal(nil " + e.Type.String() + ")"
169 }
170
171 func (d *decodeState) unmarshal(v any) error {
172 rv := reflect.ValueOf(v)
173 if rv.Kind() != reflect.Pointer || rv.IsNil() {
174 return &InvalidUnmarshalError{reflect.TypeOf(v)}
175 }
176
177 d.scan.reset()
178 d.scanWhile(scanSkipSpace)
179
180
181 err := d.value(rv)
182 if err != nil {
183 return d.addErrorContext(err)
184 }
185 return d.savedError
186 }
187
188
189 type Number string
190
191
192 func (n Number) String() string { return string(n) }
193
194
195 func (n Number) Float64() (float64, error) {
196 return strconv.ParseFloat(string(n), 64)
197 }
198
199
200 func (n Number) Int64() (int64, error) {
201 return strconv.ParseInt(string(n), 10, 64)
202 }
203
204
205 type errorContext struct {
206 Struct reflect.Type
207 FieldStack []string
208 }
209
210
211 type decodeState struct {
212 data []byte
213 off int
214 opcode int
215 scan scanner
216 errorContext *errorContext
217 savedError error
218 useNumber bool
219 disallowUnknownFields bool
220 }
221
222
223 func (d *decodeState) readIndex() int {
224 return d.off - 1
225 }
226
227
228
229
230 const phasePanicMsg = "JSON decoder out of sync - data changing underfoot?"
231
232 func (d *decodeState) init(data []byte) *decodeState {
233 d.data = data
234 d.off = 0
235 d.savedError = nil
236 if d.errorContext != nil {
237 d.errorContext.Struct = nil
238
239 d.errorContext.FieldStack = d.errorContext.FieldStack[:0]
240 }
241 return d
242 }
243
244
245
246 func (d *decodeState) saveError(err error) {
247 if d.savedError == nil {
248 d.savedError = d.addErrorContext(err)
249 }
250 }
251
252
253 func (d *decodeState) addErrorContext(err error) error {
254 if d.errorContext != nil && (d.errorContext.Struct != nil || len(d.errorContext.FieldStack) > 0) {
255 switch err := err.(type) {
256 case *UnmarshalTypeError:
257 err.Struct = d.errorContext.Struct.Name()
258 err.Field = strings.Join(d.errorContext.FieldStack, ".")
259 }
260 }
261 return err
262 }
263
264
265 func (d *decodeState) skip() {
266 s, data, i := &d.scan, d.data, d.off
267 depth := len(s.parseState)
268 for {
269 op := s.step(s, data[i])
270 i++
271 if len(s.parseState) < depth {
272 d.off = i
273 d.opcode = op
274 return
275 }
276 }
277 }
278
279
280 func (d *decodeState) scanNext() {
281 if d.off < len(d.data) {
282 d.opcode = d.scan.step(&d.scan, d.data[d.off])
283 d.off++
284 } else {
285 d.opcode = d.scan.eof()
286 d.off = len(d.data) + 1
287 }
288 }
289
290
291
292 func (d *decodeState) scanWhile(op int) {
293 s, data, i := &d.scan, d.data, d.off
294 for i < len(data) {
295 newOp := s.step(s, data[i])
296 i++
297 if newOp != op {
298 d.opcode = newOp
299 d.off = i
300 return
301 }
302 }
303
304 d.off = len(data) + 1
305 d.opcode = d.scan.eof()
306 }
307
308
309
310
311
312
313
314
315
316 func (d *decodeState) rescanLiteral() {
317 data, i := d.data, d.off
318 Switch:
319 switch data[i-1] {
320 case '"':
321 for ; i < len(data); i++ {
322 switch data[i] {
323 case '\\':
324 i++
325 case '"':
326 i++
327 break Switch
328 }
329 }
330 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-':
331 for ; i < len(data); i++ {
332 switch data[i] {
333 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
334 '.', 'e', 'E', '+', '-':
335 default:
336 break Switch
337 }
338 }
339 case 't':
340 i += len("rue")
341 case 'f':
342 i += len("alse")
343 case 'n':
344 i += len("ull")
345 }
346 if i < len(data) {
347 d.opcode = stateEndValue(&d.scan, data[i])
348 } else {
349 d.opcode = scanEnd
350 }
351 d.off = i + 1
352 }
353
354
355
356
357 func (d *decodeState) value(v reflect.Value) error {
358 switch d.opcode {
359 default:
360 panic(phasePanicMsg)
361
362 case scanBeginArray:
363 if v.IsValid() {
364 if err := d.array(v); err != nil {
365 return err
366 }
367 } else {
368 d.skip()
369 }
370 d.scanNext()
371
372 case scanBeginObject:
373 if v.IsValid() {
374 if err := d.object(v); err != nil {
375 return err
376 }
377 } else {
378 d.skip()
379 }
380 d.scanNext()
381
382 case scanBeginLiteral:
383
384 start := d.readIndex()
385 d.rescanLiteral()
386
387 if v.IsValid() {
388 if err := d.literalStore(d.data[start:d.readIndex()], v, false); err != nil {
389 return err
390 }
391 }
392 }
393 return nil
394 }
395
396 type unquotedValue struct{}
397
398
399
400
401
402 func (d *decodeState) valueQuoted() any {
403 switch d.opcode {
404 default:
405 panic(phasePanicMsg)
406
407 case scanBeginArray, scanBeginObject:
408 d.skip()
409 d.scanNext()
410
411 case scanBeginLiteral:
412 v := d.literalInterface()
413 switch v.(type) {
414 case nil, string:
415 return v
416 }
417 }
418 return unquotedValue{}
419 }
420
421
422
423
424
425
426 func indirect(v reflect.Value, decodingNull bool) (Unmarshaler, encoding.TextUnmarshaler, reflect.Value) {
427
428
429
430
431
432
433
434
435
436
437
438 v0 := v
439 haveAddr := false
440
441
442
443
444 if v.Kind() != reflect.Pointer && v.Type().Name() != "" && v.CanAddr() {
445 haveAddr = true
446 v = v.Addr()
447 }
448 for {
449
450
451 if v.Kind() == reflect.Interface && !v.IsNil() {
452 e := v.Elem()
453 if e.Kind() == reflect.Pointer && !e.IsNil() && (!decodingNull || e.Elem().Kind() == reflect.Pointer) {
454 haveAddr = false
455 v = e
456 continue
457 }
458 }
459
460 if v.Kind() != reflect.Pointer {
461 break
462 }
463
464 if decodingNull && v.CanSet() {
465 break
466 }
467
468
469
470
471 if v.Elem().Kind() == reflect.Interface && v.Elem().Elem() == v {
472 v = v.Elem()
473 break
474 }
475 if v.IsNil() {
476 v.Set(reflect.New(v.Type().Elem()))
477 }
478 if v.Type().NumMethod() > 0 && v.CanInterface() {
479 if u, ok := v.Interface().(Unmarshaler); ok {
480 return u, nil, reflect.Value{}
481 }
482 if !decodingNull {
483 if u, ok := v.Interface().(encoding.TextUnmarshaler); ok {
484 return nil, u, reflect.Value{}
485 }
486 }
487 }
488
489 if haveAddr {
490 v = v0
491 haveAddr = false
492 } else {
493 v = v.Elem()
494 }
495 }
496 return nil, nil, v
497 }
498
499
500
501 func (d *decodeState) array(v reflect.Value) error {
502
503 u, ut, pv := indirect(v, false)
504 if u != nil {
505 start := d.readIndex()
506 d.skip()
507 return u.UnmarshalJSON(d.data[start:d.off])
508 }
509 if ut != nil {
510 d.saveError(&UnmarshalTypeError{Value: "array", Type: v.Type(), Offset: int64(d.off)})
511 d.skip()
512 return nil
513 }
514 v = pv
515
516
517 switch v.Kind() {
518 case reflect.Interface:
519 if v.NumMethod() == 0 {
520
521 ai := d.arrayInterface()
522 v.Set(reflect.ValueOf(ai))
523 return nil
524 }
525
526 fallthrough
527 default:
528 d.saveError(&UnmarshalTypeError{Value: "array", Type: v.Type(), Offset: int64(d.off)})
529 d.skip()
530 return nil
531 case reflect.Array, reflect.Slice:
532 break
533 }
534
535 i := 0
536 for {
537
538 d.scanWhile(scanSkipSpace)
539 if d.opcode == scanEndArray {
540 break
541 }
542
543
544 if v.Kind() == reflect.Slice {
545 if i >= v.Cap() {
546 v.Grow(1)
547 }
548 if i >= v.Len() {
549 v.SetLen(i + 1)
550 }
551 }
552
553 if i < v.Len() {
554
555 if err := d.value(v.Index(i)); err != nil {
556 return err
557 }
558 } else {
559
560 if err := d.value(reflect.Value{}); err != nil {
561 return err
562 }
563 }
564 i++
565
566
567 if d.opcode == scanSkipSpace {
568 d.scanWhile(scanSkipSpace)
569 }
570 if d.opcode == scanEndArray {
571 break
572 }
573 if d.opcode != scanArrayValue {
574 panic(phasePanicMsg)
575 }
576 }
577
578 if i < v.Len() {
579 if v.Kind() == reflect.Array {
580 for ; i < v.Len(); i++ {
581 v.Index(i).SetZero()
582 }
583 } else {
584 v.SetLen(i)
585 }
586 }
587 if i == 0 && v.Kind() == reflect.Slice {
588 v.Set(reflect.MakeSlice(v.Type(), 0, 0))
589 }
590 return nil
591 }
592
593 var nullLiteral = []byte("null")
594 var textUnmarshalerType = reflect.TypeFor[encoding.TextUnmarshaler]()
595
596
597
598 func (d *decodeState) object(v reflect.Value) error {
599
600 u, ut, pv := indirect(v, false)
601 if u != nil {
602 start := d.readIndex()
603 d.skip()
604 return u.UnmarshalJSON(d.data[start:d.off])
605 }
606 if ut != nil {
607 d.saveError(&UnmarshalTypeError{Value: "object", Type: v.Type(), Offset: int64(d.off)})
608 d.skip()
609 return nil
610 }
611 v = pv
612 t := v.Type()
613
614
615 if v.Kind() == reflect.Interface && v.NumMethod() == 0 {
616 oi := d.objectInterface()
617 v.Set(reflect.ValueOf(oi))
618 return nil
619 }
620
621 var fields structFields
622
623
624
625
626
627 switch v.Kind() {
628 case reflect.Map:
629
630
631 switch t.Key().Kind() {
632 case reflect.String,
633 reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
634 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
635 default:
636 if !reflect.PointerTo(t.Key()).Implements(textUnmarshalerType) {
637 d.saveError(&UnmarshalTypeError{Value: "object", Type: t, Offset: int64(d.off)})
638 d.skip()
639 return nil
640 }
641 }
642 if v.IsNil() {
643 v.Set(reflect.MakeMap(t))
644 }
645 case reflect.Struct:
646 fields = cachedTypeFields(t)
647
648 default:
649 d.saveError(&UnmarshalTypeError{Value: "object", Type: t, Offset: int64(d.off)})
650 d.skip()
651 return nil
652 }
653
654 var mapElem reflect.Value
655 var origErrorContext errorContext
656 if d.errorContext != nil {
657 origErrorContext = *d.errorContext
658 }
659
660 for {
661
662 d.scanWhile(scanSkipSpace)
663 if d.opcode == scanEndObject {
664
665 break
666 }
667 if d.opcode != scanBeginLiteral {
668 panic(phasePanicMsg)
669 }
670
671
672 start := d.readIndex()
673 d.rescanLiteral()
674 item := d.data[start:d.readIndex()]
675 key, ok := unquoteBytes(item)
676 if !ok {
677 panic(phasePanicMsg)
678 }
679
680
681 var subv reflect.Value
682 destring := false
683
684 if v.Kind() == reflect.Map {
685 elemType := t.Elem()
686 if !mapElem.IsValid() {
687 mapElem = reflect.New(elemType).Elem()
688 } else {
689 mapElem.SetZero()
690 }
691 subv = mapElem
692 } else {
693 f := fields.byExactName[string(key)]
694 if f == nil {
695 f = fields.byFoldedName[string(foldName(key))]
696 }
697 if f != nil {
698 subv = v
699 destring = f.quoted
700 for _, i := range f.index {
701 if subv.Kind() == reflect.Pointer {
702 if subv.IsNil() {
703
704
705
706
707
708 if !subv.CanSet() {
709 d.saveError(fmt.Errorf("json: cannot set embedded pointer to unexported struct: %v", subv.Type().Elem()))
710
711
712 subv = reflect.Value{}
713 destring = false
714 break
715 }
716 subv.Set(reflect.New(subv.Type().Elem()))
717 }
718 subv = subv.Elem()
719 }
720 subv = subv.Field(i)
721 }
722 if d.errorContext == nil {
723 d.errorContext = new(errorContext)
724 }
725 d.errorContext.FieldStack = append(d.errorContext.FieldStack, f.name)
726 d.errorContext.Struct = t
727 } else if d.disallowUnknownFields {
728 d.saveError(fmt.Errorf("json: unknown field %q", key))
729 }
730 }
731
732
733 if d.opcode == scanSkipSpace {
734 d.scanWhile(scanSkipSpace)
735 }
736 if d.opcode != scanObjectKey {
737 panic(phasePanicMsg)
738 }
739 d.scanWhile(scanSkipSpace)
740
741 if destring {
742 switch qv := d.valueQuoted().(type) {
743 case nil:
744 if err := d.literalStore(nullLiteral, subv, false); err != nil {
745 return err
746 }
747 case string:
748 if err := d.literalStore([]byte(qv), subv, true); err != nil {
749 return err
750 }
751 default:
752 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal unquoted value into %v", subv.Type()))
753 }
754 } else {
755 if err := d.value(subv); err != nil {
756 return err
757 }
758 }
759
760
761
762 if v.Kind() == reflect.Map {
763 kt := t.Key()
764 var kv reflect.Value
765 if reflect.PointerTo(kt).Implements(textUnmarshalerType) {
766 kv = reflect.New(kt)
767 if err := d.literalStore(item, kv, true); err != nil {
768 return err
769 }
770 kv = kv.Elem()
771 } else {
772 switch kt.Kind() {
773 case reflect.String:
774 kv = reflect.New(kt).Elem()
775 kv.SetString(string(key))
776 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
777 s := string(key)
778 n, err := strconv.ParseInt(s, 10, 64)
779 if err != nil || kt.OverflowInt(n) {
780 d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: kt, Offset: int64(start + 1)})
781 break
782 }
783 kv = reflect.New(kt).Elem()
784 kv.SetInt(n)
785 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
786 s := string(key)
787 n, err := strconv.ParseUint(s, 10, 64)
788 if err != nil || kt.OverflowUint(n) {
789 d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: kt, Offset: int64(start + 1)})
790 break
791 }
792 kv = reflect.New(kt).Elem()
793 kv.SetUint(n)
794 default:
795 panic("json: Unexpected key type")
796 }
797 }
798 if kv.IsValid() {
799 v.SetMapIndex(kv, subv)
800 }
801 }
802
803
804 if d.opcode == scanSkipSpace {
805 d.scanWhile(scanSkipSpace)
806 }
807 if d.errorContext != nil {
808
809
810
811 d.errorContext.FieldStack = d.errorContext.FieldStack[:len(origErrorContext.FieldStack)]
812 d.errorContext.Struct = origErrorContext.Struct
813 }
814 if d.opcode == scanEndObject {
815 break
816 }
817 if d.opcode != scanObjectValue {
818 panic(phasePanicMsg)
819 }
820 }
821 return nil
822 }
823
824
825
826 func (d *decodeState) convertNumber(s string) (any, error) {
827 if d.useNumber {
828 return Number(s), nil
829 }
830 f, err := strconv.ParseFloat(s, 64)
831 if err != nil {
832 return nil, &UnmarshalTypeError{Value: "number " + s, Type: reflect.TypeFor[float64](), Offset: int64(d.off)}
833 }
834 return f, nil
835 }
836
837 var numberType = reflect.TypeFor[Number]()
838
839
840
841
842
843
844 func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool) error {
845
846 if len(item) == 0 {
847
848 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
849 return nil
850 }
851 isNull := item[0] == 'n'
852 u, ut, pv := indirect(v, isNull)
853 if u != nil {
854 return u.UnmarshalJSON(item)
855 }
856 if ut != nil {
857 if item[0] != '"' {
858 if fromQuoted {
859 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
860 return nil
861 }
862 val := "number"
863 switch item[0] {
864 case 'n':
865 val = "null"
866 case 't', 'f':
867 val = "bool"
868 }
869 d.saveError(&UnmarshalTypeError{Value: val, Type: v.Type(), Offset: int64(d.readIndex())})
870 return nil
871 }
872 s, ok := unquoteBytes(item)
873 if !ok {
874 if fromQuoted {
875 return fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())
876 }
877 panic(phasePanicMsg)
878 }
879 return ut.UnmarshalText(s)
880 }
881
882 v = pv
883
884 switch c := item[0]; c {
885 case 'n':
886
887
888 if fromQuoted && string(item) != "null" {
889 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
890 break
891 }
892 switch v.Kind() {
893 case reflect.Interface, reflect.Pointer, reflect.Map, reflect.Slice:
894 v.SetZero()
895
896 }
897 case 't', 'f':
898 value := item[0] == 't'
899
900
901 if fromQuoted && string(item) != "true" && string(item) != "false" {
902 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
903 break
904 }
905 switch v.Kind() {
906 default:
907 if fromQuoted {
908 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
909 } else {
910 d.saveError(&UnmarshalTypeError{Value: "bool", Type: v.Type(), Offset: int64(d.readIndex())})
911 }
912 case reflect.Bool:
913 v.SetBool(value)
914 case reflect.Interface:
915 if v.NumMethod() == 0 {
916 v.Set(reflect.ValueOf(value))
917 } else {
918 d.saveError(&UnmarshalTypeError{Value: "bool", Type: v.Type(), Offset: int64(d.readIndex())})
919 }
920 }
921
922 case '"':
923 s, ok := unquoteBytes(item)
924 if !ok {
925 if fromQuoted {
926 return fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())
927 }
928 panic(phasePanicMsg)
929 }
930 switch v.Kind() {
931 default:
932 d.saveError(&UnmarshalTypeError{Value: "string", Type: v.Type(), Offset: int64(d.readIndex())})
933 case reflect.Slice:
934 if v.Type().Elem().Kind() != reflect.Uint8 {
935 d.saveError(&UnmarshalTypeError{Value: "string", Type: v.Type(), Offset: int64(d.readIndex())})
936 break
937 }
938 b := make([]byte, base64.StdEncoding.DecodedLen(len(s)))
939 n, err := base64.StdEncoding.Decode(b, s)
940 if err != nil {
941 d.saveError(err)
942 break
943 }
944 v.SetBytes(b[:n])
945 case reflect.String:
946 t := string(s)
947 if v.Type() == numberType && !isValidNumber(t) {
948 return fmt.Errorf("json: invalid number literal, trying to unmarshal %q into Number", item)
949 }
950 v.SetString(t)
951 case reflect.Interface:
952 if v.NumMethod() == 0 {
953 v.Set(reflect.ValueOf(string(s)))
954 } else {
955 d.saveError(&UnmarshalTypeError{Value: "string", Type: v.Type(), Offset: int64(d.readIndex())})
956 }
957 }
958
959 default:
960 if c != '-' && (c < '0' || c > '9') {
961 if fromQuoted {
962 return fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())
963 }
964 panic(phasePanicMsg)
965 }
966 switch v.Kind() {
967 default:
968 if v.Kind() == reflect.String && v.Type() == numberType {
969
970
971 v.SetString(string(item))
972 break
973 }
974 if fromQuoted {
975 return fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())
976 }
977 d.saveError(&UnmarshalTypeError{Value: "number", Type: v.Type(), Offset: int64(d.readIndex())})
978 case reflect.Interface:
979 n, err := d.convertNumber(string(item))
980 if err != nil {
981 d.saveError(err)
982 break
983 }
984 if v.NumMethod() != 0 {
985 d.saveError(&UnmarshalTypeError{Value: "number", Type: v.Type(), Offset: int64(d.readIndex())})
986 break
987 }
988 v.Set(reflect.ValueOf(n))
989
990 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
991 n, err := strconv.ParseInt(string(item), 10, 64)
992 if err != nil || v.OverflowInt(n) {
993 d.saveError(&UnmarshalTypeError{Value: "number " + string(item), Type: v.Type(), Offset: int64(d.readIndex())})
994 break
995 }
996 v.SetInt(n)
997
998 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
999 n, err := strconv.ParseUint(string(item), 10, 64)
1000 if err != nil || v.OverflowUint(n) {
1001 d.saveError(&UnmarshalTypeError{Value: "number " + string(item), Type: v.Type(), Offset: int64(d.readIndex())})
1002 break
1003 }
1004 v.SetUint(n)
1005
1006 case reflect.Float32, reflect.Float64:
1007 n, err := strconv.ParseFloat(string(item), v.Type().Bits())
1008 if err != nil || v.OverflowFloat(n) {
1009 d.saveError(&UnmarshalTypeError{Value: "number " + string(item), Type: v.Type(), Offset: int64(d.readIndex())})
1010 break
1011 }
1012 v.SetFloat(n)
1013 }
1014 }
1015 return nil
1016 }
1017
1018
1019
1020
1021
1022
1023 func (d *decodeState) valueInterface() (val any) {
1024 switch d.opcode {
1025 default:
1026 panic(phasePanicMsg)
1027 case scanBeginArray:
1028 val = d.arrayInterface()
1029 d.scanNext()
1030 case scanBeginObject:
1031 val = d.objectInterface()
1032 d.scanNext()
1033 case scanBeginLiteral:
1034 val = d.literalInterface()
1035 }
1036 return
1037 }
1038
1039
1040 func (d *decodeState) arrayInterface() []any {
1041 var v = make([]any, 0)
1042 for {
1043
1044 d.scanWhile(scanSkipSpace)
1045 if d.opcode == scanEndArray {
1046 break
1047 }
1048
1049 v = append(v, d.valueInterface())
1050
1051
1052 if d.opcode == scanSkipSpace {
1053 d.scanWhile(scanSkipSpace)
1054 }
1055 if d.opcode == scanEndArray {
1056 break
1057 }
1058 if d.opcode != scanArrayValue {
1059 panic(phasePanicMsg)
1060 }
1061 }
1062 return v
1063 }
1064
1065
1066 func (d *decodeState) objectInterface() map[string]any {
1067 m := make(map[string]any)
1068 for {
1069
1070 d.scanWhile(scanSkipSpace)
1071 if d.opcode == scanEndObject {
1072
1073 break
1074 }
1075 if d.opcode != scanBeginLiteral {
1076 panic(phasePanicMsg)
1077 }
1078
1079
1080 start := d.readIndex()
1081 d.rescanLiteral()
1082 item := d.data[start:d.readIndex()]
1083 key, ok := unquote(item)
1084 if !ok {
1085 panic(phasePanicMsg)
1086 }
1087
1088
1089 if d.opcode == scanSkipSpace {
1090 d.scanWhile(scanSkipSpace)
1091 }
1092 if d.opcode != scanObjectKey {
1093 panic(phasePanicMsg)
1094 }
1095 d.scanWhile(scanSkipSpace)
1096
1097
1098 m[key] = d.valueInterface()
1099
1100
1101 if d.opcode == scanSkipSpace {
1102 d.scanWhile(scanSkipSpace)
1103 }
1104 if d.opcode == scanEndObject {
1105 break
1106 }
1107 if d.opcode != scanObjectValue {
1108 panic(phasePanicMsg)
1109 }
1110 }
1111 return m
1112 }
1113
1114
1115
1116
1117 func (d *decodeState) literalInterface() any {
1118
1119 start := d.readIndex()
1120 d.rescanLiteral()
1121
1122 item := d.data[start:d.readIndex()]
1123
1124 switch c := item[0]; c {
1125 case 'n':
1126 return nil
1127
1128 case 't', 'f':
1129 return c == 't'
1130
1131 case '"':
1132 s, ok := unquote(item)
1133 if !ok {
1134 panic(phasePanicMsg)
1135 }
1136 return s
1137
1138 default:
1139 if c != '-' && (c < '0' || c > '9') {
1140 panic(phasePanicMsg)
1141 }
1142 n, err := d.convertNumber(string(item))
1143 if err != nil {
1144 d.saveError(err)
1145 }
1146 return n
1147 }
1148 }
1149
1150
1151
1152 func getu4(s []byte) rune {
1153 if len(s) < 6 || s[0] != '\\' || s[1] != 'u' {
1154 return -1
1155 }
1156 var r rune
1157 for _, c := range s[2:6] {
1158 switch {
1159 case '0' <= c && c <= '9':
1160 c = c - '0'
1161 case 'a' <= c && c <= 'f':
1162 c = c - 'a' + 10
1163 case 'A' <= c && c <= 'F':
1164 c = c - 'A' + 10
1165 default:
1166 return -1
1167 }
1168 r = r*16 + rune(c)
1169 }
1170 return r
1171 }
1172
1173
1174
1175 func unquote(s []byte) (t string, ok bool) {
1176 s, ok = unquoteBytes(s)
1177 t = string(s)
1178 return
1179 }
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190 func unquoteBytes(s []byte) (t []byte, ok bool) {
1191 if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' {
1192 return
1193 }
1194 s = s[1 : len(s)-1]
1195
1196
1197
1198
1199 r := 0
1200 for r < len(s) {
1201 c := s[r]
1202 if c == '\\' || c == '"' || c < ' ' {
1203 break
1204 }
1205 if c < utf8.RuneSelf {
1206 r++
1207 continue
1208 }
1209 rr, size := utf8.DecodeRune(s[r:])
1210 if rr == utf8.RuneError && size == 1 {
1211 break
1212 }
1213 r += size
1214 }
1215 if r == len(s) {
1216 return s, true
1217 }
1218
1219 b := make([]byte, len(s)+2*utf8.UTFMax)
1220 w := copy(b, s[0:r])
1221 for r < len(s) {
1222
1223
1224
1225 if w >= len(b)-2*utf8.UTFMax {
1226 nb := make([]byte, (len(b)+utf8.UTFMax)*2)
1227 copy(nb, b[0:w])
1228 b = nb
1229 }
1230 switch c := s[r]; {
1231 case c == '\\':
1232 r++
1233 if r >= len(s) {
1234 return
1235 }
1236 switch s[r] {
1237 default:
1238 return
1239 case '"', '\\', '/', '\'':
1240 b[w] = s[r]
1241 r++
1242 w++
1243 case 'b':
1244 b[w] = '\b'
1245 r++
1246 w++
1247 case 'f':
1248 b[w] = '\f'
1249 r++
1250 w++
1251 case 'n':
1252 b[w] = '\n'
1253 r++
1254 w++
1255 case 'r':
1256 b[w] = '\r'
1257 r++
1258 w++
1259 case 't':
1260 b[w] = '\t'
1261 r++
1262 w++
1263 case 'u':
1264 r--
1265 rr := getu4(s[r:])
1266 if rr < 0 {
1267 return
1268 }
1269 r += 6
1270 if utf16.IsSurrogate(rr) {
1271 rr1 := getu4(s[r:])
1272 if dec := utf16.DecodeRune(rr, rr1); dec != unicode.ReplacementChar {
1273
1274 r += 6
1275 w += utf8.EncodeRune(b[w:], dec)
1276 break
1277 }
1278
1279 rr = unicode.ReplacementChar
1280 }
1281 w += utf8.EncodeRune(b[w:], rr)
1282 }
1283
1284
1285 case c == '"', c < ' ':
1286 return
1287
1288
1289 case c < utf8.RuneSelf:
1290 b[w] = c
1291 r++
1292 w++
1293
1294
1295 default:
1296 rr, size := utf8.DecodeRune(s[r:])
1297 r += size
1298 w += utf8.EncodeRune(b[w:], rr)
1299 }
1300 }
1301 return b[0:w], true
1302 }
1303
View as plain text