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