libgo: Merge from revision 18783:00cce3a34d7e of master library.
[official-gcc.git] / libgo / go / encoding / json / decode.go
blobdde0d78e327380724270cab3e8b8a45ae7e1339c
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.
5 // Represents JSON data structure using native Go types: booleans, floats,
6 // strings, arrays, and maps.
8 package json
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"
24 // Unmarshal parses the JSON-encoded data and stores the result
25 // in the value pointed to by v.
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:
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.
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.
41 // To unmarshal JSON into an interface value,
42 // Unmarshal stores one of these in the interface value:
44 // bool, for JSON booleans
45 // float64, for JSON numbers
46 // string, for JSON strings
47 // []interface{}, for JSON arrays
48 // map[string]interface{}, for JSON objects
49 // nil for JSON null
51 // If a JSON value is not appropriate for a given target type,
52 // or if a JSON number overflows the target type, Unmarshal
53 // skips that field and completes the unmarshalling as best it can.
54 // If no more serious errors are encountered, Unmarshal returns
55 // an UnmarshalTypeError describing the earliest such error.
57 // When unmarshaling quoted strings, invalid UTF-8 or
58 // invalid UTF-16 surrogate pairs are not treated as an error.
59 // Instead, they are replaced by the Unicode replacement
60 // character U+FFFD.
62 func Unmarshal(data []byte, v interface{}) error {
63 // Check for well-formedness.
64 // Avoids filling out half a data structure
65 // before discovering a JSON syntax error.
66 var d decodeState
67 err := checkValid(data, &d.scan)
68 if err != nil {
69 return err
72 d.init(data)
73 return d.unmarshal(v)
76 // Unmarshaler is the interface implemented by objects
77 // that can unmarshal a JSON description of themselves.
78 // The input can be assumed to be a valid encoding of
79 // a JSON value. UnmarshalJSON must copy the JSON data
80 // if it wishes to retain the data after returning.
81 type Unmarshaler interface {
82 UnmarshalJSON([]byte) error
85 // An UnmarshalTypeError describes a JSON value that was
86 // not appropriate for a value of a specific Go type.
87 type UnmarshalTypeError struct {
88 Value string // description of JSON value - "bool", "array", "number -5"
89 Type reflect.Type // type of Go value it could not be assigned to
92 func (e *UnmarshalTypeError) Error() string {
93 return "json: cannot unmarshal " + e.Value + " into Go value of type " + e.Type.String()
96 // An UnmarshalFieldError describes a JSON object key that
97 // led to an unexported (and therefore unwritable) struct field.
98 // (No longer used; kept for compatibility.)
99 type UnmarshalFieldError struct {
100 Key string
101 Type reflect.Type
102 Field reflect.StructField
105 func (e *UnmarshalFieldError) Error() string {
106 return "json: cannot unmarshal object key " + strconv.Quote(e.Key) + " into unexported field " + e.Field.Name + " of type " + e.Type.String()
109 // An InvalidUnmarshalError describes an invalid argument passed to Unmarshal.
110 // (The argument to Unmarshal must be a non-nil pointer.)
111 type InvalidUnmarshalError struct {
112 Type reflect.Type
115 func (e *InvalidUnmarshalError) Error() string {
116 if e.Type == nil {
117 return "json: Unmarshal(nil)"
120 if e.Type.Kind() != reflect.Ptr {
121 return "json: Unmarshal(non-pointer " + e.Type.String() + ")"
123 return "json: Unmarshal(nil " + e.Type.String() + ")"
126 func (d *decodeState) unmarshal(v interface{}) (err error) {
127 defer func() {
128 if r := recover(); r != nil {
129 if _, ok := r.(runtime.Error); ok {
130 panic(r)
132 err = r.(error)
136 rv := reflect.ValueOf(v)
137 if rv.Kind() != reflect.Ptr || rv.IsNil() {
138 return &InvalidUnmarshalError{reflect.TypeOf(v)}
141 d.scan.reset()
142 // We decode rv not rv.Elem because the Unmarshaler interface
143 // test must be applied at the top level of the value.
144 d.value(rv)
145 return d.savedError
148 // A Number represents a JSON number literal.
149 type Number string
151 // String returns the literal text of the number.
152 func (n Number) String() string { return string(n) }
154 // Float64 returns the number as a float64.
155 func (n Number) Float64() (float64, error) {
156 return strconv.ParseFloat(string(n), 64)
159 // Int64 returns the number as an int64.
160 func (n Number) Int64() (int64, error) {
161 return strconv.ParseInt(string(n), 10, 64)
164 // decodeState represents the state while decoding a JSON value.
165 type decodeState struct {
166 data []byte
167 off int // read offset in data
168 scan scanner
169 nextscan scanner // for calls to nextValue
170 savedError error
171 tempstr string // scratch space to avoid some allocations
172 useNumber bool
175 // errPhase is used for errors that should not happen unless
176 // there is a bug in the JSON decoder or something is editing
177 // the data slice while the decoder executes.
178 var errPhase = errors.New("JSON decoder out of sync - data changing underfoot?")
180 func (d *decodeState) init(data []byte) *decodeState {
181 d.data = data
182 d.off = 0
183 d.savedError = nil
184 return d
187 // error aborts the decoding by panicking with err.
188 func (d *decodeState) error(err error) {
189 panic(err)
192 // saveError saves the first err it is called with,
193 // for reporting at the end of the unmarshal.
194 func (d *decodeState) saveError(err error) {
195 if d.savedError == nil {
196 d.savedError = err
200 // next cuts off and returns the next full JSON value in d.data[d.off:].
201 // The next value is known to be an object or array, not a literal.
202 func (d *decodeState) next() []byte {
203 c := d.data[d.off]
204 item, rest, err := nextValue(d.data[d.off:], &d.nextscan)
205 if err != nil {
206 d.error(err)
208 d.off = len(d.data) - len(rest)
210 // Our scanner has seen the opening brace/bracket
211 // and thinks we're still in the middle of the object.
212 // invent a closing brace/bracket to get it out.
213 if c == '{' {
214 d.scan.step(&d.scan, '}')
215 } else {
216 d.scan.step(&d.scan, ']')
219 return item
222 // scanWhile processes bytes in d.data[d.off:] until it
223 // receives a scan code not equal to op.
224 // It updates d.off and returns the new scan code.
225 func (d *decodeState) scanWhile(op int) int {
226 var newOp int
227 for {
228 if d.off >= len(d.data) {
229 newOp = d.scan.eof()
230 d.off = len(d.data) + 1 // mark processed EOF with len+1
231 } else {
232 c := int(d.data[d.off])
233 d.off++
234 newOp = d.scan.step(&d.scan, c)
236 if newOp != op {
237 break
240 return newOp
243 // value decodes a JSON value from d.data[d.off:] into the value.
244 // it updates d.off to point past the decoded value.
245 func (d *decodeState) value(v reflect.Value) {
246 if !v.IsValid() {
247 _, rest, err := nextValue(d.data[d.off:], &d.nextscan)
248 if err != nil {
249 d.error(err)
251 d.off = len(d.data) - len(rest)
253 // d.scan thinks we're still at the beginning of the item.
254 // Feed in an empty string - the shortest, simplest value -
255 // so that it knows we got to the end of the value.
256 if d.scan.redo {
257 // rewind.
258 d.scan.redo = false
259 d.scan.step = stateBeginValue
261 d.scan.step(&d.scan, '"')
262 d.scan.step(&d.scan, '"')
264 n := len(d.scan.parseState)
265 if n > 0 && d.scan.parseState[n-1] == parseObjectKey {
266 // d.scan thinks we just read an object key; finish the object
267 d.scan.step(&d.scan, ':')
268 d.scan.step(&d.scan, '"')
269 d.scan.step(&d.scan, '"')
270 d.scan.step(&d.scan, '}')
273 return
276 switch op := d.scanWhile(scanSkipSpace); op {
277 default:
278 d.error(errPhase)
280 case scanBeginArray:
281 d.array(v)
283 case scanBeginObject:
284 d.object(v)
286 case scanBeginLiteral:
287 d.literal(v)
291 // indirect walks down v allocating pointers as needed,
292 // until it gets to a non-pointer.
293 // if it encounters an Unmarshaler, indirect stops and returns that.
294 // if decodingNull is true, indirect stops at the last pointer so it can be set to nil.
295 func (d *decodeState) indirect(v reflect.Value, decodingNull bool) (Unmarshaler, encoding.TextUnmarshaler, reflect.Value) {
296 // If v is a named type and is addressable,
297 // start with its address, so that if the type has pointer methods,
298 // we find them.
299 if v.Kind() != reflect.Ptr && v.Type().Name() != "" && v.CanAddr() {
300 v = v.Addr()
302 for {
303 // Load value from interface, but only if the result will be
304 // usefully addressable.
305 if v.Kind() == reflect.Interface && !v.IsNil() {
306 e := v.Elem()
307 if e.Kind() == reflect.Ptr && !e.IsNil() && (!decodingNull || e.Elem().Kind() == reflect.Ptr) {
308 v = e
309 continue
313 if v.Kind() != reflect.Ptr {
314 break
317 if v.Elem().Kind() != reflect.Ptr && decodingNull && v.CanSet() {
318 break
320 if v.IsNil() {
321 v.Set(reflect.New(v.Type().Elem()))
323 if v.Type().NumMethod() > 0 {
324 if u, ok := v.Interface().(Unmarshaler); ok {
325 return u, nil, reflect.Value{}
327 if u, ok := v.Interface().(encoding.TextUnmarshaler); ok {
328 return nil, u, reflect.Value{}
331 v = v.Elem()
333 return nil, nil, v
336 // array consumes an array from d.data[d.off-1:], decoding into the value v.
337 // the first byte of the array ('[') has been read already.
338 func (d *decodeState) array(v reflect.Value) {
339 // Check for unmarshaler.
340 u, ut, pv := d.indirect(v, false)
341 if u != nil {
342 d.off--
343 err := u.UnmarshalJSON(d.next())
344 if err != nil {
345 d.error(err)
347 return
349 if ut != nil {
350 d.saveError(&UnmarshalTypeError{"array", v.Type()})
351 d.off--
352 d.next()
353 return
356 v = pv
358 // Check type of target.
359 switch v.Kind() {
360 case reflect.Interface:
361 if v.NumMethod() == 0 {
362 // Decoding into nil interface? Switch to non-reflect code.
363 v.Set(reflect.ValueOf(d.arrayInterface()))
364 return
366 // Otherwise it's invalid.
367 fallthrough
368 default:
369 d.saveError(&UnmarshalTypeError{"array", v.Type()})
370 d.off--
371 d.next()
372 return
373 case reflect.Array:
374 case reflect.Slice:
375 break
378 i := 0
379 for {
380 // Look ahead for ] - can only happen on first iteration.
381 op := d.scanWhile(scanSkipSpace)
382 if op == scanEndArray {
383 break
386 // Back up so d.value can have the byte we just read.
387 d.off--
388 d.scan.undo(op)
390 // Get element of array, growing if necessary.
391 if v.Kind() == reflect.Slice {
392 // Grow slice if necessary
393 if i >= v.Cap() {
394 newcap := v.Cap() + v.Cap()/2
395 if newcap < 4 {
396 newcap = 4
398 newv := reflect.MakeSlice(v.Type(), v.Len(), newcap)
399 reflect.Copy(newv, v)
400 v.Set(newv)
402 if i >= v.Len() {
403 v.SetLen(i + 1)
407 if i < v.Len() {
408 // Decode into element.
409 d.value(v.Index(i))
410 } else {
411 // Ran out of fixed array: skip.
412 d.value(reflect.Value{})
416 // Next token must be , or ].
417 op = d.scanWhile(scanSkipSpace)
418 if op == scanEndArray {
419 break
421 if op != scanArrayValue {
422 d.error(errPhase)
426 if i < v.Len() {
427 if v.Kind() == reflect.Array {
428 // Array. Zero the rest.
429 z := reflect.Zero(v.Type().Elem())
430 for ; i < v.Len(); i++ {
431 v.Index(i).Set(z)
433 } else {
434 v.SetLen(i)
437 if i == 0 && v.Kind() == reflect.Slice {
438 v.Set(reflect.MakeSlice(v.Type(), 0, 0))
442 // object consumes an object from d.data[d.off-1:], decoding into the value v.
443 // the first byte of the object ('{') has been read already.
444 func (d *decodeState) object(v reflect.Value) {
445 // Check for unmarshaler.
446 u, ut, pv := d.indirect(v, false)
447 if u != nil {
448 d.off--
449 err := u.UnmarshalJSON(d.next())
450 if err != nil {
451 d.error(err)
453 return
455 if ut != nil {
456 d.saveError(&UnmarshalTypeError{"object", v.Type()})
457 d.off--
458 d.next() // skip over { } in input
459 return
461 v = pv
463 // Decoding into nil interface? Switch to non-reflect code.
464 if v.Kind() == reflect.Interface && v.NumMethod() == 0 {
465 v.Set(reflect.ValueOf(d.objectInterface()))
466 return
469 // Check type of target: struct or map[string]T
470 switch v.Kind() {
471 case reflect.Map:
472 // map must have string kind
473 t := v.Type()
474 if t.Key().Kind() != reflect.String {
475 d.saveError(&UnmarshalTypeError{"object", v.Type()})
476 break
478 if v.IsNil() {
479 v.Set(reflect.MakeMap(t))
481 case reflect.Struct:
483 default:
484 d.saveError(&UnmarshalTypeError{"object", v.Type()})
485 d.off--
486 d.next() // skip over { } in input
487 return
490 var mapElem reflect.Value
492 for {
493 // Read opening " of string key or closing }.
494 op := d.scanWhile(scanSkipSpace)
495 if op == scanEndObject {
496 // closing } - can only happen on first iteration.
497 break
499 if op != scanBeginLiteral {
500 d.error(errPhase)
503 // Read key.
504 start := d.off - 1
505 op = d.scanWhile(scanContinue)
506 item := d.data[start : d.off-1]
507 key, ok := unquoteBytes(item)
508 if !ok {
509 d.error(errPhase)
512 // Figure out field corresponding to key.
513 var subv reflect.Value
514 destring := false // whether the value is wrapped in a string to be decoded first
516 if v.Kind() == reflect.Map {
517 elemType := v.Type().Elem()
518 if !mapElem.IsValid() {
519 mapElem = reflect.New(elemType).Elem()
520 } else {
521 mapElem.Set(reflect.Zero(elemType))
523 subv = mapElem
524 } else {
525 var f *field
526 fields := cachedTypeFields(v.Type())
527 for i := range fields {
528 ff := &fields[i]
529 if bytes.Equal(ff.nameBytes, key) {
530 f = ff
531 break
533 if f == nil && ff.equalFold(ff.nameBytes, key) {
534 f = ff
537 if f != nil {
538 subv = v
539 destring = f.quoted
540 for _, i := range f.index {
541 if subv.Kind() == reflect.Ptr {
542 if subv.IsNil() {
543 subv.Set(reflect.New(subv.Type().Elem()))
545 subv = subv.Elem()
547 subv = subv.Field(i)
552 // Read : before value.
553 if op == scanSkipSpace {
554 op = d.scanWhile(scanSkipSpace)
556 if op != scanObjectKey {
557 d.error(errPhase)
560 // Read value.
561 if destring {
562 d.value(reflect.ValueOf(&d.tempstr))
563 d.literalStore([]byte(d.tempstr), subv, true)
564 d.tempstr = "" // Zero scratch space for successive values.
565 } else {
566 d.value(subv)
569 // Write value back to map;
570 // if using struct, subv points into struct already.
571 if v.Kind() == reflect.Map {
572 kv := reflect.ValueOf(key).Convert(v.Type().Key())
573 v.SetMapIndex(kv, subv)
576 // Next token must be , or }.
577 op = d.scanWhile(scanSkipSpace)
578 if op == scanEndObject {
579 break
581 if op != scanObjectValue {
582 d.error(errPhase)
587 // literal consumes a literal from d.data[d.off-1:], decoding into the value v.
588 // The first byte of the literal has been read already
589 // (that's how the caller knows it's a literal).
590 func (d *decodeState) literal(v reflect.Value) {
591 // All bytes inside literal return scanContinue op code.
592 start := d.off - 1
593 op := d.scanWhile(scanContinue)
595 // Scan read one byte too far; back up.
596 d.off--
597 d.scan.undo(op)
599 d.literalStore(d.data[start:d.off], v, false)
602 // convertNumber converts the number literal s to a float64 or a Number
603 // depending on the setting of d.useNumber.
604 func (d *decodeState) convertNumber(s string) (interface{}, error) {
605 if d.useNumber {
606 return Number(s), nil
608 f, err := strconv.ParseFloat(s, 64)
609 if err != nil {
610 return nil, &UnmarshalTypeError{"number " + s, reflect.TypeOf(0.0)}
612 return f, nil
615 var numberType = reflect.TypeOf(Number(""))
617 // literalStore decodes a literal stored in item into v.
619 // fromQuoted indicates whether this literal came from unwrapping a
620 // string from the ",string" struct tag option. this is used only to
621 // produce more helpful error messages.
622 func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool) {
623 // Check for unmarshaler.
624 if len(item) == 0 {
625 //Empty string given
626 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
627 return
629 wantptr := item[0] == 'n' // null
630 u, ut, pv := d.indirect(v, wantptr)
631 if u != nil {
632 err := u.UnmarshalJSON(item)
633 if err != nil {
634 d.error(err)
636 return
638 if ut != nil {
639 if item[0] != '"' {
640 if fromQuoted {
641 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
642 } else {
643 d.saveError(&UnmarshalTypeError{"string", v.Type()})
646 s, ok := unquoteBytes(item)
647 if !ok {
648 if fromQuoted {
649 d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
650 } else {
651 d.error(errPhase)
654 err := ut.UnmarshalText(s)
655 if err != nil {
656 d.error(err)
658 return
661 v = pv
663 switch c := item[0]; c {
664 case 'n': // null
665 switch v.Kind() {
666 case reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice:
667 v.Set(reflect.Zero(v.Type()))
668 // otherwise, ignore null for primitives/string
670 case 't', 'f': // true, false
671 value := c == 't'
672 switch v.Kind() {
673 default:
674 if fromQuoted {
675 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
676 } else {
677 d.saveError(&UnmarshalTypeError{"bool", v.Type()})
679 case reflect.Bool:
680 v.SetBool(value)
681 case reflect.Interface:
682 if v.NumMethod() == 0 {
683 v.Set(reflect.ValueOf(value))
684 } else {
685 d.saveError(&UnmarshalTypeError{"bool", v.Type()})
689 case '"': // string
690 s, ok := unquoteBytes(item)
691 if !ok {
692 if fromQuoted {
693 d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
694 } else {
695 d.error(errPhase)
698 switch v.Kind() {
699 default:
700 d.saveError(&UnmarshalTypeError{"string", v.Type()})
701 case reflect.Slice:
702 if v.Type() != byteSliceType {
703 d.saveError(&UnmarshalTypeError{"string", v.Type()})
704 break
706 b := make([]byte, base64.StdEncoding.DecodedLen(len(s)))
707 n, err := base64.StdEncoding.Decode(b, s)
708 if err != nil {
709 d.saveError(err)
710 break
712 v.Set(reflect.ValueOf(b[0:n]))
713 case reflect.String:
714 v.SetString(string(s))
715 case reflect.Interface:
716 if v.NumMethod() == 0 {
717 v.Set(reflect.ValueOf(string(s)))
718 } else {
719 d.saveError(&UnmarshalTypeError{"string", v.Type()})
723 default: // number
724 if c != '-' && (c < '0' || c > '9') {
725 if fromQuoted {
726 d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
727 } else {
728 d.error(errPhase)
731 s := string(item)
732 switch v.Kind() {
733 default:
734 if v.Kind() == reflect.String && v.Type() == numberType {
735 v.SetString(s)
736 break
738 if fromQuoted {
739 d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
740 } else {
741 d.error(&UnmarshalTypeError{"number", v.Type()})
743 case reflect.Interface:
744 n, err := d.convertNumber(s)
745 if err != nil {
746 d.saveError(err)
747 break
749 if v.NumMethod() != 0 {
750 d.saveError(&UnmarshalTypeError{"number", v.Type()})
751 break
753 v.Set(reflect.ValueOf(n))
755 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
756 n, err := strconv.ParseInt(s, 10, 64)
757 if err != nil || v.OverflowInt(n) {
758 d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
759 break
761 v.SetInt(n)
763 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
764 n, err := strconv.ParseUint(s, 10, 64)
765 if err != nil || v.OverflowUint(n) {
766 d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
767 break
769 v.SetUint(n)
771 case reflect.Float32, reflect.Float64:
772 n, err := strconv.ParseFloat(s, v.Type().Bits())
773 if err != nil || v.OverflowFloat(n) {
774 d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
775 break
777 v.SetFloat(n)
782 // The xxxInterface routines build up a value to be stored
783 // in an empty interface. They are not strictly necessary,
784 // but they avoid the weight of reflection in this common case.
786 // valueInterface is like value but returns interface{}
787 func (d *decodeState) valueInterface() interface{} {
788 switch d.scanWhile(scanSkipSpace) {
789 default:
790 d.error(errPhase)
791 panic("unreachable")
792 case scanBeginArray:
793 return d.arrayInterface()
794 case scanBeginObject:
795 return d.objectInterface()
796 case scanBeginLiteral:
797 return d.literalInterface()
801 // arrayInterface is like array but returns []interface{}.
802 func (d *decodeState) arrayInterface() []interface{} {
803 var v = make([]interface{}, 0)
804 for {
805 // Look ahead for ] - can only happen on first iteration.
806 op := d.scanWhile(scanSkipSpace)
807 if op == scanEndArray {
808 break
811 // Back up so d.value can have the byte we just read.
812 d.off--
813 d.scan.undo(op)
815 v = append(v, d.valueInterface())
817 // Next token must be , or ].
818 op = d.scanWhile(scanSkipSpace)
819 if op == scanEndArray {
820 break
822 if op != scanArrayValue {
823 d.error(errPhase)
826 return v
829 // objectInterface is like object but returns map[string]interface{}.
830 func (d *decodeState) objectInterface() map[string]interface{} {
831 m := make(map[string]interface{})
832 for {
833 // Read opening " of string key or closing }.
834 op := d.scanWhile(scanSkipSpace)
835 if op == scanEndObject {
836 // closing } - can only happen on first iteration.
837 break
839 if op != scanBeginLiteral {
840 d.error(errPhase)
843 // Read string key.
844 start := d.off - 1
845 op = d.scanWhile(scanContinue)
846 item := d.data[start : d.off-1]
847 key, ok := unquote(item)
848 if !ok {
849 d.error(errPhase)
852 // Read : before value.
853 if op == scanSkipSpace {
854 op = d.scanWhile(scanSkipSpace)
856 if op != scanObjectKey {
857 d.error(errPhase)
860 // Read value.
861 m[key] = d.valueInterface()
863 // Next token must be , or }.
864 op = d.scanWhile(scanSkipSpace)
865 if op == scanEndObject {
866 break
868 if op != scanObjectValue {
869 d.error(errPhase)
872 return m
875 // literalInterface is like literal but returns an interface value.
876 func (d *decodeState) literalInterface() interface{} {
877 // All bytes inside literal return scanContinue op code.
878 start := d.off - 1
879 op := d.scanWhile(scanContinue)
881 // Scan read one byte too far; back up.
882 d.off--
883 d.scan.undo(op)
884 item := d.data[start:d.off]
886 switch c := item[0]; c {
887 case 'n': // null
888 return nil
890 case 't', 'f': // true, false
891 return c == 't'
893 case '"': // string
894 s, ok := unquote(item)
895 if !ok {
896 d.error(errPhase)
898 return s
900 default: // number
901 if c != '-' && (c < '0' || c > '9') {
902 d.error(errPhase)
904 n, err := d.convertNumber(string(item))
905 if err != nil {
906 d.saveError(err)
908 return n
912 // getu4 decodes \uXXXX from the beginning of s, returning the hex value,
913 // or it returns -1.
914 func getu4(s []byte) rune {
915 if len(s) < 6 || s[0] != '\\' || s[1] != 'u' {
916 return -1
918 r, err := strconv.ParseUint(string(s[2:6]), 16, 64)
919 if err != nil {
920 return -1
922 return rune(r)
925 // unquote converts a quoted JSON string literal s into an actual string t.
926 // The rules are different than for Go, so cannot use strconv.Unquote.
927 func unquote(s []byte) (t string, ok bool) {
928 s, ok = unquoteBytes(s)
929 t = string(s)
930 return
933 func unquoteBytes(s []byte) (t []byte, ok bool) {
934 if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' {
935 return
937 s = s[1 : len(s)-1]
939 // Check for unusual characters. If there are none,
940 // then no unquoting is needed, so return a slice of the
941 // original bytes.
942 r := 0
943 for r < len(s) {
944 c := s[r]
945 if c == '\\' || c == '"' || c < ' ' {
946 break
948 if c < utf8.RuneSelf {
950 continue
952 rr, size := utf8.DecodeRune(s[r:])
953 if rr == utf8.RuneError && size == 1 {
954 break
956 r += size
958 if r == len(s) {
959 return s, true
962 b := make([]byte, len(s)+2*utf8.UTFMax)
963 w := copy(b, s[0:r])
964 for r < len(s) {
965 // Out of room? Can only happen if s is full of
966 // malformed UTF-8 and we're replacing each
967 // byte with RuneError.
968 if w >= len(b)-2*utf8.UTFMax {
969 nb := make([]byte, (len(b)+utf8.UTFMax)*2)
970 copy(nb, b[0:w])
971 b = nb
973 switch c := s[r]; {
974 case c == '\\':
976 if r >= len(s) {
977 return
979 switch s[r] {
980 default:
981 return
982 case '"', '\\', '/', '\'':
983 b[w] = s[r]
986 case 'b':
987 b[w] = '\b'
990 case 'f':
991 b[w] = '\f'
994 case 'n':
995 b[w] = '\n'
998 case 'r':
999 b[w] = '\r'
1002 case 't':
1003 b[w] = '\t'
1006 case 'u':
1008 rr := getu4(s[r:])
1009 if rr < 0 {
1010 return
1012 r += 6
1013 if utf16.IsSurrogate(rr) {
1014 rr1 := getu4(s[r:])
1015 if dec := utf16.DecodeRune(rr, rr1); dec != unicode.ReplacementChar {
1016 // A valid pair; consume.
1017 r += 6
1018 w += utf8.EncodeRune(b[w:], dec)
1019 break
1021 // Invalid surrogate; fall back to replacement rune.
1022 rr = unicode.ReplacementChar
1024 w += utf8.EncodeRune(b[w:], rr)
1027 // Quote, control characters are invalid.
1028 case c == '"', c < ' ':
1029 return
1031 // ASCII
1032 case c < utf8.RuneSelf:
1033 b[w] = c
1037 // Coerce to well-formed UTF-8.
1038 default:
1039 rr, size := utf8.DecodeRune(s[r:])
1040 r += size
1041 w += utf8.EncodeRune(b[w:], rr)
1044 return b[0:w], true