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.
40 // Unmarshal will only set exported fields of the struct.
42 // To unmarshal JSON into an interface value,
43 // Unmarshal stores one of these in the interface value:
45 // bool, for JSON booleans
46 // float64, for JSON numbers
47 // string, for JSON strings
48 // []interface{}, for JSON arrays
49 // map[string]interface{}, for JSON objects
52 // To unmarshal a JSON array into a slice, Unmarshal resets the slice length
53 // to zero and then appends each element to the slice.
54 // As a special case, to unmarshal an empty JSON array into a slice,
55 // Unmarshal replaces the slice with a new empty slice.
57 // To unmarshal a JSON array into a Go array, Unmarshal decodes
58 // JSON array elements into corresponding Go array elements.
59 // If the Go array is smaller than the JSON array,
60 // the additional JSON array elements are discarded.
61 // If the JSON array is smaller than the Go array,
62 // the additional Go array elements are set to zero values.
64 // To unmarshal a JSON object into a string-keyed map, Unmarshal first
65 // establishes a map to use, If the map is nil, Unmarshal allocates a new map.
66 // Otherwise Unmarshal reuses the existing map, keeping existing entries.
67 // Unmarshal then stores key-value pairs from the JSON object into the map.
69 // If a JSON value is not appropriate for a given target type,
70 // or if a JSON number overflows the target type, Unmarshal
71 // skips that field and completes the unmarshaling as best it can.
72 // If no more serious errors are encountered, Unmarshal returns
73 // an UnmarshalTypeError describing the earliest such error.
75 // The JSON null value unmarshals into an interface, map, pointer, or slice
76 // by setting that Go value to nil. Because null is often used in JSON to mean
77 // ``not present,'' unmarshaling a JSON null into any other Go type has no effect
78 // on the value and produces no error.
80 // When unmarshaling quoted strings, invalid UTF-8 or
81 // invalid UTF-16 surrogate pairs are not treated as an error.
82 // Instead, they are replaced by the Unicode replacement
85 func Unmarshal(data []byte, v interface{}) error {
86 // Check for well-formedness.
87 // Avoids filling out half a data structure
88 // before discovering a JSON syntax error.
90 err := checkValid(data, &d.scan)
99 // Unmarshaler is the interface implemented by objects
100 // that can unmarshal a JSON description of themselves.
101 // The input can be assumed to be a valid encoding of
102 // a JSON value. UnmarshalJSON must copy the JSON data
103 // if it wishes to retain the data after returning.
104 type Unmarshaler interface {
105 UnmarshalJSON([]byte) error
108 // An UnmarshalTypeError describes a JSON value that was
109 // not appropriate for a value of a specific Go type.
110 type UnmarshalTypeError struct {
111 Value string // description of JSON value - "bool", "array", "number -5"
112 Type reflect.Type // type of Go value it could not be assigned to
113 Offset int64 // error occurred after reading Offset bytes
116 func (e *UnmarshalTypeError) Error() string {
117 return "json: cannot unmarshal " + e.Value + " into Go value of type " + e.Type.String()
120 // An UnmarshalFieldError describes a JSON object key that
121 // led to an unexported (and therefore unwritable) struct field.
122 // (No longer used; kept for compatibility.)
123 type UnmarshalFieldError struct {
126 Field reflect.StructField
129 func (e *UnmarshalFieldError) Error() string {
130 return "json: cannot unmarshal object key " + strconv.Quote(e.Key) + " into unexported field " + e.Field.Name + " of type " + e.Type.String()
133 // An InvalidUnmarshalError describes an invalid argument passed to Unmarshal.
134 // (The argument to Unmarshal must be a non-nil pointer.)
135 type InvalidUnmarshalError struct {
139 func (e *InvalidUnmarshalError) Error() string {
141 return "json: Unmarshal(nil)"
144 if e.Type.Kind() != reflect.Ptr {
145 return "json: Unmarshal(non-pointer " + e.Type.String() + ")"
147 return "json: Unmarshal(nil " + e.Type.String() + ")"
150 func (d *decodeState) unmarshal(v interface{}) (err error) {
152 if r := recover(); r != nil {
153 if _, ok := r.(runtime.Error); ok {
160 rv := reflect.ValueOf(v)
161 if rv.Kind() != reflect.Ptr || rv.IsNil() {
162 return &InvalidUnmarshalError{reflect.TypeOf(v)}
166 // We decode rv not rv.Elem because the Unmarshaler interface
167 // test must be applied at the top level of the value.
172 // A Number represents a JSON number literal.
175 // String returns the literal text of the number.
176 func (n Number) String() string { return string(n) }
178 // Float64 returns the number as a float64.
179 func (n Number) Float64() (float64, error) {
180 return strconv.ParseFloat(string(n), 64)
183 // Int64 returns the number as an int64.
184 func (n Number) Int64() (int64, error) {
185 return strconv.ParseInt(string(n), 10, 64)
188 // isValidNumber reports whether s is a valid JSON number literal.
189 func isValidNumber(s string) bool {
190 // This function implements the JSON numbers grammar.
191 // See https://tools.ietf.org/html/rfc7159#section-6
192 // and http://json.org/number.gif
214 case '1' <= s[0] && s[0] <= '9':
216 for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
221 // . followed by 1 or more digits.
222 if len(s) >= 2 && s[0] == '.' && '0' <= s[1] && s[1] <= '9' {
224 for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
229 // e or E followed by an optional - or + and
231 if len(s) >= 2 && (s[0] == 'e' || s[0] == 'E') {
233 if s[0] == '+' || s[0] == '-' {
239 for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
244 // Make sure we are at the end.
248 // decodeState represents the state while decoding a JSON value.
249 type decodeState struct {
251 off int // read offset in data
253 nextscan scanner // for calls to nextValue
259 // errPhase is used for errors that should not happen unless
260 // there is a bug in the JSON decoder or something is editing
261 // the data slice while the decoder executes.
262 var errPhase = errors.New("JSON decoder out of sync - data changing underfoot?")
264 func (d *decodeState) init(data []byte) *decodeState {
271 // error aborts the decoding by panicking with err.
272 func (d *decodeState) error(err error) {
276 // saveError saves the first err it is called with,
277 // for reporting at the end of the unmarshal.
278 func (d *decodeState) saveError(err error) {
279 if d.savedError == nil {
284 // next cuts off and returns the next full JSON value in d.data[d.off:].
285 // The next value is known to be an object or array, not a literal.
286 func (d *decodeState) next() []byte {
288 item, rest, err := nextValue(d.data[d.off:], &d.nextscan)
292 d.off = len(d.data) - len(rest)
294 // Our scanner has seen the opening brace/bracket
295 // and thinks we're still in the middle of the object.
296 // invent a closing brace/bracket to get it out.
298 d.scan.step(&d.scan, '}')
300 d.scan.step(&d.scan, ']')
306 // scanWhile processes bytes in d.data[d.off:] until it
307 // receives a scan code not equal to op.
308 // It updates d.off and returns the new scan code.
309 func (d *decodeState) scanWhile(op int) int {
312 if d.off >= len(d.data) {
314 d.off = len(d.data) + 1 // mark processed EOF with len+1
318 newOp = d.scan.step(&d.scan, c)
327 // value decodes a JSON value from d.data[d.off:] into the value.
328 // it updates d.off to point past the decoded value.
329 func (d *decodeState) value(v reflect.Value) {
331 _, rest, err := nextValue(d.data[d.off:], &d.nextscan)
335 d.off = len(d.data) - len(rest)
337 // d.scan thinks we're still at the beginning of the item.
338 // Feed in an empty string - the shortest, simplest value -
339 // so that it knows we got to the end of the value.
343 d.scan.step = stateBeginValue
345 d.scan.step(&d.scan, '"')
346 d.scan.step(&d.scan, '"')
348 n := len(d.scan.parseState)
349 if n > 0 && d.scan.parseState[n-1] == parseObjectKey {
350 // d.scan thinks we just read an object key; finish the object
351 d.scan.step(&d.scan, ':')
352 d.scan.step(&d.scan, '"')
353 d.scan.step(&d.scan, '"')
354 d.scan.step(&d.scan, '}')
360 switch op := d.scanWhile(scanSkipSpace); op {
367 case scanBeginObject:
370 case scanBeginLiteral:
375 type unquotedValue struct{}
377 // valueQuoted is like value but decodes a
378 // quoted string literal or literal null into an interface value.
379 // If it finds anything other than a quoted string literal or null,
380 // valueQuoted returns unquotedValue{}.
381 func (d *decodeState) valueQuoted() interface{} {
382 switch op := d.scanWhile(scanSkipSpace); op {
387 d.array(reflect.Value{})
389 case scanBeginObject:
390 d.object(reflect.Value{})
392 case scanBeginLiteral:
393 switch v := d.literalInterface().(type) {
398 return unquotedValue{}
401 // indirect walks down v allocating pointers as needed,
402 // until it gets to a non-pointer.
403 // if it encounters an Unmarshaler, indirect stops and returns that.
404 // if decodingNull is true, indirect stops at the last pointer so it can be set to nil.
405 func (d *decodeState) indirect(v reflect.Value, decodingNull bool) (Unmarshaler, encoding.TextUnmarshaler, reflect.Value) {
406 // If v is a named type and is addressable,
407 // start with its address, so that if the type has pointer methods,
409 if v.Kind() != reflect.Ptr && v.Type().Name() != "" && v.CanAddr() {
413 // Load value from interface, but only if the result will be
414 // usefully addressable.
415 if v.Kind() == reflect.Interface && !v.IsNil() {
417 if e.Kind() == reflect.Ptr && !e.IsNil() && (!decodingNull || e.Elem().Kind() == reflect.Ptr) {
423 if v.Kind() != reflect.Ptr {
427 if v.Elem().Kind() != reflect.Ptr && decodingNull && v.CanSet() {
431 v.Set(reflect.New(v.Type().Elem()))
433 if v.Type().NumMethod() > 0 {
434 if u, ok := v.Interface().(Unmarshaler); ok {
435 return u, nil, reflect.Value{}
437 if u, ok := v.Interface().(encoding.TextUnmarshaler); ok {
438 return nil, u, reflect.Value{}
446 // array consumes an array from d.data[d.off-1:], decoding into the value v.
447 // the first byte of the array ('[') has been read already.
448 func (d *decodeState) array(v reflect.Value) {
449 // Check for unmarshaler.
450 u, ut, pv := d.indirect(v, false)
453 err := u.UnmarshalJSON(d.next())
460 d.saveError(&UnmarshalTypeError{"array", v.Type(), int64(d.off)})
468 // Check type of target.
470 case reflect.Interface:
471 if v.NumMethod() == 0 {
472 // Decoding into nil interface? Switch to non-reflect code.
473 v.Set(reflect.ValueOf(d.arrayInterface()))
476 // Otherwise it's invalid.
479 d.saveError(&UnmarshalTypeError{"array", v.Type(), int64(d.off)})
490 // Look ahead for ] - can only happen on first iteration.
491 op := d.scanWhile(scanSkipSpace)
492 if op == scanEndArray {
496 // Back up so d.value can have the byte we just read.
500 // Get element of array, growing if necessary.
501 if v.Kind() == reflect.Slice {
502 // Grow slice if necessary
504 newcap := v.Cap() + v.Cap()/2
508 newv := reflect.MakeSlice(v.Type(), v.Len(), newcap)
509 reflect.Copy(newv, v)
518 // Decode into element.
521 // Ran out of fixed array: skip.
522 d.value(reflect.Value{})
526 // Next token must be , or ].
527 op = d.scanWhile(scanSkipSpace)
528 if op == scanEndArray {
531 if op != scanArrayValue {
537 if v.Kind() == reflect.Array {
538 // Array. Zero the rest.
539 z := reflect.Zero(v.Type().Elem())
540 for ; i < v.Len(); i++ {
547 if i == 0 && v.Kind() == reflect.Slice {
548 v.Set(reflect.MakeSlice(v.Type(), 0, 0))
552 var nullLiteral = []byte("null")
554 // object consumes an object from d.data[d.off-1:], decoding into the value v.
555 // the first byte ('{') of the object has been read already.
556 func (d *decodeState) object(v reflect.Value) {
557 // Check for unmarshaler.
558 u, ut, pv := d.indirect(v, false)
561 err := u.UnmarshalJSON(d.next())
568 d.saveError(&UnmarshalTypeError{"object", v.Type(), int64(d.off)})
570 d.next() // skip over { } in input
575 // Decoding into nil interface? Switch to non-reflect code.
576 if v.Kind() == reflect.Interface && v.NumMethod() == 0 {
577 v.Set(reflect.ValueOf(d.objectInterface()))
581 // Check type of target: struct or map[string]T
584 // map must have string kind
586 if t.Key().Kind() != reflect.String {
587 d.saveError(&UnmarshalTypeError{"object", v.Type(), int64(d.off)})
589 d.next() // skip over { } in input
593 v.Set(reflect.MakeMap(t))
598 d.saveError(&UnmarshalTypeError{"object", v.Type(), int64(d.off)})
600 d.next() // skip over { } in input
604 var mapElem reflect.Value
607 // Read opening " of string key or closing }.
608 op := d.scanWhile(scanSkipSpace)
609 if op == scanEndObject {
610 // closing } - can only happen on first iteration.
613 if op != scanBeginLiteral {
619 op = d.scanWhile(scanContinue)
620 item := d.data[start : d.off-1]
621 key, ok := unquoteBytes(item)
626 // Figure out field corresponding to key.
627 var subv reflect.Value
628 destring := false // whether the value is wrapped in a string to be decoded first
630 if v.Kind() == reflect.Map {
631 elemType := v.Type().Elem()
632 if !mapElem.IsValid() {
633 mapElem = reflect.New(elemType).Elem()
635 mapElem.Set(reflect.Zero(elemType))
640 fields := cachedTypeFields(v.Type(), false)
641 for i := range fields {
643 if bytes.Equal(ff.nameBytes, key) {
647 if f == nil && ff.equalFold(ff.nameBytes, key) {
654 for _, i := range f.index {
655 if subv.Kind() == reflect.Ptr {
657 subv.Set(reflect.New(subv.Type().Elem()))
666 // Read : before value.
667 if op == scanSkipSpace {
668 op = d.scanWhile(scanSkipSpace)
670 if op != scanObjectKey {
676 switch qv := d.valueQuoted().(type) {
678 d.literalStore(nullLiteral, subv, false)
680 d.literalStore([]byte(qv), subv, true)
682 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal unquoted value into %v", subv.Type()))
688 // Write value back to map;
689 // if using struct, subv points into struct already.
690 if v.Kind() == reflect.Map {
691 kv := reflect.ValueOf(key).Convert(v.Type().Key())
692 v.SetMapIndex(kv, subv)
695 // Next token must be , or }.
696 op = d.scanWhile(scanSkipSpace)
697 if op == scanEndObject {
700 if op != scanObjectValue {
706 // literal consumes a literal from d.data[d.off-1:], decoding into the value v.
707 // The first byte of the literal has been read already
708 // (that's how the caller knows it's a literal).
709 func (d *decodeState) literal(v reflect.Value) {
710 // All bytes inside literal return scanContinue op code.
712 op := d.scanWhile(scanContinue)
714 // Scan read one byte too far; back up.
718 d.literalStore(d.data[start:d.off], v, false)
721 // convertNumber converts the number literal s to a float64 or a Number
722 // depending on the setting of d.useNumber.
723 func (d *decodeState) convertNumber(s string) (interface{}, error) {
725 return Number(s), nil
727 f, err := strconv.ParseFloat(s, 64)
729 return nil, &UnmarshalTypeError{"number " + s, reflect.TypeOf(0.0), int64(d.off)}
734 var numberType = reflect.TypeOf(Number(""))
736 // literalStore decodes a literal stored in item into v.
738 // fromQuoted indicates whether this literal came from unwrapping a
739 // string from the ",string" struct tag option. this is used only to
740 // produce more helpful error messages.
741 func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool) {
742 // Check for unmarshaler.
745 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
748 wantptr := item[0] == 'n' // null
749 u, ut, pv := d.indirect(v, wantptr)
751 err := u.UnmarshalJSON(item)
760 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
762 d.saveError(&UnmarshalTypeError{"string", v.Type(), int64(d.off)})
766 s, ok := unquoteBytes(item)
769 d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
774 err := ut.UnmarshalText(s)
783 switch c := item[0]; c {
786 case reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice:
787 v.Set(reflect.Zero(v.Type()))
788 // otherwise, ignore null for primitives/string
790 case 't', 'f': // true, false
795 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
797 d.saveError(&UnmarshalTypeError{"bool", v.Type(), int64(d.off)})
801 case reflect.Interface:
802 if v.NumMethod() == 0 {
803 v.Set(reflect.ValueOf(value))
805 d.saveError(&UnmarshalTypeError{"bool", v.Type(), int64(d.off)})
810 s, ok := unquoteBytes(item)
813 d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
820 d.saveError(&UnmarshalTypeError{"string", v.Type(), int64(d.off)})
822 if v.Type().Elem().Kind() != reflect.Uint8 {
823 d.saveError(&UnmarshalTypeError{"string", v.Type(), int64(d.off)})
826 b := make([]byte, base64.StdEncoding.DecodedLen(len(s)))
827 n, err := base64.StdEncoding.Decode(b, s)
834 v.SetString(string(s))
835 case reflect.Interface:
836 if v.NumMethod() == 0 {
837 v.Set(reflect.ValueOf(string(s)))
839 d.saveError(&UnmarshalTypeError{"string", v.Type(), int64(d.off)})
844 if c != '-' && (c < '0' || c > '9') {
846 d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
854 if v.Kind() == reflect.String && v.Type() == numberType {
856 if !isValidNumber(s) {
857 d.error(fmt.Errorf("json: invalid number literal, trying to unmarshal %q into Number", item))
862 d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
864 d.error(&UnmarshalTypeError{"number", v.Type(), int64(d.off)})
866 case reflect.Interface:
867 n, err := d.convertNumber(s)
872 if v.NumMethod() != 0 {
873 d.saveError(&UnmarshalTypeError{"number", v.Type(), int64(d.off)})
876 v.Set(reflect.ValueOf(n))
878 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
879 n, err := strconv.ParseInt(s, 10, 64)
880 if err != nil || v.OverflowInt(n) {
881 d.saveError(&UnmarshalTypeError{"number " + s, v.Type(), int64(d.off)})
886 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
887 n, err := strconv.ParseUint(s, 10, 64)
888 if err != nil || v.OverflowUint(n) {
889 d.saveError(&UnmarshalTypeError{"number " + s, v.Type(), int64(d.off)})
894 case reflect.Float32, reflect.Float64:
895 n, err := strconv.ParseFloat(s, v.Type().Bits())
896 if err != nil || v.OverflowFloat(n) {
897 d.saveError(&UnmarshalTypeError{"number " + s, v.Type(), int64(d.off)})
905 // The xxxInterface routines build up a value to be stored
906 // in an empty interface. They are not strictly necessary,
907 // but they avoid the weight of reflection in this common case.
909 // valueInterface is like value but returns interface{}
910 func (d *decodeState) valueInterface() interface{} {
911 switch d.scanWhile(scanSkipSpace) {
916 return d.arrayInterface()
917 case scanBeginObject:
918 return d.objectInterface()
919 case scanBeginLiteral:
920 return d.literalInterface()
924 // arrayInterface is like array but returns []interface{}.
925 func (d *decodeState) arrayInterface() []interface{} {
926 var v = make([]interface{}, 0)
928 // Look ahead for ] - can only happen on first iteration.
929 op := d.scanWhile(scanSkipSpace)
930 if op == scanEndArray {
934 // Back up so d.value can have the byte we just read.
938 v = append(v, d.valueInterface())
940 // Next token must be , or ].
941 op = d.scanWhile(scanSkipSpace)
942 if op == scanEndArray {
945 if op != scanArrayValue {
952 // objectInterface is like object but returns map[string]interface{}.
953 func (d *decodeState) objectInterface() map[string]interface{} {
954 m := make(map[string]interface{})
956 // Read opening " of string key or closing }.
957 op := d.scanWhile(scanSkipSpace)
958 if op == scanEndObject {
959 // closing } - can only happen on first iteration.
962 if op != scanBeginLiteral {
968 op = d.scanWhile(scanContinue)
969 item := d.data[start : d.off-1]
970 key, ok := unquote(item)
975 // Read : before value.
976 if op == scanSkipSpace {
977 op = d.scanWhile(scanSkipSpace)
979 if op != scanObjectKey {
984 m[key] = d.valueInterface()
986 // Next token must be , or }.
987 op = d.scanWhile(scanSkipSpace)
988 if op == scanEndObject {
991 if op != scanObjectValue {
998 // literalInterface is like literal but returns an interface value.
999 func (d *decodeState) literalInterface() interface{} {
1000 // All bytes inside literal return scanContinue op code.
1002 op := d.scanWhile(scanContinue)
1004 // Scan read one byte too far; back up.
1007 item := d.data[start:d.off]
1009 switch c := item[0]; c {
1013 case 't', 'f': // true, false
1017 s, ok := unquote(item)
1024 if c != '-' && (c < '0' || c > '9') {
1027 n, err := d.convertNumber(string(item))
1035 // getu4 decodes \uXXXX from the beginning of s, returning the hex value,
1036 // or it returns -1.
1037 func getu4(s []byte) rune {
1038 if len(s) < 6 || s[0] != '\\' || s[1] != 'u' {
1041 r, err := strconv.ParseUint(string(s[2:6]), 16, 64)
1048 // unquote converts a quoted JSON string literal s into an actual string t.
1049 // The rules are different than for Go, so cannot use strconv.Unquote.
1050 func unquote(s []byte) (t string, ok bool) {
1051 s, ok = unquoteBytes(s)
1056 func unquoteBytes(s []byte) (t []byte, ok bool) {
1057 if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' {
1062 // Check for unusual characters. If there are none,
1063 // then no unquoting is needed, so return a slice of the
1068 if c == '\\' || c == '"' || c < ' ' {
1071 if c < utf8.RuneSelf {
1075 rr, size := utf8.DecodeRune(s[r:])
1076 if rr == utf8.RuneError && size == 1 {
1085 b := make([]byte, len(s)+2*utf8.UTFMax)
1086 w := copy(b, s[0:r])
1088 // Out of room? Can only happen if s is full of
1089 // malformed UTF-8 and we're replacing each
1090 // byte with RuneError.
1091 if w >= len(b)-2*utf8.UTFMax {
1092 nb := make([]byte, (len(b)+utf8.UTFMax)*2)
1105 case '"', '\\', '/', '\'':
1136 if utf16.IsSurrogate(rr) {
1138 if dec := utf16.DecodeRune(rr, rr1); dec != unicode.ReplacementChar {
1139 // A valid pair; consume.
1141 w += utf8.EncodeRune(b[w:], dec)
1144 // Invalid surrogate; fall back to replacement rune.
1145 rr = unicode.ReplacementChar
1147 w += utf8.EncodeRune(b[w:], rr)
1150 // Quote, control characters are invalid.
1151 case c == '"', c < ' ':
1155 case c < utf8.RuneSelf:
1160 // Coerce to well-formed UTF-8.
1162 rr, size := utf8.DecodeRune(s[r:])
1164 w += utf8.EncodeRune(b[w:], rr)