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.
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
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 // The JSON null value unmarshals into an interface, map, pointer, or slice
58 // by setting that Go value to nil. Because null is often used in JSON to mean
59 // ``not present,'' unmarshaling a JSON null into any other Go type has no effect
60 // on the value and produces no error.
62 // When unmarshaling quoted strings, invalid UTF-8 or
63 // invalid UTF-16 surrogate pairs are not treated as an error.
64 // Instead, they are replaced by the Unicode replacement
67 func Unmarshal(data
[]byte, v
interface{}) error
{
68 // Check for well-formedness.
69 // Avoids filling out half a data structure
70 // before discovering a JSON syntax error.
72 err
:= checkValid(data
, &d
.scan
)
81 // Unmarshaler is the interface implemented by objects
82 // that can unmarshal a JSON description of themselves.
83 // The input can be assumed to be a valid encoding of
84 // a JSON value. UnmarshalJSON must copy the JSON data
85 // if it wishes to retain the data after returning.
86 type Unmarshaler
interface {
87 UnmarshalJSON([]byte) error
90 // An UnmarshalTypeError describes a JSON value that was
91 // not appropriate for a value of a specific Go type.
92 type UnmarshalTypeError
struct {
93 Value
string // description of JSON value - "bool", "array", "number -5"
94 Type reflect
.Type
// type of Go value it could not be assigned to
97 func (e
*UnmarshalTypeError
) Error() string {
98 return "json: cannot unmarshal " + e
.Value
+ " into Go value of type " + e
.Type
.String()
101 // An UnmarshalFieldError describes a JSON object key that
102 // led to an unexported (and therefore unwritable) struct field.
103 // (No longer used; kept for compatibility.)
104 type UnmarshalFieldError
struct {
107 Field reflect
.StructField
110 func (e
*UnmarshalFieldError
) Error() string {
111 return "json: cannot unmarshal object key " + strconv
.Quote(e
.Key
) + " into unexported field " + e
.Field
.Name
+ " of type " + e
.Type
.String()
114 // An InvalidUnmarshalError describes an invalid argument passed to Unmarshal.
115 // (The argument to Unmarshal must be a non-nil pointer.)
116 type InvalidUnmarshalError
struct {
120 func (e
*InvalidUnmarshalError
) Error() string {
122 return "json: Unmarshal(nil)"
125 if e
.Type
.Kind() != reflect
.Ptr
{
126 return "json: Unmarshal(non-pointer " + e
.Type
.String() + ")"
128 return "json: Unmarshal(nil " + e
.Type
.String() + ")"
131 func (d
*decodeState
) unmarshal(v
interface{}) (err error
) {
133 if r
:= recover(); r
!= nil {
134 if _
, ok
:= r
.(runtime
.Error
); ok
{
141 rv
:= reflect
.ValueOf(v
)
142 if rv
.Kind() != reflect
.Ptr || rv
.IsNil() {
143 return &InvalidUnmarshalError
{reflect
.TypeOf(v
)}
147 // We decode rv not rv.Elem because the Unmarshaler interface
148 // test must be applied at the top level of the value.
153 // A Number represents a JSON number literal.
156 // String returns the literal text of the number.
157 func (n Number
) String() string { return string(n
) }
159 // Float64 returns the number as a float64.
160 func (n Number
) Float64() (float64, error
) {
161 return strconv
.ParseFloat(string(n
), 64)
164 // Int64 returns the number as an int64.
165 func (n Number
) Int64() (int64, error
) {
166 return strconv
.ParseInt(string(n
), 10, 64)
169 // decodeState represents the state while decoding a JSON value.
170 type decodeState
struct {
172 off
int // read offset in data
174 nextscan scanner
// for calls to nextValue
179 // errPhase is used for errors that should not happen unless
180 // there is a bug in the JSON decoder or something is editing
181 // the data slice while the decoder executes.
182 var errPhase
= errors
.New("JSON decoder out of sync - data changing underfoot?")
184 func (d
*decodeState
) init(data
[]byte) *decodeState
{
191 // error aborts the decoding by panicking with err.
192 func (d
*decodeState
) error(err error
) {
196 // saveError saves the first err it is called with,
197 // for reporting at the end of the unmarshal.
198 func (d
*decodeState
) saveError(err error
) {
199 if d
.savedError
== nil {
204 // next cuts off and returns the next full JSON value in d.data[d.off:].
205 // The next value is known to be an object or array, not a literal.
206 func (d
*decodeState
) next() []byte {
208 item
, rest
, err
:= nextValue(d
.data
[d
.off
:], &d
.nextscan
)
212 d
.off
= len(d
.data
) - len(rest
)
214 // Our scanner has seen the opening brace/bracket
215 // and thinks we're still in the middle of the object.
216 // invent a closing brace/bracket to get it out.
218 d
.scan
.step(&d
.scan
, '}')
220 d
.scan
.step(&d
.scan
, ']')
226 // scanWhile processes bytes in d.data[d.off:] until it
227 // receives a scan code not equal to op.
228 // It updates d.off and returns the new scan code.
229 func (d
*decodeState
) scanWhile(op
int) int {
232 if d
.off
>= len(d
.data
) {
234 d
.off
= len(d
.data
) + 1 // mark processed EOF with len+1
236 c
:= int(d
.data
[d
.off
])
238 newOp
= d
.scan
.step(&d
.scan
, c
)
247 // value decodes a JSON value from d.data[d.off:] into the value.
248 // it updates d.off to point past the decoded value.
249 func (d
*decodeState
) value(v reflect
.Value
) {
251 _
, rest
, err
:= nextValue(d
.data
[d
.off
:], &d
.nextscan
)
255 d
.off
= len(d
.data
) - len(rest
)
257 // d.scan thinks we're still at the beginning of the item.
258 // Feed in an empty string - the shortest, simplest value -
259 // so that it knows we got to the end of the value.
263 d
.scan
.step
= stateBeginValue
265 d
.scan
.step(&d
.scan
, '"')
266 d
.scan
.step(&d
.scan
, '"')
268 n
:= len(d
.scan
.parseState
)
269 if n
> 0 && d
.scan
.parseState
[n
-1] == parseObjectKey
{
270 // d.scan thinks we just read an object key; finish the object
271 d
.scan
.step(&d
.scan
, ':')
272 d
.scan
.step(&d
.scan
, '"')
273 d
.scan
.step(&d
.scan
, '"')
274 d
.scan
.step(&d
.scan
, '}')
280 switch op
:= d
.scanWhile(scanSkipSpace
); op
{
287 case scanBeginObject
:
290 case scanBeginLiteral
:
295 type unquotedValue
struct{}
297 // valueQuoted is like value but decodes a
298 // quoted string literal or literal null into an interface value.
299 // If it finds anything other than a quoted string literal or null,
300 // valueQuoted returns unquotedValue{}.
301 func (d
*decodeState
) valueQuoted() interface{} {
302 switch op
:= d
.scanWhile(scanSkipSpace
); op
{
307 d
.array(reflect
.Value
{})
309 case scanBeginObject
:
310 d
.object(reflect
.Value
{})
312 case scanBeginLiteral
:
313 switch v
:= d
.literalInterface().(type) {
318 return unquotedValue
{}
321 // indirect walks down v allocating pointers as needed,
322 // until it gets to a non-pointer.
323 // if it encounters an Unmarshaler, indirect stops and returns that.
324 // if decodingNull is true, indirect stops at the last pointer so it can be set to nil.
325 func (d
*decodeState
) indirect(v reflect
.Value
, decodingNull
bool) (Unmarshaler
, encoding
.TextUnmarshaler
, reflect
.Value
) {
326 // If v is a named type and is addressable,
327 // start with its address, so that if the type has pointer methods,
329 if v
.Kind() != reflect
.Ptr
&& v
.Type().Name() != "" && v
.CanAddr() {
333 // Load value from interface, but only if the result will be
334 // usefully addressable.
335 if v
.Kind() == reflect
.Interface
&& !v
.IsNil() {
337 if e
.Kind() == reflect
.Ptr
&& !e
.IsNil() && (!decodingNull || e
.Elem().Kind() == reflect
.Ptr
) {
343 if v
.Kind() != reflect
.Ptr
{
347 if v
.Elem().Kind() != reflect
.Ptr
&& decodingNull
&& v
.CanSet() {
351 v
.Set(reflect
.New(v
.Type().Elem()))
353 if v
.Type().NumMethod() > 0 {
354 if u
, ok
:= v
.Interface().(Unmarshaler
); ok
{
355 return u
, nil, reflect
.Value
{}
357 if u
, ok
:= v
.Interface().(encoding
.TextUnmarshaler
); ok
{
358 return nil, u
, reflect
.Value
{}
366 // array consumes an array from d.data[d.off-1:], decoding into the value v.
367 // the first byte of the array ('[') has been read already.
368 func (d
*decodeState
) array(v reflect
.Value
) {
369 // Check for unmarshaler.
370 u
, ut
, pv
:= d
.indirect(v
, false)
373 err
:= u
.UnmarshalJSON(d
.next())
380 d
.saveError(&UnmarshalTypeError
{"array", v
.Type()})
388 // Check type of target.
390 case reflect
.Interface
:
391 if v
.NumMethod() == 0 {
392 // Decoding into nil interface? Switch to non-reflect code.
393 v
.Set(reflect
.ValueOf(d
.arrayInterface()))
396 // Otherwise it's invalid.
399 d
.saveError(&UnmarshalTypeError
{"array", v
.Type()})
410 // Look ahead for ] - can only happen on first iteration.
411 op
:= d
.scanWhile(scanSkipSpace
)
412 if op
== scanEndArray
{
416 // Back up so d.value can have the byte we just read.
420 // Get element of array, growing if necessary.
421 if v
.Kind() == reflect
.Slice
{
422 // Grow slice if necessary
424 newcap
:= v
.Cap() + v
.Cap()/2
428 newv
:= reflect
.MakeSlice(v
.Type(), v
.Len(), newcap
)
429 reflect
.Copy(newv
, v
)
438 // Decode into element.
441 // Ran out of fixed array: skip.
442 d
.value(reflect
.Value
{})
446 // Next token must be , or ].
447 op
= d
.scanWhile(scanSkipSpace
)
448 if op
== scanEndArray
{
451 if op
!= scanArrayValue
{
457 if v
.Kind() == reflect
.Array
{
458 // Array. Zero the rest.
459 z
:= reflect
.Zero(v
.Type().Elem())
460 for ; i
< v
.Len(); i
++ {
467 if i
== 0 && v
.Kind() == reflect
.Slice
{
468 v
.Set(reflect
.MakeSlice(v
.Type(), 0, 0))
472 var nullLiteral
= []byte("null")
474 // object consumes an object from d.data[d.off-1:], decoding into the value v.
475 // the first byte ('{') of the object has been read already.
476 func (d
*decodeState
) object(v reflect
.Value
) {
477 // Check for unmarshaler.
478 u
, ut
, pv
:= d
.indirect(v
, false)
481 err
:= u
.UnmarshalJSON(d
.next())
488 d
.saveError(&UnmarshalTypeError
{"object", v
.Type()})
490 d
.next() // skip over { } in input
495 // Decoding into nil interface? Switch to non-reflect code.
496 if v
.Kind() == reflect
.Interface
&& v
.NumMethod() == 0 {
497 v
.Set(reflect
.ValueOf(d
.objectInterface()))
501 // Check type of target: struct or map[string]T
504 // map must have string kind
506 if t
.Key().Kind() != reflect
.String
{
507 d
.saveError(&UnmarshalTypeError
{"object", v
.Type()})
509 d
.next() // skip over { } in input
513 v
.Set(reflect
.MakeMap(t
))
518 d
.saveError(&UnmarshalTypeError
{"object", v
.Type()})
520 d
.next() // skip over { } in input
524 var mapElem reflect
.Value
527 // Read opening " of string key or closing }.
528 op
:= d
.scanWhile(scanSkipSpace
)
529 if op
== scanEndObject
{
530 // closing } - can only happen on first iteration.
533 if op
!= scanBeginLiteral
{
539 op
= d
.scanWhile(scanContinue
)
540 item
:= d
.data
[start
: d
.off
-1]
541 key
, ok
:= unquoteBytes(item
)
546 // Figure out field corresponding to key.
547 var subv reflect
.Value
548 destring
:= false // whether the value is wrapped in a string to be decoded first
550 if v
.Kind() == reflect
.Map
{
551 elemType
:= v
.Type().Elem()
552 if !mapElem
.IsValid() {
553 mapElem
= reflect
.New(elemType
).Elem()
555 mapElem
.Set(reflect
.Zero(elemType
))
560 fields
:= cachedTypeFields(v
.Type())
561 for i
:= range fields
{
563 if bytes
.Equal(ff
.nameBytes
, key
) {
567 if f
== nil && ff
.equalFold(ff
.nameBytes
, key
) {
574 for _
, i
:= range f
.index
{
575 if subv
.Kind() == reflect
.Ptr
{
577 subv
.Set(reflect
.New(subv
.Type().Elem()))
586 // Read : before value.
587 if op
== scanSkipSpace
{
588 op
= d
.scanWhile(scanSkipSpace
)
590 if op
!= scanObjectKey
{
596 switch qv
:= d
.valueQuoted().(type) {
598 d
.literalStore(nullLiteral
, subv
, false)
600 d
.literalStore([]byte(qv
), subv
, true)
602 d
.saveError(fmt
.Errorf("json: invalid use of ,string struct tag, trying to unmarshal unquoted value into %v", item
, v
.Type()))
608 // Write value back to map;
609 // if using struct, subv points into struct already.
610 if v
.Kind() == reflect
.Map
{
611 kv
:= reflect
.ValueOf(key
).Convert(v
.Type().Key())
612 v
.SetMapIndex(kv
, subv
)
615 // Next token must be , or }.
616 op
= d
.scanWhile(scanSkipSpace
)
617 if op
== scanEndObject
{
620 if op
!= scanObjectValue
{
626 // literal consumes a literal from d.data[d.off-1:], decoding into the value v.
627 // The first byte of the literal has been read already
628 // (that's how the caller knows it's a literal).
629 func (d
*decodeState
) literal(v reflect
.Value
) {
630 // All bytes inside literal return scanContinue op code.
632 op
:= d
.scanWhile(scanContinue
)
634 // Scan read one byte too far; back up.
638 d
.literalStore(d
.data
[start
:d
.off
], v
, false)
641 // convertNumber converts the number literal s to a float64 or a Number
642 // depending on the setting of d.useNumber.
643 func (d
*decodeState
) convertNumber(s
string) (interface{}, error
) {
645 return Number(s
), nil
647 f
, err
:= strconv
.ParseFloat(s
, 64)
649 return nil, &UnmarshalTypeError
{"number " + s
, reflect
.TypeOf(0.0)}
654 var numberType
= reflect
.TypeOf(Number(""))
656 // literalStore decodes a literal stored in item into v.
658 // fromQuoted indicates whether this literal came from unwrapping a
659 // string from the ",string" struct tag option. this is used only to
660 // produce more helpful error messages.
661 func (d
*decodeState
) literalStore(item
[]byte, v reflect
.Value
, fromQuoted
bool) {
662 // Check for unmarshaler.
665 d
.saveError(fmt
.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item
, v
.Type()))
668 wantptr
:= item
[0] == 'n' // null
669 u
, ut
, pv
:= d
.indirect(v
, wantptr
)
671 err
:= u
.UnmarshalJSON(item
)
680 d
.saveError(fmt
.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item
, v
.Type()))
682 d
.saveError(&UnmarshalTypeError
{"string", v
.Type()})
685 s
, ok
:= unquoteBytes(item
)
688 d
.error(fmt
.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item
, v
.Type()))
693 err
:= ut
.UnmarshalText(s
)
702 switch c
:= item
[0]; c
{
705 case reflect
.Interface
, reflect
.Ptr
, reflect
.Map
, reflect
.Slice
:
706 v
.Set(reflect
.Zero(v
.Type()))
707 // otherwise, ignore null for primitives/string
709 case 't', 'f': // true, false
714 d
.saveError(fmt
.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item
, v
.Type()))
716 d
.saveError(&UnmarshalTypeError
{"bool", v
.Type()})
720 case reflect
.Interface
:
721 if v
.NumMethod() == 0 {
722 v
.Set(reflect
.ValueOf(value
))
724 d
.saveError(&UnmarshalTypeError
{"bool", v
.Type()})
729 s
, ok
:= unquoteBytes(item
)
732 d
.error(fmt
.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item
, v
.Type()))
739 d
.saveError(&UnmarshalTypeError
{"string", v
.Type()})
741 if v
.Type() != byteSliceType
{
742 d
.saveError(&UnmarshalTypeError
{"string", v
.Type()})
745 b
:= make([]byte, base64
.StdEncoding
.DecodedLen(len(s
)))
746 n
, err
:= base64
.StdEncoding
.Decode(b
, s
)
751 v
.Set(reflect
.ValueOf(b
[0:n
]))
753 v
.SetString(string(s
))
754 case reflect
.Interface
:
755 if v
.NumMethod() == 0 {
756 v
.Set(reflect
.ValueOf(string(s
)))
758 d
.saveError(&UnmarshalTypeError
{"string", v
.Type()})
763 if c
!= '-' && (c
< '0' || c
> '9') {
765 d
.error(fmt
.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item
, v
.Type()))
773 if v
.Kind() == reflect
.String
&& v
.Type() == numberType
{
778 d
.error(fmt
.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item
, v
.Type()))
780 d
.error(&UnmarshalTypeError
{"number", v
.Type()})
782 case reflect
.Interface
:
783 n
, err
:= d
.convertNumber(s
)
788 if v
.NumMethod() != 0 {
789 d
.saveError(&UnmarshalTypeError
{"number", v
.Type()})
792 v
.Set(reflect
.ValueOf(n
))
794 case reflect
.Int
, reflect
.Int8
, reflect
.Int16
, reflect
.Int32
, reflect
.Int64
:
795 n
, err
:= strconv
.ParseInt(s
, 10, 64)
796 if err
!= nil || v
.OverflowInt(n
) {
797 d
.saveError(&UnmarshalTypeError
{"number " + s
, v
.Type()})
802 case reflect
.Uint
, reflect
.Uint8
, reflect
.Uint16
, reflect
.Uint32
, reflect
.Uint64
, reflect
.Uintptr
:
803 n
, err
:= strconv
.ParseUint(s
, 10, 64)
804 if err
!= nil || v
.OverflowUint(n
) {
805 d
.saveError(&UnmarshalTypeError
{"number " + s
, v
.Type()})
810 case reflect
.Float32
, reflect
.Float64
:
811 n
, err
:= strconv
.ParseFloat(s
, v
.Type().Bits())
812 if err
!= nil || v
.OverflowFloat(n
) {
813 d
.saveError(&UnmarshalTypeError
{"number " + s
, v
.Type()})
821 // The xxxInterface routines build up a value to be stored
822 // in an empty interface. They are not strictly necessary,
823 // but they avoid the weight of reflection in this common case.
825 // valueInterface is like value but returns interface{}
826 func (d
*decodeState
) valueInterface() interface{} {
827 switch d
.scanWhile(scanSkipSpace
) {
832 return d
.arrayInterface()
833 case scanBeginObject
:
834 return d
.objectInterface()
835 case scanBeginLiteral
:
836 return d
.literalInterface()
840 // arrayInterface is like array but returns []interface{}.
841 func (d
*decodeState
) arrayInterface() []interface{} {
842 var v
= make([]interface{}, 0)
844 // Look ahead for ] - can only happen on first iteration.
845 op
:= d
.scanWhile(scanSkipSpace
)
846 if op
== scanEndArray
{
850 // Back up so d.value can have the byte we just read.
854 v
= append(v
, d
.valueInterface())
856 // Next token must be , or ].
857 op
= d
.scanWhile(scanSkipSpace
)
858 if op
== scanEndArray
{
861 if op
!= scanArrayValue
{
868 // objectInterface is like object but returns map[string]interface{}.
869 func (d
*decodeState
) objectInterface() map[string]interface{} {
870 m
:= make(map[string]interface{})
872 // Read opening " of string key or closing }.
873 op
:= d
.scanWhile(scanSkipSpace
)
874 if op
== scanEndObject
{
875 // closing } - can only happen on first iteration.
878 if op
!= scanBeginLiteral
{
884 op
= d
.scanWhile(scanContinue
)
885 item
:= d
.data
[start
: d
.off
-1]
886 key
, ok
:= unquote(item
)
891 // Read : before value.
892 if op
== scanSkipSpace
{
893 op
= d
.scanWhile(scanSkipSpace
)
895 if op
!= scanObjectKey
{
900 m
[key
] = d
.valueInterface()
902 // Next token must be , or }.
903 op
= d
.scanWhile(scanSkipSpace
)
904 if op
== scanEndObject
{
907 if op
!= scanObjectValue
{
914 // literalInterface is like literal but returns an interface value.
915 func (d
*decodeState
) literalInterface() interface{} {
916 // All bytes inside literal return scanContinue op code.
918 op
:= d
.scanWhile(scanContinue
)
920 // Scan read one byte too far; back up.
923 item
:= d
.data
[start
:d
.off
]
925 switch c
:= item
[0]; c
{
929 case 't', 'f': // true, false
933 s
, ok
:= unquote(item
)
940 if c
!= '-' && (c
< '0' || c
> '9') {
943 n
, err
:= d
.convertNumber(string(item
))
951 // getu4 decodes \uXXXX from the beginning of s, returning the hex value,
953 func getu4(s
[]byte) rune
{
954 if len(s
) < 6 || s
[0] != '\\' || s
[1] != 'u' {
957 r
, err
:= strconv
.ParseUint(string(s
[2:6]), 16, 64)
964 // unquote converts a quoted JSON string literal s into an actual string t.
965 // The rules are different than for Go, so cannot use strconv.Unquote.
966 func unquote(s
[]byte) (t
string, ok
bool) {
967 s
, ok
= unquoteBytes(s
)
972 func unquoteBytes(s
[]byte) (t
[]byte, ok
bool) {
973 if len(s
) < 2 || s
[0] != '"' || s
[len(s
)-1] != '"' {
978 // Check for unusual characters. If there are none,
979 // then no unquoting is needed, so return a slice of the
984 if c
== '\\' || c
== '"' || c
< ' ' {
987 if c
< utf8
.RuneSelf
{
991 rr
, size
:= utf8
.DecodeRune(s
[r
:])
992 if rr
== utf8
.RuneError
&& size
== 1 {
1001 b
:= make([]byte, len(s
)+2*utf8
.UTFMax
)
1002 w
:= copy(b
, s
[0:r
])
1004 // Out of room? Can only happen if s is full of
1005 // malformed UTF-8 and we're replacing each
1006 // byte with RuneError.
1007 if w
>= len(b
)-2*utf8
.UTFMax
{
1008 nb
:= make([]byte, (len(b
)+utf8
.UTFMax
)*2)
1021 case '"', '\\', '/', '\'':
1052 if utf16
.IsSurrogate(rr
) {
1054 if dec
:= utf16
.DecodeRune(rr
, rr1
); dec
!= unicode
.ReplacementChar
{
1055 // A valid pair; consume.
1057 w
+= utf8
.EncodeRune(b
[w
:], dec
)
1060 // Invalid surrogate; fall back to replacement rune.
1061 rr
= unicode
.ReplacementChar
1063 w
+= utf8
.EncodeRune(b
[w
:], rr
)
1066 // Quote, control characters are invalid.
1067 case c
== '"', c
< ' ':
1071 case c
< utf8
.RuneSelf
:
1076 // Coerce to well-formed UTF-8.
1078 rr
, size
:= utf8
.DecodeRune(s
[r
:])
1080 w
+= utf8
.EncodeRune(b
[w
:], rr
)