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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 Type reflect.Type // type of Go value it could not be assigned to | 78 Type reflect.Type // type of Go value it could not be assigned to |
79 } | 79 } |
80 | 80 |
81 func (e *UnmarshalTypeError) String() string { | 81 func (e *UnmarshalTypeError) String() string { |
82 return "json: cannot unmarshal " + e.Value + " into Go value of type " +
e.Type.String() | 82 return "json: cannot unmarshal " + e.Value + " into Go value of type " +
e.Type.String() |
83 } | 83 } |
84 | 84 |
85 // An UnmarshalFieldError describes a JSON object key that | 85 // An UnmarshalFieldError describes a JSON object key that |
86 // led to an unexported (and therefore unwritable) struct field. | 86 // led to an unexported (and therefore unwritable) struct field. |
87 type UnmarshalFieldError struct { | 87 type UnmarshalFieldError struct { |
88 » Key string | 88 » Key string |
89 » Type *reflect.StructType | 89 » Type *reflect.StructType |
90 Field reflect.StructField | 90 Field reflect.StructField |
91 } | 91 } |
92 | 92 |
93 func (e *UnmarshalFieldError) String() string { | 93 func (e *UnmarshalFieldError) String() string { |
94 return "json: cannot unmarshal object key " + strconv.Quote(e.Key) + " i
nto unexported field " + e.Field.Name + " of type " + e.Type.String() | 94 return "json: cannot unmarshal object key " + strconv.Quote(e.Key) + " i
nto unexported field " + e.Field.Name + " of type " + e.Type.String() |
95 } | 95 } |
96 | 96 |
97 // An InvalidUnmarshalError describes an invalid argument passed to Unmarshal. | 97 // An InvalidUnmarshalError describes an invalid argument passed to Unmarshal. |
98 // (The argument to Unmarshal must be a non-nil pointer.) | 98 // (The argument to Unmarshal must be a non-nil pointer.) |
99 type InvalidUnmarshalError struct { | 99 type InvalidUnmarshalError struct { |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
474 } | 474 } |
475 } | 475 } |
476 if !ok { | 476 if !ok { |
477 // Second, exact match. | 477 // Second, exact match. |
478 f, ok = st.FieldByName(key) | 478 f, ok = st.FieldByName(key) |
479 } | 479 } |
480 if !ok { | 480 if !ok { |
481 // Third, case-insensitive match. | 481 // Third, case-insensitive match. |
482 f, ok = st.FieldByNameFunc(func(s string) bool {
return matchName(key, s) }) | 482 f, ok = st.FieldByNameFunc(func(s string) bool {
return matchName(key, s) }) |
483 } | 483 } |
484 » » » | 484 |
485 // Extract value; name must be exported. | 485 // Extract value; name must be exported. |
486 if ok { | 486 if ok { |
487 if f.PkgPath != "" { | 487 if f.PkgPath != "" { |
488 d.saveError(&UnmarshalFieldError{key, st
, f}) | 488 d.saveError(&UnmarshalFieldError{key, st
, f}) |
489 } else { | 489 } else { |
490 subv = sv.FieldByIndex(f.Index) | 490 subv = sv.FieldByIndex(f.Index) |
491 } | 491 } |
492 } | 492 } |
493 } | 493 } |
494 | 494 |
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
850 | 850 |
851 // Coerce to well-formed UTF-8. | 851 // Coerce to well-formed UTF-8. |
852 default: | 852 default: |
853 rune, size := utf8.DecodeRune(s[r:]) | 853 rune, size := utf8.DecodeRune(s[r:]) |
854 r += size | 854 r += size |
855 w += utf8.EncodeRune(rune, b[w:]) | 855 w += utf8.EncodeRune(rune, b[w:]) |
856 } | 856 } |
857 } | 857 } |
858 return string(b[0:w]), true | 858 return string(b[0:w]), true |
859 } | 859 } |
LEFT | RIGHT |