LEFT | RIGHT |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Represents JSON data structure using native Go types: booleans, floats, | 5 // Represents JSON data structure using native Go types: booleans, floats, |
6 // strings, arrays, and maps. | 6 // strings, arrays, and maps. |
7 | 7 |
8 package json | 8 package json |
9 | 9 |
10 import ( | 10 import ( |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 err := checkValid(data, &d.scan) | 60 err := checkValid(data, &d.scan) |
61 if err != nil { | 61 if err != nil { |
62 return err | 62 return err |
63 } | 63 } |
64 | 64 |
65 return d.unmarshal(v) | 65 return d.unmarshal(v) |
66 } | 66 } |
67 | 67 |
68 // Unmarshaler is the interface implemented by objects | 68 // Unmarshaler is the interface implemented by objects |
69 // that can unmarshal a JSON description of themselves. | 69 // that can unmarshal a JSON description of themselves. |
70 // The input can be assumed to be a valid JSON object | 70 // The input can be assumed to be a valid encoding of |
71 // encoding. UnmarshalJSON must copy the JSON data | 71 // a JSON value. UnmarshalJSON must copy the JSON data |
72 // if it wishes to retain the data after returning. | 72 // if it wishes to retain the data after returning. |
73 type Unmarshaler interface { | 73 type Unmarshaler interface { |
74 UnmarshalJSON([]byte) error | 74 UnmarshalJSON([]byte) error |
75 } | 75 } |
76 | 76 |
77 // An UnmarshalTypeError describes a JSON value that was | 77 // An UnmarshalTypeError describes a JSON value that was |
78 // not appropriate for a value of a specific Go type. | 78 // not appropriate for a value of a specific Go type. |
79 type UnmarshalTypeError struct { | 79 type UnmarshalTypeError struct { |
80 Value string // description of JSON value - "bool", "array", "numb
er -5" | 80 Value string // description of JSON value - "bool", "array", "numb
er -5" |
81 Type reflect.Type // type of Go value it could not be assigned to | 81 Type reflect.Type // type of Go value it could not be assigned to |
(...skipping 527 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
609 err := unmarshaler.UnmarshalJSON(item) | 609 err := unmarshaler.UnmarshalJSON(item) |
610 if err != nil { | 610 if err != nil { |
611 d.error(err) | 611 d.error(err) |
612 } | 612 } |
613 return | 613 return |
614 } | 614 } |
615 v = pv | 615 v = pv |
616 | 616 |
617 switch c := item[0]; c { | 617 switch c := item[0]; c { |
618 case 'n': // null | 618 case 'n': // null |
619 » » v.Set(reflect.Zero(v.Type())) | 619 » » switch v.Kind() { |
620 | 620 » » case reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice: |
| 621 » » » v.Set(reflect.Zero(v.Type())) |
| 622 » » » // otherwise, ignore null for primitives/string |
| 623 » » } |
621 case 't', 'f': // true, false | 624 case 't', 'f': // true, false |
622 value := c == 't' | 625 value := c == 't' |
623 switch v.Kind() { | 626 switch v.Kind() { |
624 default: | 627 default: |
625 if fromQuoted { | 628 if fromQuoted { |
626 d.saveError(fmt.Errorf("json: invalid use of ,st
ring struct tag, trying to unmarshal %q into %v", item, v.Type())) | 629 d.saveError(fmt.Errorf("json: invalid use of ,st
ring struct tag, trying to unmarshal %q into %v", item, v.Type())) |
627 } else { | 630 } else { |
628 d.saveError(&UnmarshalTypeError{"bool", v.Type()
}) | 631 d.saveError(&UnmarshalTypeError{"bool", v.Type()
}) |
629 } | 632 } |
630 case reflect.Bool: | 633 case reflect.Bool: |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
976 | 979 |
977 // Coerce to well-formed UTF-8. | 980 // Coerce to well-formed UTF-8. |
978 default: | 981 default: |
979 rr, size := utf8.DecodeRune(s[r:]) | 982 rr, size := utf8.DecodeRune(s[r:]) |
980 r += size | 983 r += size |
981 w += utf8.EncodeRune(b[w:], rr) | 984 w += utf8.EncodeRune(b[w:], rr) |
982 } | 985 } |
983 } | 986 } |
984 return b[0:w], true | 987 return b[0:w], true |
985 } | 988 } |
LEFT | RIGHT |