Debian Codegraph @

json/decode.go

raw symbols SCIP
1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Represents JSON data structure using native Go types: booleans, floats,
6 // strings, arrays, and maps.
7
8 package json
9
10 import (
11 "bytes"
12 "encoding"
13 "encoding/base64"
14 "errors"
15 "fmt"
16 "reflect"
17 "runtime"
18 "strconv"
19 "unicode"
20 "unicode/utf16"
21 "unicode/utf8"
22 )
23
24 // Unmarshal parses the JSON-encoded data and stores the result
25 // in the value pointed to by v.
26 //
27 // Unmarshal uses the inverse of the encodings that
28 // Marshal uses, allocating maps, slices, and pointers as necessary,
29 // with the following additional rules:
30 //
31 // To unmarshal JSON into a pointer, Unmarshal first handles the case of
32 // the JSON being the JSON literal null. In that case, Unmarshal sets
33 // the pointer to nil. Otherwise, Unmarshal unmarshals the JSON into
34 // the value pointed at by the pointer. If the pointer is nil, Unmarshal
35 // allocates a new value for it to point to.
36 //
37 // To unmarshal JSON into a struct, Unmarshal matches incoming object
38 // keys to the keys used by Marshal (either the struct field name or its tag),
39 // preferring an exact match but also accepting a case-insensitive match.
40 // Unmarshal will only set exported fields of the struct.
41 //
42 // To unmarshal JSON into an interface value,
43 // Unmarshal stores one of these in the interface value:
44 //
45 // bool, for JSON booleans
46 // float64, for JSON numbers
47 // string, for JSON strings
48 // []interface{}, for JSON arrays
49 // map[string]interface{}, for JSON objects
50 // nil for JSON null
51 //
52 // To unmarshal a JSON array into a slice, Unmarshal resets the slice length
53 // to zero and then appends each element to the slice.
54 // As a special case, to unmarshal an empty JSON array into a slice,
55 // Unmarshal replaces the slice with a new empty slice.
56 //
57 // To unmarshal a JSON array into a Go array, Unmarshal decodes
58 // JSON array elements into corresponding Go array elements.
59 // If the Go array is smaller than the JSON array,
60 // the additional JSON array elements are discarded.
61 // If the JSON array is smaller than the Go array,
62 // the additional Go array elements are set to zero values.
63 //
64 // To unmarshal a JSON object into a string-keyed map, Unmarshal first
65 // establishes a map to use, If the map is nil, Unmarshal allocates a new map.
66 // Otherwise Unmarshal reuses the existing map, keeping existing entries.
67 // Unmarshal then stores key-value pairs from the JSON object into the map.
68 //
69 // If a JSON value is not appropriate for a given target type,
70 // or if a JSON number overflows the target type, Unmarshal
71 // skips that field and completes the unmarshaling as best it can.
72 // If no more serious errors are encountered, Unmarshal returns
73 // an UnmarshalTypeError describing the earliest such error.
74 //
75 // The JSON null value unmarshals into an interface, map, pointer, or slice
76 // by setting that Go value to nil. Because null is often used in JSON to mean
77 // ``not present,'' unmarshaling a JSON null into any other Go type has no effect
78 // on the value and produces no error.
79 //
80 // When unmarshaling quoted strings, invalid UTF-8 or
81 // invalid UTF-16 surrogate pairs are not treated as an error.
82 // Instead, they are replaced by the Unicode replacement
83 // character U+FFFD.
84 //
85 func Unmarshal(data []byte, v interface{}) error {
86 // Check for well-formedness.
87 // Avoids filling out half a data structure
88 // before discovering a JSON syntax error.
89 var d decodeState
90 err := checkValid(data, &d.scan)
91 if err != nil {
92 return err
93 }
94
95 d.init(data)
96 return d.unmarshal(v)
97 }
98
99 // Unmarshaler is the interface implemented by objects
100 // that can unmarshal a JSON description of themselves.
101 // The input can be assumed to be a valid encoding of
102 // a JSON value. UnmarshalJSON must copy the JSON data
103 // if it wishes to retain the data after returning.
104 type Unmarshaler interface {
105 UnmarshalJSON([]byte) error
106 }
107
108 // An UnmarshalTypeError describes a JSON value that was
109 // not appropriate for a value of a specific Go type.
110 type UnmarshalTypeError struct {
111 Value string // description of JSON value - "bool", "array", "number -5"
112 Type reflect.Type // type of Go value it could not be assigned to
113 Offset int64 // error occurred after reading Offset bytes
114 }
115
116 func (e *UnmarshalTypeError) Error() string {
117 return "json: cannot unmarshal " + e.Value + " into Go value of type " + e.Type.String()
118 }
119
120 // An UnmarshalFieldError describes a JSON object key that
121 // led to an unexported (and therefore unwritable) struct field.
122 // (No longer used; kept for compatibility.)
123 type UnmarshalFieldError struct {
124 Key string
125 Type reflect.Type
126 Field reflect.StructField
127 }
128
129 func (e *UnmarshalFieldError) Error() string {
130 return "json: cannot unmarshal object key " + strconv.Quote(e.Key) + " into unexported field " + e.Field.Name + " of type " + e.Type.String()
131 }
132
133 // An InvalidUnmarshalError describes an invalid argument passed to Unmarshal.
134 // (The argument to Unmarshal must be a non-nil pointer.)
135 type InvalidUnmarshalError struct {
136 Type reflect.Type
137 }
138
139 func (e *InvalidUnmarshalError) Error() string {
140 if e.Type == nil {
141 return "json: Unmarshal(nil)"
142 }
143
144 if e.Type.Kind() != reflect.Ptr {
145 return "json: Unmarshal(non-pointer " + e.Type.String() + ")"
146 }
147 return "json: Unmarshal(nil " + e.Type.String() + ")"
148 }
149
150 func (d *decodeState) unmarshal(v interface{}) (err error) {
151 defer func() {
152 if r := recover(); r != nil {
153 if _, ok := r.(runtime.Error); ok {
154 panic(r)
155 }
156 err = r.(error)
157 }
158 }()
159
160 rv := reflect.ValueOf(v)
161 if rv.Kind() != reflect.Ptr || rv.IsNil() {
162 return &InvalidUnmarshalError{reflect.TypeOf(v)}
163 }
164
165 d.scan.reset()
166 // We decode rv not rv.Elem because the Unmarshaler interface
167 // test must be applied at the top level of the value.
168 d.value(rv)
169 return d.savedError
170 }
171
172 // A Number represents a JSON number literal.
173 type Number string
174
175 // String returns the literal text of the number.
176 func (n Number) String() string { return string(n) }
177
178 // Float64 returns the number as a float64.
179 func (n Number) Float64() (float64, error) {
180 return strconv.ParseFloat(string(n), 64)
181 }
182
183 // Int64 returns the number as an int64.
184 func (n Number) Int64() (int64, error) {
185 return strconv.ParseInt(string(n), 10, 64)
186 }
187
188 // isValidNumber reports whether s is a valid JSON number literal.
189 func isValidNumber(s string) bool {
190 // This function implements the JSON numbers grammar.
191 // See https://tools.ietf.org/html/rfc7159#section-6
192 // and http://json.org/number.gif
193
194 if s == "" {
195 return false
196 }
197
198 // Optional -
199 if s[0] == '-' {
200 s = s[1:]
201 if s == "" {
202 return false
203 }
204 }
205
206 // Digits
207 switch {
208 default:
209 return false
210
211 case s[0] == '0':
212 s = s[1:]
213
214 case '1' <= s[0] && s[0] <= '9':
215 s = s[1:]
216 for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
217 s = s[1:]
218 }
219 }
220
221 // . followed by 1 or more digits.
222 if len(s) >= 2 && s[0] == '.' && '0' <= s[1] && s[1] <= '9' {
223 s = s[2:]
224 for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
225 s = s[1:]
226 }
227 }
228
229 // e or E followed by an optional - or + and
230 // 1 or more digits.
231 if len(s) >= 2 && (s[0] == 'e' || s[0] == 'E') {
232 s = s[1:]
233 if s[0] == '+' || s[0] == '-' {
234 s = s[1:]
235 if s == "" {
236 return false
237 }
238 }
239 for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
240 s = s[1:]
241 }
242 }
243
244 // Make sure we are at the end.
245 return s == ""
246 }
247
248 // decodeState represents the state while decoding a JSON value.
249 type decodeState struct {
250 data []byte
251 off int // read offset in data
252 scan scanner
253 nextscan scanner // for calls to nextValue
254 savedError error
255 useNumber bool
256 }
257
258 // errPhase is used for errors that should not happen unless
259 // there is a bug in the JSON decoder or something is editing
260 // the data slice while the decoder executes.
261 var errPhase = errors.New("JSON decoder out of sync - data changing underfoot?")
262
263 func (d *decodeState) init(data []byte) *decodeState {
264 d.data = data
265 d.off = 0
266 d.savedError = nil
267 return d
268 }
269
270 // error aborts the decoding by panicking with err.
271 func (d *decodeState) error(err error) {
272 panic(err)
273 }
274
275 // saveError saves the first err it is called with,
276 // for reporting at the end of the unmarshal.
277 func (d *decodeState) saveError(err error) {
278 if d.savedError == nil {
279 d.savedError = err
280 }
281 }
282
283 // next cuts off and returns the next full JSON value in d.data[d.off:].
284 // The next value is known to be an object or array, not a literal.
285 func (d *decodeState) next() []byte {
286 c := d.data[d.off]
287 item, rest, err := nextValue(d.data[d.off:], &d.nextscan)
288 if err != nil {
289 d.error(err)
290 }
291 d.off = len(d.data) - len(rest)
292
293 // Our scanner has seen the opening brace/bracket
294 // and thinks we're still in the middle of the object.
295 // invent a closing brace/bracket to get it out.
296 if c == '{' {
297 d.scan.step(&d.scan, '}')
298 } else {
299 d.scan.step(&d.scan, ']')
300 }
301
302 return item
303 }
304
305 // scanWhile processes bytes in d.data[d.off:] until it
306 // receives a scan code not equal to op.
307 // It updates d.off and returns the new scan code.
308 func (d *decodeState) scanWhile(op int) int {
309 var newOp int
310 for {
311 if d.off >= len(d.data) {
312 newOp = d.scan.eof()
313 d.off = len(d.data) + 1 // mark processed EOF with len+1
314 } else {
315 c := d.data[d.off]
316 d.off++
317 newOp = d.scan.step(&d.scan, c)
318 }
319 if newOp != op {
320 break
321 }
322 }
323 return newOp
324 }
325
326 // value decodes a JSON value from d.data[d.off:] into the value.
327 // it updates d.off to point past the decoded value.
328 func (d *decodeState) value(v reflect.Value) {
329 if !v.IsValid() {
330 _, rest, err := nextValue(d.data[d.off:], &d.nextscan)
331 if err != nil {
332 d.error(err)
333 }
334 d.off = len(d.data) - len(rest)
335
336 // d.scan thinks we're still at the beginning of the item.
337 // Feed in an empty string - the shortest, simplest value -
338 // so that it knows we got to the end of the value.
339 if d.scan.redo {
340 // rewind.
341 d.scan.redo = false
342 d.scan.step = stateBeginValue
343 }
344 d.scan.step(&d.scan, '"')
345 d.scan.step(&d.scan, '"')
346
347 n := len(d.scan.parseState)
348 if n > 0 && d.scan.parseState[n-1] == parseObjectKey {
349 // d.scan thinks we just read an object key; finish the object
350 d.scan.step(&d.scan, ':')
351 d.scan.step(&d.scan, '"')
352 d.scan.step(&d.scan, '"')
353 d.scan.step(&d.scan, '}')
354 }
355
356 return
357 }
358
359 switch op := d.scanWhile(scanSkipSpace); op {
360 default:
361 d.error(errPhase)
362
363 case scanBeginArray:
364 d.array(v)
365
366 case scanBeginObject:
367 d.object(v)
368
369 case scanBeginLiteral:
370 d.literal(v)
371 }
372 }
373
374 type unquotedValue struct{}
375
376 // valueQuoted is like value but decodes a
377 // quoted string literal or literal null into an interface value.
378 // If it finds anything other than a quoted string literal or null,
379 // valueQuoted returns unquotedValue{}.
380 func (d *decodeState) valueQuoted() interface{} {
381 switch op := d.scanWhile(scanSkipSpace); op {
382 default:
383 d.error(errPhase)
384
385 case scanBeginArray:
386 d.array(reflect.Value{})
387
388 case scanBeginObject:
389 d.object(reflect.Value{})
390
391 case scanBeginLiteral:
392 switch v := d.literalInterface().(type) {
393 case nil, string:
394 return v
395 }
396 }
397 return unquotedValue{}
398 }
399
400 // indirect walks down v allocating pointers as needed,
401 // until it gets to a non-pointer.
402 // if it encounters an Unmarshaler, indirect stops and returns that.
403 // if decodingNull is true, indirect stops at the last pointer so it can be set to nil.
404 func (d *decodeState) indirect(v reflect.Value, decodingNull bool) (Unmarshaler, encoding.TextUnmarshaler, reflect.Value) {
405 // If v is a named type and is addressable,
406 // start with its address, so that if the type has pointer methods,
407 // we find them.
408 if v.Kind() != reflect.Ptr && v.Type().Name() != "" && v.CanAddr() {
409 v = v.Addr()
410 }
411 for {
412 // Load value from interface, but only if the result will be
413 // usefully addressable.
414 if v.Kind() == reflect.Interface && !v.IsNil() {
415 e := v.Elem()
416 if e.Kind() == reflect.Ptr && !e.IsNil() && (!decodingNull || e.Elem().Kind() == reflect.Ptr) {
417 v = e
418 continue
419 }
420 }
421
422 if v.Kind() != reflect.Ptr {
423 break
424 }
425
426 if v.Elem().Kind() != reflect.Ptr && decodingNull && v.CanSet() {
427 break
428 }
429 if v.IsNil() {
430 v.Set(reflect.New(v.Type().Elem()))
431 }
432 if v.Type().NumMethod() > 0 {
433 if u, ok := v.Interface().(Unmarshaler); ok {
434 return u, nil, reflect.Value{}
435 }
436 if u, ok := v.Interface().(encoding.TextUnmarshaler); ok {
437 return nil, u, reflect.Value{}
438 }
439 }
440 v = v.Elem()
441 }
442 return nil, nil, v
443 }
444
445 // array consumes an array from d.data[d.off-1:], decoding into the value v.
446 // the first byte of the array ('[') has been read already.
447 func (d *decodeState) array(v reflect.Value) {
448 // Check for unmarshaler.
449 u, ut, pv := d.indirect(v, false)
450 if u != nil {
451 d.off--
452 err := u.UnmarshalJSON(d.next())
453 if err != nil {
454 d.error(err)
455 }
456 return
457 }
458 if ut != nil {
459 d.saveError(&UnmarshalTypeError{"array", v.Type(), int64(d.off)})
460 d.off--
461 d.next()
462 return
463 }
464
465 v = pv
466
467 // Check type of target.
468 switch v.Kind() {
469 case reflect.Interface:
470 if v.NumMethod() == 0 {
471 // Decoding into nil interface? Switch to non-reflect code.
472 v.Set(reflect.ValueOf(d.arrayInterface()))
473 return
474 }
475 // Otherwise it's invalid.
476 fallthrough
477 default:
478 d.saveError(&UnmarshalTypeError{"array", v.Type(), int64(d.off)})
479 d.off--
480 d.next()
481 return
482 case reflect.Array:
483 case reflect.Slice:
484 break
485 }
486
487 i := 0
488 for {
489 // Look ahead for ] - can only happen on first iteration.
490 op := d.scanWhile(scanSkipSpace)
491 if op == scanEndArray {
492 break
493 }
494
495 // Back up so d.value can have the byte we just read.
496 d.off--
497 d.scan.undo(op)
498
499 // Get element of array, growing if necessary.
500 if v.Kind() == reflect.Slice {
501 // Grow slice if necessary
502 if i >= v.Cap() {
503 newcap := v.Cap() + v.Cap()/2
504 if newcap < 4 {
505 newcap = 4
506 }
507 newv := reflect.MakeSlice(v.Type(), v.Len(), newcap)
508 reflect.Copy(newv, v)
509 v.Set(newv)
510 }
511 if i >= v.Len() {
512 v.SetLen(i + 1)
513 }
514 }
515
516 if i < v.Len() {
517 // Decode into element.
518 d.value(v.Index(i))
519 } else {
520 // Ran out of fixed array: skip.
521 d.value(reflect.Value{})
522 }
523 i++
524
525 // Next token must be , or ].
526 op = d.scanWhile(scanSkipSpace)
527 if op == scanEndArray {
528 break
529 }
530 if op != scanArrayValue {
531 d.error(errPhase)
532 }
533 }
534
535 if i < v.Len() {
536 if v.Kind() == reflect.Array {
537 // Array. Zero the rest.
538 z := reflect.Zero(v.Type().Elem())
539 for ; i < v.Len(); i++ {
540 v.Index(i).Set(z)
541 }
542 } else {
543 v.SetLen(i)
544 }
545 }
546 if i == 0 && v.Kind() == reflect.Slice {
547 v.Set(reflect.MakeSlice(v.Type(), 0, 0))
548 }
549 }
550
551 var nullLiteral = []byte("null")
552
553 // object consumes an object from d.data[d.off-1:], decoding into the value v.
554 // the first byte ('{') of the object has been read already.
555 func (d *decodeState) object(v reflect.Value) {
556 // Check for unmarshaler.
557 u, ut, pv := d.indirect(v, false)
558 if u != nil {
559 d.off--
560 err := u.UnmarshalJSON(d.next())
561 if err != nil {
562 d.error(err)
563 }
564 return
565 }
566 if ut != nil {
567 d.saveError(&UnmarshalTypeError{"object", v.Type(), int64(d.off)})
568 d.off--
569 d.next() // skip over { } in input
570 return
571 }
572 v = pv
573
574 // Decoding into nil interface? Switch to non-reflect code.
575 if v.Kind() == reflect.Interface && v.NumMethod() == 0 {
576 v.Set(reflect.ValueOf(d.objectInterface()))
577 return
578 }
579
580 // Check type of target: struct or map[string]T
581 switch v.Kind() {
582 case reflect.Map:
583 // map must have string kind
584 t := v.Type()
585 if t.Key().Kind() != reflect.String {
586 d.saveError(&UnmarshalTypeError{"object", v.Type(), int64(d.off)})
587 d.off--
588 d.next() // skip over { } in input
589 return
590 }
591 if v.IsNil() {
592 v.Set(reflect.MakeMap(t))
593 }
594 case reflect.Struct:
595
596 default:
597 d.saveError(&UnmarshalTypeError{"object", v.Type(), int64(d.off)})
598 d.off--
599 d.next() // skip over { } in input
600 return
601 }
602
603 var mapElem reflect.Value
604 keys := map[string]bool{}
605
606 for {
607 // Read opening " of string key or closing }.
608 op := d.scanWhile(scanSkipSpace)
609 if op == scanEndObject {
610 // closing } - can only happen on first iteration.
611 break
612 }
613 if op != scanBeginLiteral {
614 d.error(errPhase)
615 }
616
617 // Read key.
618 start := d.off - 1
619 op = d.scanWhile(scanContinue)
620 item := d.data[start : d.off-1]
621 key, ok := unquote(item)
622 if !ok {
623 d.error(errPhase)
624 }
625
626 // Check for duplicate keys.
627 _, ok = keys[key]
628 if !ok {
629 keys[key] = true
630 } else {
631 d.error(fmt.Errorf("json: duplicate key '%s' in object", key))
632 }
633
634 // Figure out field corresponding to key.
635 var subv reflect.Value
636 destring := false // whether the value is wrapped in a string to be decoded first
637
638 if v.Kind() == reflect.Map {
639 elemType := v.Type().Elem()
640 if !mapElem.IsValid() {
641 mapElem = reflect.New(elemType).Elem()
642 } else {
643 mapElem.Set(reflect.Zero(elemType))
644 }
645 subv = mapElem
646 } else {
647 var f *field
648 fields := cachedTypeFields(v.Type())
649 for i := range fields {
650 ff := &fields[i]
651 if bytes.Equal(ff.nameBytes, []byte(key)) {
652 f = ff
653 break
654 }
655 }
656 if f != nil {
657 subv = v
658 destring = f.quoted
659 for _, i := range f.index {
660 if subv.Kind() == reflect.Ptr {
661 if subv.IsNil() {
662 subv.Set(reflect.New(subv.Type().Elem()))
663 }
664 subv = subv.Elem()
665 }
666 subv = subv.Field(i)
667 }
668 }
669 }
670
671 // Read : before value.
672 if op == scanSkipSpace {
673 op = d.scanWhile(scanSkipSpace)
674 }
675 if op != scanObjectKey {
676 d.error(errPhase)
677 }
678
679 // Read value.
680 if destring {
681 switch qv := d.valueQuoted().(type) {
682 case nil:
683 d.literalStore(nullLiteral, subv, false)
684 case string:
685 d.literalStore([]byte(qv), subv, true)
686 default:
687 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal unquoted value into %v", subv.Type()))
688 }
689 } else {
690 d.value(subv)
691 }
692
693 // Write value back to map;
694 // if using struct, subv points into struct already.
695 if v.Kind() == reflect.Map {
696 kv := reflect.ValueOf(key).Convert(v.Type().Key())
697 v.SetMapIndex(kv, subv)
698 }
699
700 // Next token must be , or }.
701 op = d.scanWhile(scanSkipSpace)
702 if op == scanEndObject {
703 break
704 }
705 if op != scanObjectValue {
706 d.error(errPhase)
707 }
708 }
709 }
710
711 // literal consumes a literal from d.data[d.off-1:], decoding into the value v.
712 // The first byte of the literal has been read already
713 // (that's how the caller knows it's a literal).
714 func (d *decodeState) literal(v reflect.Value) {
715 // All bytes inside literal return scanContinue op code.
716 start := d.off - 1
717 op := d.scanWhile(scanContinue)
718
719 // Scan read one byte too far; back up.
720 d.off--
721 d.scan.undo(op)
722
723 d.literalStore(d.data[start:d.off], v, false)
724 }
725
726 // convertNumber converts the number literal s to a float64 or a Number
727 // depending on the setting of d.useNumber.
728 func (d *decodeState) convertNumber(s string) (interface{}, error) {
729 if d.useNumber {
730 return Number(s), nil
731 }
732 f, err := strconv.ParseFloat(s, 64)
733 if err != nil {
734 return nil, &UnmarshalTypeError{"number " + s, reflect.TypeOf(0.0), int64(d.off)}
735 }
736 return f, nil
737 }
738
739 var numberType = reflect.TypeOf(Number(""))
740
741 // literalStore decodes a literal stored in item into v.
742 //
743 // fromQuoted indicates whether this literal came from unwrapping a
744 // string from the ",string" struct tag option. this is used only to
745 // produce more helpful error messages.
746 func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool) {
747 // Check for unmarshaler.
748 if len(item) == 0 {
749 //Empty string given
750 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
751 return
752 }
753 wantptr := item[0] == 'n' // null
754 u, ut, pv := d.indirect(v, wantptr)
755 if u != nil {
756 err := u.UnmarshalJSON(item)
757 if err != nil {
758 d.error(err)
759 }
760 return
761 }
762 if ut != nil {
763 if item[0] != '"' {
764 if fromQuoted {
765 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
766 } else {
767 d.saveError(&UnmarshalTypeError{"string", v.Type(), int64(d.off)})
768 }
769 return
770 }
771 s, ok := unquoteBytes(item)
772 if !ok {
773 if fromQuoted {
774 d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
775 } else {
776 d.error(errPhase)
777 }
778 }
779 err := ut.UnmarshalText(s)
780 if err != nil {
781 d.error(err)
782 }
783 return
784 }
785
786 v = pv
787
788 switch c := item[0]; c {
789 case 'n': // null
790 switch v.Kind() {
791 case reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice:
792 v.Set(reflect.Zero(v.Type()))
793 // otherwise, ignore null for primitives/string
794 }
795 case 't', 'f': // true, false
796 value := c == 't'
797 switch v.Kind() {
798 default:
799 if fromQuoted {
800 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
801 } else {
802 d.saveError(&UnmarshalTypeError{"bool", v.Type(), int64(d.off)})
803 }
804 case reflect.Bool:
805 v.SetBool(value)
806 case reflect.Interface:
807 if v.NumMethod() == 0 {
808 v.Set(reflect.ValueOf(value))
809 } else {
810 d.saveError(&UnmarshalTypeError{"bool", v.Type(), int64(d.off)})
811 }
812 }
813
814 case '"': // string
815 s, ok := unquoteBytes(item)
816 if !ok {
817 if fromQuoted {
818 d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
819 } else {
820 d.error(errPhase)
821 }
822 }
823 switch v.Kind() {
824 default:
825 d.saveError(&UnmarshalTypeError{"string", v.Type(), int64(d.off)})
826 case reflect.Slice:
827 if v.Type().Elem().Kind() != reflect.Uint8 {
828 d.saveError(&UnmarshalTypeError{"string", v.Type(), int64(d.off)})
829 break
830 }
831 b := make([]byte, base64.StdEncoding.DecodedLen(len(s)))
832 n, err := base64.StdEncoding.Decode(b, s)
833 if err != nil {
834 d.saveError(err)
835 break
836 }
837 v.SetBytes(b[:n])
838 case reflect.String:
839 v.SetString(string(s))
840 case reflect.Interface:
841 if v.NumMethod() == 0 {
842 v.Set(reflect.ValueOf(string(s)))
843 } else {
844 d.saveError(&UnmarshalTypeError{"string", v.Type(), int64(d.off)})
845 }
846 }
847
848 default: // number
849 if c != '-' && (c < '0' || c > '9') {
850 if fromQuoted {
851 d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
852 } else {
853 d.error(errPhase)
854 }
855 }
856 s := string(item)
857 switch v.Kind() {
858 default:
859 if v.Kind() == reflect.String && v.Type() == numberType {
860 v.SetString(s)
861 if !isValidNumber(s) {
862 d.error(fmt.Errorf("json: invalid number literal, trying to unmarshal %q into Number", item))
863 }
864 break
865 }
866 if fromQuoted {
867 d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
868 } else {
869 d.error(&UnmarshalTypeError{"number", v.Type(), int64(d.off)})
870 }
871 case reflect.Interface:
872 n, err := d.convertNumber(s)
873 if err != nil {
874 d.saveError(err)
875 break
876 }
877 if v.NumMethod() != 0 {
878 d.saveError(&UnmarshalTypeError{"number", v.Type(), int64(d.off)})
879 break
880 }
881 v.Set(reflect.ValueOf(n))
882
883 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
884 n, err := strconv.ParseInt(s, 10, 64)
885 if err != nil || v.OverflowInt(n) {
886 d.saveError(&UnmarshalTypeError{"number " + s, v.Type(), int64(d.off)})
887 break
888 }
889 v.SetInt(n)
890
891 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
892 n, err := strconv.ParseUint(s, 10, 64)
893 if err != nil || v.OverflowUint(n) {
894 d.saveError(&UnmarshalTypeError{"number " + s, v.Type(), int64(d.off)})
895 break
896 }
897 v.SetUint(n)
898
899 case reflect.Float32, reflect.Float64:
900 n, err := strconv.ParseFloat(s, v.Type().Bits())
901 if err != nil || v.OverflowFloat(n) {
902 d.saveError(&UnmarshalTypeError{"number " + s, v.Type(), int64(d.off)})
903 break
904 }
905 v.SetFloat(n)
906 }
907 }
908 }
909
910 // The xxxInterface routines build up a value to be stored
911 // in an empty interface. They are not strictly necessary,
912 // but they avoid the weight of reflection in this common case.
913
914 // valueInterface is like value but returns interface{}
915 func (d *decodeState) valueInterface() interface{} {
916 switch d.scanWhile(scanSkipSpace) {
917 default:
918 d.error(errPhase)
919 panic("unreachable")
920 case scanBeginArray:
921 return d.arrayInterface()
922 case scanBeginObject:
923 return d.objectInterface()
924 case scanBeginLiteral:
925 return d.literalInterface()
926 }
927 }
928
929 // arrayInterface is like array but returns []interface{}.
930 func (d *decodeState) arrayInterface() []interface{} {
931 var v = make([]interface{}, 0)
932 for {
933 // Look ahead for ] - can only happen on first iteration.
934 op := d.scanWhile(scanSkipSpace)
935 if op == scanEndArray {
936 break
937 }
938
939 // Back up so d.value can have the byte we just read.
940 d.off--
941 d.scan.undo(op)
942
943 v = append(v, d.valueInterface())
944
945 // Next token must be , or ].
946 op = d.scanWhile(scanSkipSpace)
947 if op == scanEndArray {
948 break
949 }
950 if op != scanArrayValue {
951 d.error(errPhase)
952 }
953 }
954 return v
955 }
956
957 // objectInterface is like object but returns map[string]interface{}.
958 func (d *decodeState) objectInterface() map[string]interface{} {
959 m := make(map[string]interface{})
960 keys := map[string]bool{}
961
962 for {
963 // Read opening " of string key or closing }.
964 op := d.scanWhile(scanSkipSpace)
965 if op == scanEndObject {
966 // closing } - can only happen on first iteration.
967 break
968 }
969 if op != scanBeginLiteral {
970 d.error(errPhase)
971 }
972
973 // Read string key.
974 start := d.off - 1
975 op = d.scanWhile(scanContinue)
976 item := d.data[start : d.off-1]
977 key, ok := unquote(item)
978 if !ok {
979 d.error(errPhase)
980 }
981
982 // Check for duplicate keys.
983 _, ok = keys[key]
984 if !ok {
985 keys[key] = true
986 } else {
987 d.error(fmt.Errorf("json: duplicate key '%s' in object", key))
988 }
989
990 // Read : before value.
991 if op == scanSkipSpace {
992 op = d.scanWhile(scanSkipSpace)
993 }
994 if op != scanObjectKey {
995 d.error(errPhase)
996 }
997
998 // Read value.
999 m[key] = d.valueInterface()
1000
1001 // Next token must be , or }.
1002 op = d.scanWhile(scanSkipSpace)
1003 if op == scanEndObject {
1004 break
1005 }
1006 if op != scanObjectValue {
1007 d.error(errPhase)
1008 }
1009 }
1010 return m
1011 }
1012
1013 // literalInterface is like literal but returns an interface value.
1014 func (d *decodeState) literalInterface() interface{} {
1015 // All bytes inside literal return scanContinue op code.
1016 start := d.off - 1
1017 op := d.scanWhile(scanContinue)
1018
1019 // Scan read one byte too far; back up.
1020 d.off--
1021 d.scan.undo(op)
1022 item := d.data[start:d.off]
1023
1024 switch c := item[0]; c {
1025 case 'n': // null
1026 return nil
1027
1028 case 't', 'f': // true, false
1029 return c == 't'
1030
1031 case '"': // string
1032 s, ok := unquote(item)
1033 if !ok {
1034 d.error(errPhase)
1035 }
1036 return s
1037
1038 default: // number
1039 if c != '-' && (c < '0' || c > '9') {
1040 d.error(errPhase)
1041 }
1042 n, err := d.convertNumber(string(item))
1043 if err != nil {
1044 d.saveError(err)
1045 }
1046 return n
1047 }
1048 }
1049
1050 // getu4 decodes \uXXXX from the beginning of s, returning the hex value,
1051 // or it returns -1.
1052 func getu4(s []byte) rune {
1053 if len(s) < 6 || s[0] != '\\' || s[1] != 'u' {
1054 return -1
1055 }
1056 r, err := strconv.ParseUint(string(s[2:6]), 16, 64)
1057 if err != nil {
1058 return -1
1059 }
1060 return rune(r)
1061 }
1062
1063 // unquote converts a quoted JSON string literal s into an actual string t.
1064 // The rules are different than for Go, so cannot use strconv.Unquote.
1065 func unquote(s []byte) (t string, ok bool) {
1066 s, ok = unquoteBytes(s)
1067 t = string(s)
1068 return
1069 }
1070
1071 func unquoteBytes(s []byte) (t []byte, ok bool) {
1072 if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' {
1073 return
1074 }
1075 s = s[1 : len(s)-1]
1076
1077 // Check for unusual characters. If there are none,
1078 // then no unquoting is needed, so return a slice of the
1079 // original bytes.
1080 r := 0
1081 for r < len(s) {
1082 c := s[r]
1083 if c == '\\' || c == '"' || c < ' ' {
1084 break
1085 }
1086 if c < utf8.RuneSelf {
1087 r++
1088 continue
1089 }
1090 rr, size := utf8.DecodeRune(s[r:])
1091 if rr == utf8.RuneError && size == 1 {
1092 break
1093 }
1094 r += size
1095 }
1096 if r == len(s) {
1097 return s, true
1098 }
1099
1100 b := make([]byte, len(s)+2*utf8.UTFMax)
1101 w := copy(b, s[0:r])
1102 for r < len(s) {
1103 // Out of room? Can only happen if s is full of
1104 // malformed UTF-8 and we're replacing each
1105 // byte with RuneError.
1106 if w >= len(b)-2*utf8.UTFMax {
1107 nb := make([]byte, (len(b)+utf8.UTFMax)*2)
1108 copy(nb, b[0:w])
1109 b = nb
1110 }
1111 switch c := s[r]; {
1112 case c == '\\':
1113 r++
1114 if r >= len(s) {
1115 return
1116 }
1117 switch s[r] {
1118 default:
1119 return
1120 case '"', '\\', '/', '\'':
1121 b[w] = s[r]
1122 r++
1123 w++
1124 case 'b':
1125 b[w] = '\b'
1126 r++
1127 w++
1128 case 'f':
1129 b[w] = '\f'
1130 r++
1131 w++
1132 case 'n':
1133 b[w] = '\n'
1134 r++
1135 w++
1136 case 'r':
1137 b[w] = '\r'
1138 r++
1139 w++
1140 case 't':
1141 b[w] = '\t'
1142 r++
1143 w++
1144 case 'u':
1145 r--
1146 rr := getu4(s[r:])
1147 if rr < 0 {
1148 return
1149 }
1150 r += 6
1151 if utf16.IsSurrogate(rr) {
1152 rr1 := getu4(s[r:])
1153 if dec := utf16.DecodeRune(rr, rr1); dec != unicode.ReplacementChar {
1154 // A valid pair; consume.
1155 r += 6
1156 w += utf8.EncodeRune(b[w:], dec)
1157 break
1158 }
1159 // Invalid surrogate; fall back to replacement rune.
1160 rr = unicode.ReplacementChar
1161 }
1162 w += utf8.EncodeRune(b[w:], rr)
1163 }
1164
1165 // Quote, control characters are invalid.
1166 case c == '"', c < ' ':
1167 return
1168
1169 // ASCII
1170 case c < utf8.RuneSelf:
1171 b[w] = c
1172 r++
1173 w++
1174
1175 // Coerce to well-formed UTF-8.
1176 default:
1177 rr, size := utf8.DecodeRune(s[r:])
1178 r += size
1179 w += utf8.EncodeRune(b[w:], rr)
1180 }
1181 }
1182 return b[0:w], true
1183 }