OSDN Git Service

Update Go library to release r60.1.
[pf3gnuchains/gcc-fork.git] / libgo / go / json / decode.go
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         "encoding/base64"
12         "os"
13         "reflect"
14         "runtime"
15         "strconv"
16         "strings"
17         "unicode"
18         "utf16"
19         "utf8"
20 )
21
22 // Unmarshal parses the JSON-encoded data and stores the result
23 // in the value pointed to by v.
24 //
25 // Unmarshal traverses the value v recursively.
26 // If an encountered value implements the Unmarshaler interface,
27 // Unmarshal calls its UnmarshalJSON method with a well-formed
28 // JSON encoding.
29 //
30 // Otherwise, Unmarshal uses the inverse of the encodings that
31 // Marshal uses, allocating maps, slices, and pointers as necessary,
32 // with the following additional rules:
33 //
34 // To unmarshal a JSON value into a nil interface value, the
35 // type stored in the interface value is one of:
36 //
37 //      bool, for JSON booleans
38 //      float64, for JSON numbers
39 //      string, for JSON strings
40 //      []interface{}, for JSON arrays
41 //      map[string]interface{}, for JSON objects
42 //      nil for JSON null
43 //
44 // If a JSON value is not appropriate for a given target type,
45 // or if a JSON number overflows the target type, Unmarshal
46 // skips that field and completes the unmarshalling as best it can.
47 // If no more serious errors are encountered, Unmarshal returns
48 // an UnmarshalTypeError describing the earliest such error.
49 //
50 func Unmarshal(data []byte, v interface{}) os.Error {
51         d := new(decodeState).init(data)
52
53         // Quick check for well-formedness.
54         // Avoids filling out half a data structure
55         // before discovering a JSON syntax error.
56         err := checkValid(data, &d.scan)
57         if err != nil {
58                 return err
59         }
60
61         return d.unmarshal(v)
62 }
63
64 // Unmarshaler is the interface implemented by objects
65 // that can unmarshal a JSON description of themselves.
66 // The input can be assumed to be a valid JSON object
67 // encoding.  UnmarshalJSON must copy the JSON data
68 // if it wishes to retain the data after returning.
69 type Unmarshaler interface {
70         UnmarshalJSON([]byte) os.Error
71 }
72
73 // An UnmarshalTypeError describes a JSON value that was
74 // not appropriate for a value of a specific Go type.
75 type UnmarshalTypeError struct {
76         Value string       // description of JSON value - "bool", "array", "number -5"
77         Type  reflect.Type // type of Go value it could not be assigned to
78 }
79
80 func (e *UnmarshalTypeError) String() string {
81         return "json: cannot unmarshal " + e.Value + " into Go value of type " + e.Type.String()
82 }
83
84 // An UnmarshalFieldError describes a JSON object key that
85 // led to an unexported (and therefore unwritable) struct field.
86 type UnmarshalFieldError struct {
87         Key   string
88         Type  reflect.Type
89         Field reflect.StructField
90 }
91
92 func (e *UnmarshalFieldError) String() string {
93         return "json: cannot unmarshal object key " + strconv.Quote(e.Key) + " into unexported field " + e.Field.Name + " of type " + e.Type.String()
94 }
95
96 // An InvalidUnmarshalError describes an invalid argument passed to Unmarshal.
97 // (The argument to Unmarshal must be a non-nil pointer.)
98 type InvalidUnmarshalError struct {
99         Type reflect.Type
100 }
101
102 func (e *InvalidUnmarshalError) String() string {
103         if e.Type == nil {
104                 return "json: Unmarshal(nil)"
105         }
106
107         if e.Type.Kind() != reflect.Ptr {
108                 return "json: Unmarshal(non-pointer " + e.Type.String() + ")"
109         }
110         return "json: Unmarshal(nil " + e.Type.String() + ")"
111 }
112
113 func (d *decodeState) unmarshal(v interface{}) (err os.Error) {
114         defer func() {
115                 if r := recover(); r != nil {
116                         if _, ok := r.(runtime.Error); ok {
117                                 panic(r)
118                         }
119                         err = r.(os.Error)
120                 }
121         }()
122
123         rv := reflect.ValueOf(v)
124         pv := rv
125         if pv.Kind() != reflect.Ptr || pv.IsNil() {
126                 return &InvalidUnmarshalError{reflect.TypeOf(v)}
127         }
128
129         d.scan.reset()
130         // We decode rv not pv.Elem because the Unmarshaler interface
131         // test must be applied at the top level of the value.
132         d.value(rv)
133         return d.savedError
134 }
135
136 // decodeState represents the state while decoding a JSON value.
137 type decodeState struct {
138         data       []byte
139         off        int // read offset in data
140         scan       scanner
141         nextscan   scanner // for calls to nextValue
142         savedError os.Error
143         tempstr    string // scratch space to avoid some allocations
144 }
145
146 // errPhase is used for errors that should not happen unless
147 // there is a bug in the JSON decoder or something is editing
148 // the data slice while the decoder executes.
149 var errPhase = os.NewError("JSON decoder out of sync - data changing underfoot?")
150
151 func (d *decodeState) init(data []byte) *decodeState {
152         d.data = data
153         d.off = 0
154         d.savedError = nil
155         return d
156 }
157
158 // error aborts the decoding by panicking with err.
159 func (d *decodeState) error(err os.Error) {
160         panic(err)
161 }
162
163 // saveError saves the first err it is called with,
164 // for reporting at the end of the unmarshal.
165 func (d *decodeState) saveError(err os.Error) {
166         if d.savedError == nil {
167                 d.savedError = err
168         }
169 }
170
171 // next cuts off and returns the next full JSON value in d.data[d.off:].
172 // The next value is known to be an object or array, not a literal.
173 func (d *decodeState) next() []byte {
174         c := d.data[d.off]
175         item, rest, err := nextValue(d.data[d.off:], &d.nextscan)
176         if err != nil {
177                 d.error(err)
178         }
179         d.off = len(d.data) - len(rest)
180
181         // Our scanner has seen the opening brace/bracket
182         // and thinks we're still in the middle of the object.
183         // invent a closing brace/bracket to get it out.
184         if c == '{' {
185                 d.scan.step(&d.scan, '}')
186         } else {
187                 d.scan.step(&d.scan, ']')
188         }
189
190         return item
191 }
192
193 // scanWhile processes bytes in d.data[d.off:] until it
194 // receives a scan code not equal to op.
195 // It updates d.off and returns the new scan code.
196 func (d *decodeState) scanWhile(op int) int {
197         var newOp int
198         for {
199                 if d.off >= len(d.data) {
200                         newOp = d.scan.eof()
201                         d.off = len(d.data) + 1 // mark processed EOF with len+1
202                 } else {
203                         c := int(d.data[d.off])
204                         d.off++
205                         newOp = d.scan.step(&d.scan, c)
206                 }
207                 if newOp != op {
208                         break
209                 }
210         }
211         return newOp
212 }
213
214 // value decodes a JSON value from d.data[d.off:] into the value.
215 // it updates d.off to point past the decoded value.
216 func (d *decodeState) value(v reflect.Value) {
217         if !v.IsValid() {
218                 _, rest, err := nextValue(d.data[d.off:], &d.nextscan)
219                 if err != nil {
220                         d.error(err)
221                 }
222                 d.off = len(d.data) - len(rest)
223
224                 // d.scan thinks we're still at the beginning of the item.
225                 // Feed in an empty string - the shortest, simplest value -
226                 // so that it knows we got to the end of the value.
227                 if d.scan.step == stateRedo {
228                         panic("redo")
229                 }
230                 d.scan.step(&d.scan, '"')
231                 d.scan.step(&d.scan, '"')
232                 return
233         }
234
235         switch op := d.scanWhile(scanSkipSpace); op {
236         default:
237                 d.error(errPhase)
238
239         case scanBeginArray:
240                 d.array(v)
241
242         case scanBeginObject:
243                 d.object(v)
244
245         case scanBeginLiteral:
246                 d.literal(v)
247         }
248 }
249
250 // indirect walks down v allocating pointers as needed,
251 // until it gets to a non-pointer.
252 // if it encounters an Unmarshaler, indirect stops and returns that.
253 // if wantptr is true, indirect stops at the last pointer.
254 func (d *decodeState) indirect(v reflect.Value, wantptr bool) (Unmarshaler, reflect.Value) {
255         // If v is a named type and is addressable,
256         // start with its address, so that if the type has pointer methods,
257         // we find them.
258         if v.Kind() != reflect.Ptr && v.Type().Name() != "" && v.CanAddr() {
259                 v = v.Addr()
260         }
261         for {
262                 var isUnmarshaler bool
263                 if v.Type().NumMethod() > 0 {
264                         // Remember that this is an unmarshaler,
265                         // but wait to return it until after allocating
266                         // the pointer (if necessary).
267                         _, isUnmarshaler = v.Interface().(Unmarshaler)
268                 }
269
270                 if iv := v; iv.Kind() == reflect.Interface && !iv.IsNil() {
271                         v = iv.Elem()
272                         continue
273                 }
274
275                 pv := v
276                 if pv.Kind() != reflect.Ptr {
277                         break
278                 }
279
280                 if pv.Elem().Kind() != reflect.Ptr && wantptr && pv.CanSet() && !isUnmarshaler {
281                         return nil, pv
282                 }
283                 if pv.IsNil() {
284                         pv.Set(reflect.New(pv.Type().Elem()))
285                 }
286                 if isUnmarshaler {
287                         // Using v.Interface().(Unmarshaler)
288                         // here means that we have to use a pointer
289                         // as the struct field.  We cannot use a value inside
290                         // a pointer to a struct, because in that case
291                         // v.Interface() is the value (x.f) not the pointer (&x.f).
292                         // This is an unfortunate consequence of reflect.
293                         // An alternative would be to look up the
294                         // UnmarshalJSON method and return a FuncValue.
295                         return v.Interface().(Unmarshaler), reflect.Value{}
296                 }
297                 v = pv.Elem()
298         }
299         return nil, v
300 }
301
302 // array consumes an array from d.data[d.off-1:], decoding into the value v.
303 // the first byte of the array ('[') has been read already.
304 func (d *decodeState) array(v reflect.Value) {
305         // Check for unmarshaler.
306         unmarshaler, pv := d.indirect(v, false)
307         if unmarshaler != nil {
308                 d.off--
309                 err := unmarshaler.UnmarshalJSON(d.next())
310                 if err != nil {
311                         d.error(err)
312                 }
313                 return
314         }
315         v = pv
316
317         // Decoding into nil interface?  Switch to non-reflect code.
318         iv := v
319         ok := iv.Kind() == reflect.Interface
320         if ok {
321                 iv.Set(reflect.ValueOf(d.arrayInterface()))
322                 return
323         }
324
325         // Check type of target.
326         av := v
327         if av.Kind() != reflect.Array && av.Kind() != reflect.Slice {
328                 d.saveError(&UnmarshalTypeError{"array", v.Type()})
329                 d.off--
330                 d.next()
331                 return
332         }
333
334         sv := v
335
336         i := 0
337         for {
338                 // Look ahead for ] - can only happen on first iteration.
339                 op := d.scanWhile(scanSkipSpace)
340                 if op == scanEndArray {
341                         break
342                 }
343
344                 // Back up so d.value can have the byte we just read.
345                 d.off--
346                 d.scan.undo(op)
347
348                 // Get element of array, growing if necessary.
349                 if i >= av.Cap() && sv.IsValid() {
350                         newcap := sv.Cap() + sv.Cap()/2
351                         if newcap < 4 {
352                                 newcap = 4
353                         }
354                         newv := reflect.MakeSlice(sv.Type(), sv.Len(), newcap)
355                         reflect.Copy(newv, sv)
356                         sv.Set(newv)
357                 }
358                 if i >= av.Len() && sv.IsValid() {
359                         // Must be slice; gave up on array during i >= av.Cap().
360                         sv.SetLen(i + 1)
361                 }
362
363                 // Decode into element.
364                 if i < av.Len() {
365                         d.value(av.Index(i))
366                 } else {
367                         // Ran out of fixed array: skip.
368                         d.value(reflect.Value{})
369                 }
370                 i++
371
372                 // Next token must be , or ].
373                 op = d.scanWhile(scanSkipSpace)
374                 if op == scanEndArray {
375                         break
376                 }
377                 if op != scanArrayValue {
378                         d.error(errPhase)
379                 }
380         }
381         if i < av.Len() {
382                 if !sv.IsValid() {
383                         // Array.  Zero the rest.
384                         z := reflect.Zero(av.Type().Elem())
385                         for ; i < av.Len(); i++ {
386                                 av.Index(i).Set(z)
387                         }
388                 } else {
389                         sv.SetLen(i)
390                 }
391         }
392 }
393
394 // matchName returns true if key should be written to a field named name.
395 func matchName(key, name string) bool {
396         return strings.ToLower(key) == strings.ToLower(name)
397 }
398
399 // object consumes an object from d.data[d.off-1:], decoding into the value v.
400 // the first byte of the object ('{') has been read already.
401 func (d *decodeState) object(v reflect.Value) {
402         // Check for unmarshaler.
403         unmarshaler, pv := d.indirect(v, false)
404         if unmarshaler != nil {
405                 d.off--
406                 err := unmarshaler.UnmarshalJSON(d.next())
407                 if err != nil {
408                         d.error(err)
409                 }
410                 return
411         }
412         v = pv
413
414         // Decoding into nil interface?  Switch to non-reflect code.
415         iv := v
416         if iv.Kind() == reflect.Interface {
417                 iv.Set(reflect.ValueOf(d.objectInterface()))
418                 return
419         }
420
421         // Check type of target: struct or map[string]T
422         var (
423                 mv reflect.Value
424                 sv reflect.Value
425         )
426         switch v.Kind() {
427         case reflect.Map:
428                 // map must have string type
429                 t := v.Type()
430                 if t.Key() != reflect.TypeOf("") {
431                         d.saveError(&UnmarshalTypeError{"object", v.Type()})
432                         break
433                 }
434                 mv = v
435                 if mv.IsNil() {
436                         mv.Set(reflect.MakeMap(t))
437                 }
438         case reflect.Struct:
439                 sv = v
440         default:
441                 d.saveError(&UnmarshalTypeError{"object", v.Type()})
442         }
443
444         if !mv.IsValid() && !sv.IsValid() {
445                 d.off--
446                 d.next() // skip over { } in input
447                 return
448         }
449
450         var mapElem reflect.Value
451
452         for {
453                 // Read opening " of string key or closing }.
454                 op := d.scanWhile(scanSkipSpace)
455                 if op == scanEndObject {
456                         // closing } - can only happen on first iteration.
457                         break
458                 }
459                 if op != scanBeginLiteral {
460                         d.error(errPhase)
461                 }
462
463                 // Read string key.
464                 start := d.off - 1
465                 op = d.scanWhile(scanContinue)
466                 item := d.data[start : d.off-1]
467                 key, ok := unquote(item)
468                 if !ok {
469                         d.error(errPhase)
470                 }
471
472                 // Figure out field corresponding to key.
473                 var subv reflect.Value
474                 destring := false // whether the value is wrapped in a string to be decoded first
475
476                 if mv.IsValid() {
477                         elemType := mv.Type().Elem()
478                         if !mapElem.IsValid() {
479                                 mapElem = reflect.New(elemType).Elem()
480                         } else {
481                                 mapElem.Set(reflect.Zero(elemType))
482                         }
483                         subv = mapElem
484                 } else {
485                         var f reflect.StructField
486                         var ok bool
487                         st := sv.Type()
488                         // First try for field with that tag.
489                         if isValidTag(key) {
490                                 for i := 0; i < sv.NumField(); i++ {
491                                         f = st.Field(i)
492                                         tagName, _ := parseTag(f.Tag.Get("json"))
493                                         if tagName == key {
494                                                 ok = true
495                                                 break
496                                         }
497                                 }
498                         }
499                         if !ok {
500                                 // Second, exact match.
501                                 f, ok = st.FieldByName(key)
502                         }
503                         if !ok {
504                                 // Third, case-insensitive match.
505                                 f, ok = st.FieldByNameFunc(func(s string) bool { return matchName(key, s) })
506                         }
507
508                         // Extract value; name must be exported.
509                         if ok {
510                                 if f.PkgPath != "" {
511                                         d.saveError(&UnmarshalFieldError{key, st, f})
512                                 } else {
513                                         subv = sv.FieldByIndex(f.Index)
514                                 }
515                                 _, opts := parseTag(f.Tag.Get("json"))
516                                 destring = opts.Contains("string")
517                         }
518                 }
519
520                 // Read : before value.
521                 if op == scanSkipSpace {
522                         op = d.scanWhile(scanSkipSpace)
523                 }
524                 if op != scanObjectKey {
525                         d.error(errPhase)
526                 }
527
528                 // Read value.
529                 if destring {
530                         d.value(reflect.ValueOf(&d.tempstr))
531                         d.literalStore([]byte(d.tempstr), subv)
532                 } else {
533                         d.value(subv)
534                 }
535                 // Write value back to map;
536                 // if using struct, subv points into struct already.
537                 if mv.IsValid() {
538                         mv.SetMapIndex(reflect.ValueOf(key), subv)
539                 }
540
541                 // Next token must be , or }.
542                 op = d.scanWhile(scanSkipSpace)
543                 if op == scanEndObject {
544                         break
545                 }
546                 if op != scanObjectValue {
547                         d.error(errPhase)
548                 }
549         }
550 }
551
552 // literal consumes a literal from d.data[d.off-1:], decoding into the value v.
553 // The first byte of the literal has been read already
554 // (that's how the caller knows it's a literal).
555 func (d *decodeState) literal(v reflect.Value) {
556         // All bytes inside literal return scanContinue op code.
557         start := d.off - 1
558         op := d.scanWhile(scanContinue)
559
560         // Scan read one byte too far; back up.
561         d.off--
562         d.scan.undo(op)
563
564         d.literalStore(d.data[start:d.off], v)
565 }
566
567 // literalStore decodes a literal stored in item into v.
568 func (d *decodeState) literalStore(item []byte, v reflect.Value) {
569         // Check for unmarshaler.
570         wantptr := item[0] == 'n' // null
571         unmarshaler, pv := d.indirect(v, wantptr)
572         if unmarshaler != nil {
573                 err := unmarshaler.UnmarshalJSON(item)
574                 if err != nil {
575                         d.error(err)
576                 }
577                 return
578         }
579         v = pv
580
581         switch c := item[0]; c {
582         case 'n': // null
583                 switch v.Kind() {
584                 default:
585                         d.saveError(&UnmarshalTypeError{"null", v.Type()})
586                 case reflect.Interface, reflect.Ptr, reflect.Map:
587                         v.Set(reflect.Zero(v.Type()))
588                 }
589
590         case 't', 'f': // true, false
591                 value := c == 't'
592                 switch v.Kind() {
593                 default:
594                         d.saveError(&UnmarshalTypeError{"bool", v.Type()})
595                 case reflect.Bool:
596                         v.SetBool(value)
597                 case reflect.Interface:
598                         v.Set(reflect.ValueOf(value))
599                 }
600
601         case '"': // string
602                 s, ok := unquoteBytes(item)
603                 if !ok {
604                         d.error(errPhase)
605                 }
606                 switch v.Kind() {
607                 default:
608                         d.saveError(&UnmarshalTypeError{"string", v.Type()})
609                 case reflect.Slice:
610                         if v.Type() != byteSliceType {
611                                 d.saveError(&UnmarshalTypeError{"string", v.Type()})
612                                 break
613                         }
614                         b := make([]byte, base64.StdEncoding.DecodedLen(len(s)))
615                         n, err := base64.StdEncoding.Decode(b, s)
616                         if err != nil {
617                                 d.saveError(err)
618                                 break
619                         }
620                         v.Set(reflect.ValueOf(b[0:n]))
621                 case reflect.String:
622                         v.SetString(string(s))
623                 case reflect.Interface:
624                         v.Set(reflect.ValueOf(string(s)))
625                 }
626
627         default: // number
628                 if c != '-' && (c < '0' || c > '9') {
629                         d.error(errPhase)
630                 }
631                 s := string(item)
632                 switch v.Kind() {
633                 default:
634                         d.error(&UnmarshalTypeError{"number", v.Type()})
635                 case reflect.Interface:
636                         n, err := strconv.Atof64(s)
637                         if err != nil {
638                                 d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
639                                 break
640                         }
641                         v.Set(reflect.ValueOf(n))
642
643                 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
644                         n, err := strconv.Atoi64(s)
645                         if err != nil || v.OverflowInt(n) {
646                                 d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
647                                 break
648                         }
649                         v.SetInt(n)
650
651                 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
652                         n, err := strconv.Atoui64(s)
653                         if err != nil || v.OverflowUint(n) {
654                                 d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
655                                 break
656                         }
657                         v.SetUint(n)
658
659                 case reflect.Float32, reflect.Float64:
660                         n, err := strconv.AtofN(s, v.Type().Bits())
661                         if err != nil || v.OverflowFloat(n) {
662                                 d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
663                                 break
664                         }
665                         v.SetFloat(n)
666                 }
667         }
668 }
669
670 // The xxxInterface routines build up a value to be stored
671 // in an empty interface.  They are not strictly necessary,
672 // but they avoid the weight of reflection in this common case.
673
674 // valueInterface is like value but returns interface{}
675 func (d *decodeState) valueInterface() interface{} {
676         switch d.scanWhile(scanSkipSpace) {
677         default:
678                 d.error(errPhase)
679         case scanBeginArray:
680                 return d.arrayInterface()
681         case scanBeginObject:
682                 return d.objectInterface()
683         case scanBeginLiteral:
684                 return d.literalInterface()
685         }
686         panic("unreachable")
687 }
688
689 // arrayInterface is like array but returns []interface{}.
690 func (d *decodeState) arrayInterface() []interface{} {
691         var v []interface{}
692         for {
693                 // Look ahead for ] - can only happen on first iteration.
694                 op := d.scanWhile(scanSkipSpace)
695                 if op == scanEndArray {
696                         break
697                 }
698
699                 // Back up so d.value can have the byte we just read.
700                 d.off--
701                 d.scan.undo(op)
702
703                 v = append(v, d.valueInterface())
704
705                 // Next token must be , or ].
706                 op = d.scanWhile(scanSkipSpace)
707                 if op == scanEndArray {
708                         break
709                 }
710                 if op != scanArrayValue {
711                         d.error(errPhase)
712                 }
713         }
714         return v
715 }
716
717 // objectInterface is like object but returns map[string]interface{}.
718 func (d *decodeState) objectInterface() map[string]interface{} {
719         m := make(map[string]interface{})
720         for {
721                 // Read opening " of string key or closing }.
722                 op := d.scanWhile(scanSkipSpace)
723                 if op == scanEndObject {
724                         // closing } - can only happen on first iteration.
725                         break
726                 }
727                 if op != scanBeginLiteral {
728                         d.error(errPhase)
729                 }
730
731                 // Read string key.
732                 start := d.off - 1
733                 op = d.scanWhile(scanContinue)
734                 item := d.data[start : d.off-1]
735                 key, ok := unquote(item)
736                 if !ok {
737                         d.error(errPhase)
738                 }
739
740                 // Read : before value.
741                 if op == scanSkipSpace {
742                         op = d.scanWhile(scanSkipSpace)
743                 }
744                 if op != scanObjectKey {
745                         d.error(errPhase)
746                 }
747
748                 // Read value.
749                 m[key] = d.valueInterface()
750
751                 // Next token must be , or }.
752                 op = d.scanWhile(scanSkipSpace)
753                 if op == scanEndObject {
754                         break
755                 }
756                 if op != scanObjectValue {
757                         d.error(errPhase)
758                 }
759         }
760         return m
761 }
762
763 // literalInterface is like literal but returns an interface value.
764 func (d *decodeState) literalInterface() interface{} {
765         // All bytes inside literal return scanContinue op code.
766         start := d.off - 1
767         op := d.scanWhile(scanContinue)
768
769         // Scan read one byte too far; back up.
770         d.off--
771         d.scan.undo(op)
772         item := d.data[start:d.off]
773
774         switch c := item[0]; c {
775         case 'n': // null
776                 return nil
777
778         case 't', 'f': // true, false
779                 return c == 't'
780
781         case '"': // string
782                 s, ok := unquote(item)
783                 if !ok {
784                         d.error(errPhase)
785                 }
786                 return s
787
788         default: // number
789                 if c != '-' && (c < '0' || c > '9') {
790                         d.error(errPhase)
791                 }
792                 n, err := strconv.Atof64(string(item))
793                 if err != nil {
794                         d.saveError(&UnmarshalTypeError{"number " + string(item), reflect.TypeOf(0.0)})
795                 }
796                 return n
797         }
798         panic("unreachable")
799 }
800
801 // getu4 decodes \uXXXX from the beginning of s, returning the hex value,
802 // or it returns -1.
803 func getu4(s []byte) int {
804         if len(s) < 6 || s[0] != '\\' || s[1] != 'u' {
805                 return -1
806         }
807         rune, err := strconv.Btoui64(string(s[2:6]), 16)
808         if err != nil {
809                 return -1
810         }
811         return int(rune)
812 }
813
814 // unquote converts a quoted JSON string literal s into an actual string t.
815 // The rules are different than for Go, so cannot use strconv.Unquote.
816 func unquote(s []byte) (t string, ok bool) {
817         s, ok = unquoteBytes(s)
818         t = string(s)
819         return
820 }
821
822 func unquoteBytes(s []byte) (t []byte, ok bool) {
823         if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' {
824                 return
825         }
826         s = s[1 : len(s)-1]
827
828         // Check for unusual characters. If there are none,
829         // then no unquoting is needed, so return a slice of the
830         // original bytes.
831         r := 0
832         for r < len(s) {
833                 c := s[r]
834                 if c == '\\' || c == '"' || c < ' ' {
835                         break
836                 }
837                 if c < utf8.RuneSelf {
838                         r++
839                         continue
840                 }
841                 rune, size := utf8.DecodeRune(s[r:])
842                 if rune == utf8.RuneError && size == 1 {
843                         break
844                 }
845                 r += size
846         }
847         if r == len(s) {
848                 return s, true
849         }
850
851         b := make([]byte, len(s)+2*utf8.UTFMax)
852         w := copy(b, s[0:r])
853         for r < len(s) {
854                 // Out of room?  Can only happen if s is full of
855                 // malformed UTF-8 and we're replacing each
856                 // byte with RuneError.
857                 if w >= len(b)-2*utf8.UTFMax {
858                         nb := make([]byte, (len(b)+utf8.UTFMax)*2)
859                         copy(nb, b[0:w])
860                         b = nb
861                 }
862                 switch c := s[r]; {
863                 case c == '\\':
864                         r++
865                         if r >= len(s) {
866                                 return
867                         }
868                         switch s[r] {
869                         default:
870                                 return
871                         case '"', '\\', '/', '\'':
872                                 b[w] = s[r]
873                                 r++
874                                 w++
875                         case 'b':
876                                 b[w] = '\b'
877                                 r++
878                                 w++
879                         case 'f':
880                                 b[w] = '\f'
881                                 r++
882                                 w++
883                         case 'n':
884                                 b[w] = '\n'
885                                 r++
886                                 w++
887                         case 'r':
888                                 b[w] = '\r'
889                                 r++
890                                 w++
891                         case 't':
892                                 b[w] = '\t'
893                                 r++
894                                 w++
895                         case 'u':
896                                 r--
897                                 rune := getu4(s[r:])
898                                 if rune < 0 {
899                                         return
900                                 }
901                                 r += 6
902                                 if utf16.IsSurrogate(rune) {
903                                         rune1 := getu4(s[r:])
904                                         if dec := utf16.DecodeRune(rune, rune1); dec != unicode.ReplacementChar {
905                                                 // A valid pair; consume.
906                                                 r += 6
907                                                 w += utf8.EncodeRune(b[w:], dec)
908                                                 break
909                                         }
910                                         // Invalid surrogate; fall back to replacement rune.
911                                         rune = unicode.ReplacementChar
912                                 }
913                                 w += utf8.EncodeRune(b[w:], rune)
914                         }
915
916                 // Quote, control characters are invalid.
917                 case c == '"', c < ' ':
918                         return
919
920                 // ASCII
921                 case c < utf8.RuneSelf:
922                         b[w] = c
923                         r++
924                         w++
925
926                 // Coerce to well-formed UTF-8.
927                 default:
928                         rune, size := utf8.DecodeRune(s[r:])
929                         r += size
930                         w += utf8.EncodeRune(b[w:], rune)
931                 }
932         }
933         return b[0:w], true
934 }