decode.go 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  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. // Represents JSON data structure using native Go types: booleans, floats,
  5. // strings, arrays, and maps.
  6. package json
  7. import (
  8. "encoding"
  9. "encoding/base64"
  10. "fmt"
  11. "reflect"
  12. "strconv"
  13. "strings"
  14. "unicode"
  15. "unicode/utf16"
  16. "unicode/utf8"
  17. )
  18. // Unmarshal parses the JSON-encoded data and stores the result
  19. // in the value pointed to by v. If v is nil or not a pointer,
  20. // Unmarshal returns an InvalidUnmarshalError.
  21. //
  22. // Unmarshal uses the inverse of the encodings that
  23. // Marshal uses, allocating maps, slices, and pointers as necessary,
  24. // with the following additional rules:
  25. //
  26. // To unmarshal JSON into a pointer, Unmarshal first handles the case of
  27. // the JSON being the JSON literal null. In that case, Unmarshal sets
  28. // the pointer to nil. Otherwise, Unmarshal unmarshals the JSON into
  29. // the value pointed at by the pointer. If the pointer is nil, Unmarshal
  30. // allocates a new value for it to point to.
  31. //
  32. // To unmarshal JSON into a value implementing the Unmarshaler interface,
  33. // Unmarshal calls that value's UnmarshalJSON method, including
  34. // when the input is a JSON null.
  35. // Otherwise, if the value implements encoding.TextUnmarshaler
  36. // and the input is a JSON quoted string, Unmarshal calls that value's
  37. // UnmarshalText method with the unquoted form of the string.
  38. //
  39. // To unmarshal JSON into a struct, Unmarshal matches incoming object
  40. // keys to the keys used by Marshal (either the struct field name or its tag),
  41. // preferring an exact match but also accepting a case-insensitive match. By
  42. // default, object keys which don't have a corresponding struct field are
  43. // ignored (see Decoder.DisallowUnknownFields for an alternative).
  44. //
  45. // To unmarshal JSON into an interface value,
  46. // Unmarshal stores one of these in the interface value:
  47. //
  48. // bool, for JSON booleans
  49. // float64, for JSON numbers
  50. // string, for JSON strings
  51. // []interface{}, for JSON arrays
  52. // map[string]interface{}, for JSON objects
  53. // nil for JSON null
  54. //
  55. // To unmarshal a JSON array into a slice, Unmarshal resets the slice length
  56. // to zero and then appends each element to the slice.
  57. // As a special case, to unmarshal an empty JSON array into a slice,
  58. // Unmarshal replaces the slice with a new empty slice.
  59. //
  60. // To unmarshal a JSON array into a Go array, Unmarshal decodes
  61. // JSON array elements into corresponding Go array elements.
  62. // If the Go array is smaller than the JSON array,
  63. // the additional JSON array elements are discarded.
  64. // If the JSON array is smaller than the Go array,
  65. // the additional Go array elements are set to zero values.
  66. //
  67. // To unmarshal a JSON object into a map, Unmarshal first establishes a map to
  68. // use. If the map is nil, Unmarshal allocates a new map. Otherwise Unmarshal
  69. // reuses the existing map, keeping existing entries. Unmarshal then stores
  70. // key-value pairs from the JSON object into the map. The map's key type must
  71. // either be any string type, an integer, implement json.Unmarshaler, or
  72. // implement encoding.TextUnmarshaler.
  73. //
  74. // If a JSON value is not appropriate for a given target type,
  75. // or if a JSON number overflows the target type, Unmarshal
  76. // skips that field and completes the unmarshaling as best it can.
  77. // If no more serious errors are encountered, Unmarshal returns
  78. // an UnmarshalTypeError describing the earliest such error. In any
  79. // case, it's not guaranteed that all the remaining fields following
  80. // the problematic one will be unmarshaled into the target object.
  81. //
  82. // The JSON null value unmarshals into an interface, map, pointer, or slice
  83. // by setting that Go value to nil. Because null is often used in JSON to mean
  84. // ``not present,'' unmarshaling a JSON null into any other Go type has no effect
  85. // on the value and produces no error.
  86. //
  87. // When unmarshaling quoted strings, invalid UTF-8 or
  88. // invalid UTF-16 surrogate pairs are not treated as an error.
  89. // Instead, they are replaced by the Unicode replacement
  90. // character U+FFFD.
  91. //
  92. func Unmarshal(data []byte, v interface{}) error {
  93. // Check for well-formedness.
  94. // Avoids filling out half a data structure
  95. // before discovering a JSON syntax error.
  96. var d decodeState
  97. err := checkValid(data, &d.scan)
  98. if err != nil {
  99. return err
  100. }
  101. d.init(data)
  102. return d.unmarshal(v)
  103. }
  104. // Unmarshaler is the interface implemented by types
  105. // that can unmarshal a JSON description of themselves.
  106. // The input can be assumed to be a valid encoding of
  107. // a JSON value. UnmarshalJSON must copy the JSON data
  108. // if it wishes to retain the data after returning.
  109. //
  110. // By convention, to approximate the behavior of Unmarshal itself,
  111. // Unmarshalers implement UnmarshalJSON([]byte("null")) as a no-op.
  112. type Unmarshaler interface {
  113. UnmarshalJSON([]byte) error
  114. }
  115. // An UnmarshalTypeError describes a JSON value that was
  116. // not appropriate for a value of a specific Go type.
  117. type UnmarshalTypeError struct {
  118. Value string // description of JSON value - "bool", "array", "number -5"
  119. Type reflect.Type // type of Go value it could not be assigned to
  120. Offset int64 // error occurred after reading Offset bytes
  121. Struct string // name of the struct type containing the field
  122. Field string // the full path from root node to the field
  123. }
  124. func (e *UnmarshalTypeError) Error() string {
  125. if e.Struct != "" || e.Field != "" {
  126. return "json: cannot unmarshal " + e.Value + " into Go struct field " + e.Struct + "." + e.Field + " of type " + e.Type.String()
  127. }
  128. return "json: cannot unmarshal " + e.Value + " into Go value of type " + e.Type.String()
  129. }
  130. // An UnmarshalFieldError describes a JSON object key that
  131. // led to an unexported (and therefore unwritable) struct field.
  132. //
  133. // Deprecated: No longer used; kept for compatibility.
  134. type UnmarshalFieldError struct {
  135. Key string
  136. Type reflect.Type
  137. Field reflect.StructField
  138. }
  139. func (e *UnmarshalFieldError) Error() string {
  140. return "json: cannot unmarshal object key " + strconv.Quote(e.Key) + " into unexported field " + e.Field.Name + " of type " + e.Type.String()
  141. }
  142. // An InvalidUnmarshalError describes an invalid argument passed to Unmarshal.
  143. // (The argument to Unmarshal must be a non-nil pointer.)
  144. type InvalidUnmarshalError struct {
  145. Type reflect.Type
  146. }
  147. func (e *InvalidUnmarshalError) Error() string {
  148. if e.Type == nil {
  149. return "json: Unmarshal(nil)"
  150. }
  151. if e.Type.Kind() != reflect.Ptr {
  152. return "json: Unmarshal(non-pointer " + e.Type.String() + ")"
  153. }
  154. return "json: Unmarshal(nil " + e.Type.String() + ")"
  155. }
  156. func (d *decodeState) unmarshal(v interface{}) error {
  157. rv := reflect.ValueOf(v)
  158. if rv.Kind() != reflect.Ptr || rv.IsNil() {
  159. return &InvalidUnmarshalError{reflect.TypeOf(v)}
  160. }
  161. d.scan.reset()
  162. d.scanWhile(scanSkipSpace)
  163. // We decode rv not rv.Elem because the Unmarshaler interface
  164. // test must be applied at the top level of the value.
  165. err := d.value(rv)
  166. if err != nil {
  167. return d.addErrorContext(err)
  168. }
  169. return d.savedError
  170. }
  171. // A Number represents a JSON number literal.
  172. type Number string
  173. // String returns the literal text of the number.
  174. func (n Number) String() string { return string(n) }
  175. // Float64 returns the number as a float64.
  176. func (n Number) Float64() (float64, error) {
  177. return strconv.ParseFloat(string(n), 64)
  178. }
  179. // Int64 returns the number as an int64.
  180. func (n Number) Int64() (int64, error) {
  181. return strconv.ParseInt(string(n), 10, 64)
  182. }
  183. // An errorContext provides context for type errors during decoding.
  184. type errorContext struct {
  185. Struct reflect.Type
  186. FieldStack []string
  187. }
  188. // decodeState represents the state while decoding a JSON value.
  189. type decodeState struct {
  190. data []byte
  191. off int // next read offset in data
  192. opcode int // last read result
  193. scan scanner
  194. errorContext *errorContext
  195. savedError error
  196. useNumber bool
  197. disallowUnknownFields bool
  198. discriminatorTypeFieldName string
  199. discriminatorValueFieldName string
  200. discriminatorToTypeFn DiscriminatorToTypeFunc
  201. }
  202. // readIndex returns the position of the last byte read.
  203. func (d *decodeState) readIndex() int {
  204. return d.off - 1
  205. }
  206. // phasePanicMsg is used as a panic message when we end up with something that
  207. // shouldn't happen. It can indicate a bug in the JSON decoder, or that
  208. // something is editing the data slice while the decoder executes.
  209. const phasePanicMsg = "JSON decoder out of sync - data changing underfoot?"
  210. func (d *decodeState) init(data []byte) *decodeState {
  211. d.data = data
  212. d.off = 0
  213. d.savedError = nil
  214. if d.errorContext != nil {
  215. d.errorContext.Struct = nil
  216. // Reuse the allocated space for the FieldStack slice.
  217. d.errorContext.FieldStack = d.errorContext.FieldStack[:0]
  218. }
  219. return d
  220. }
  221. // saveError saves the first err it is called with,
  222. // for reporting at the end of the unmarshal.
  223. func (d *decodeState) saveError(err error) {
  224. if d.savedError == nil {
  225. d.savedError = d.addErrorContext(err)
  226. }
  227. }
  228. // addErrorContext returns a new error enhanced with information from d.errorContext
  229. func (d *decodeState) addErrorContext(err error) error {
  230. if d.errorContext != nil && (d.errorContext.Struct != nil || len(d.errorContext.FieldStack) > 0) {
  231. switch err := err.(type) {
  232. case *UnmarshalTypeError:
  233. err.Struct = d.errorContext.Struct.Name()
  234. err.Field = strings.Join(d.errorContext.FieldStack, ".")
  235. }
  236. }
  237. return err
  238. }
  239. // skip scans to the end of what was started.
  240. func (d *decodeState) skip() {
  241. s, data, i := &d.scan, d.data, d.off
  242. depth := len(s.parseState)
  243. for {
  244. op := s.step(s, data[i])
  245. i++
  246. if len(s.parseState) < depth {
  247. d.off = i
  248. d.opcode = op
  249. return
  250. }
  251. }
  252. }
  253. // scanNext processes the byte at d.data[d.off].
  254. func (d *decodeState) scanNext() {
  255. if d.off < len(d.data) {
  256. d.opcode = d.scan.step(&d.scan, d.data[d.off])
  257. d.off++
  258. } else {
  259. d.opcode = d.scan.eof()
  260. d.off = len(d.data) + 1 // mark processed EOF with len+1
  261. }
  262. }
  263. // scanWhile processes bytes in d.data[d.off:] until it
  264. // receives a scan code not equal to op.
  265. func (d *decodeState) scanWhile(op int) {
  266. s, data, i := &d.scan, d.data, d.off
  267. for i < len(data) {
  268. newOp := s.step(s, data[i])
  269. i++
  270. if newOp != op {
  271. d.opcode = newOp
  272. d.off = i
  273. return
  274. }
  275. }
  276. d.off = len(data) + 1 // mark processed EOF with len+1
  277. d.opcode = d.scan.eof()
  278. }
  279. // rescanLiteral is similar to scanWhile(scanContinue), but it specialises the
  280. // common case where we're decoding a literal. The decoder scans the input
  281. // twice, once for syntax errors and to check the length of the value, and the
  282. // second to perform the decoding.
  283. //
  284. // Only in the second step do we use decodeState to tokenize literals, so we
  285. // know there aren't any syntax errors. We can take advantage of that knowledge,
  286. // and scan a literal's bytes much more quickly.
  287. func (d *decodeState) rescanLiteral() {
  288. data, i := d.data, d.off
  289. Switch:
  290. switch data[i-1] {
  291. case '"': // string
  292. for ; i < len(data); i++ {
  293. switch data[i] {
  294. case '\\':
  295. i++ // escaped char
  296. case '"':
  297. i++ // tokenize the closing quote too
  298. break Switch
  299. }
  300. }
  301. case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-': // number
  302. for ; i < len(data); i++ {
  303. switch data[i] {
  304. case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  305. '.', 'e', 'E', '+', '-':
  306. default:
  307. break Switch
  308. }
  309. }
  310. case 't': // true
  311. i += len("rue")
  312. case 'f': // false
  313. i += len("alse")
  314. case 'n': // null
  315. i += len("ull")
  316. }
  317. if i < len(data) {
  318. d.opcode = stateEndValue(&d.scan, data[i])
  319. } else {
  320. d.opcode = scanEnd
  321. }
  322. d.off = i + 1
  323. }
  324. // value consumes a JSON value from d.data[d.off-1:], decoding into v, and
  325. // reads the following byte ahead. If v is invalid, the value is discarded.
  326. // The first byte of the value has been read already.
  327. func (d *decodeState) value(v reflect.Value) error {
  328. switch d.opcode {
  329. default:
  330. panic(phasePanicMsg)
  331. case scanBeginArray:
  332. if v.IsValid() {
  333. if err := d.array(v); err != nil {
  334. return err
  335. }
  336. } else {
  337. d.skip()
  338. }
  339. d.scanNext()
  340. case scanBeginObject:
  341. if v.IsValid() {
  342. if err := d.object(v); err != nil {
  343. return err
  344. }
  345. } else {
  346. d.skip()
  347. }
  348. d.scanNext()
  349. case scanBeginLiteral:
  350. // All bytes inside literal return scanContinue op code.
  351. start := d.readIndex()
  352. d.rescanLiteral()
  353. if v.IsValid() {
  354. if err := d.literalStore(d.data[start:d.readIndex()], v, false); err != nil {
  355. return err
  356. }
  357. }
  358. }
  359. return nil
  360. }
  361. type unquotedValue struct{}
  362. // valueQuoted is like value but decodes a
  363. // quoted string literal or literal null into an interface value.
  364. // If it finds anything other than a quoted string literal or null,
  365. // valueQuoted returns unquotedValue{}.
  366. func (d *decodeState) valueQuoted() interface{} {
  367. switch d.opcode {
  368. default:
  369. panic(phasePanicMsg)
  370. case scanBeginArray, scanBeginObject:
  371. d.skip()
  372. d.scanNext()
  373. case scanBeginLiteral:
  374. v := d.literalInterface()
  375. switch v.(type) {
  376. case nil, string:
  377. return v
  378. }
  379. }
  380. return unquotedValue{}
  381. }
  382. // indirect walks down v allocating pointers as needed,
  383. // until it gets to a non-pointer.
  384. // If it encounters an Unmarshaler, indirect stops and returns that.
  385. // If decodingNull is true, indirect stops at the first settable pointer so it
  386. // can be set to nil.
  387. func indirect(v reflect.Value, decodingNull bool) (Unmarshaler, encoding.TextUnmarshaler, reflect.Value) {
  388. // Issue #24153 indicates that it is generally not a guaranteed property
  389. // that you may round-trip a reflect.Value by calling Value.Addr().Elem()
  390. // and expect the value to still be settable for values derived from
  391. // unexported embedded struct fields.
  392. //
  393. // The logic below effectively does this when it first addresses the value
  394. // (to satisfy possible pointer methods) and continues to dereference
  395. // subsequent pointers as necessary.
  396. //
  397. // After the first round-trip, we set v back to the original value to
  398. // preserve the original RW flags contained in reflect.Value.
  399. v0 := v
  400. haveAddr := false
  401. // If v is a named type and is addressable,
  402. // start with its address, so that if the type has pointer methods,
  403. // we find them.
  404. if v.Kind() != reflect.Ptr && v.Type().Name() != "" && v.CanAddr() {
  405. haveAddr = true
  406. v = v.Addr()
  407. }
  408. for {
  409. // Load value from interface, but only if the result will be
  410. // usefully addressable.
  411. if v.Kind() == reflect.Interface && !v.IsNil() {
  412. e := v.Elem()
  413. if e.Kind() == reflect.Ptr && !e.IsNil() && (!decodingNull || e.Elem().Kind() == reflect.Ptr) {
  414. haveAddr = false
  415. v = e
  416. continue
  417. }
  418. }
  419. if v.Kind() != reflect.Ptr {
  420. break
  421. }
  422. if decodingNull && v.CanSet() {
  423. break
  424. }
  425. // Prevent infinite loop if v is an interface pointing to its own address:
  426. // var v interface{}
  427. // v = &v
  428. if v.Elem().Kind() == reflect.Interface && v.Elem().Elem() == v {
  429. v = v.Elem()
  430. break
  431. }
  432. if v.IsNil() {
  433. v.Set(reflect.New(v.Type().Elem()))
  434. }
  435. if v.Type().NumMethod() > 0 && v.CanInterface() {
  436. if u, ok := v.Interface().(Unmarshaler); ok {
  437. return u, nil, reflect.Value{}
  438. }
  439. if !decodingNull {
  440. if u, ok := v.Interface().(encoding.TextUnmarshaler); ok {
  441. return nil, u, reflect.Value{}
  442. }
  443. }
  444. }
  445. if haveAddr {
  446. v = v0 // restore original value after round-trip Value.Addr().Elem()
  447. haveAddr = false
  448. } else {
  449. v = v.Elem()
  450. }
  451. }
  452. return nil, nil, v
  453. }
  454. // array consumes an array from d.data[d.off-1:], decoding into v.
  455. // The first byte of the array ('[') has been read already.
  456. func (d *decodeState) array(v reflect.Value) error {
  457. // Check for unmarshaler.
  458. u, ut, pv := indirect(v, false)
  459. if u != nil {
  460. start := d.readIndex()
  461. d.skip()
  462. return u.UnmarshalJSON(d.data[start:d.off])
  463. }
  464. if ut != nil {
  465. d.saveError(&UnmarshalTypeError{Value: "array", Type: v.Type(), Offset: int64(d.off)})
  466. d.skip()
  467. return nil
  468. }
  469. v = pv
  470. // Check type of target.
  471. switch v.Kind() {
  472. case reflect.Interface:
  473. if v.NumMethod() == 0 {
  474. // Decoding into nil interface? Switch to non-reflect code.
  475. ai := d.arrayInterface()
  476. v.Set(reflect.ValueOf(ai))
  477. return nil
  478. }
  479. // Otherwise it's invalid.
  480. fallthrough
  481. default:
  482. d.saveError(&UnmarshalTypeError{Value: "array", Type: v.Type(), Offset: int64(d.off)})
  483. d.skip()
  484. return nil
  485. case reflect.Array, reflect.Slice:
  486. break
  487. }
  488. i := 0
  489. for {
  490. // Look ahead for ] - can only happen on first iteration.
  491. d.scanWhile(scanSkipSpace)
  492. if d.opcode == scanEndArray {
  493. break
  494. }
  495. // Get element of array, growing if necessary.
  496. if v.Kind() == reflect.Slice {
  497. // Grow slice if necessary
  498. if i >= v.Cap() {
  499. newcap := v.Cap() + v.Cap()/2
  500. if newcap < 4 {
  501. newcap = 4
  502. }
  503. newv := reflect.MakeSlice(v.Type(), v.Len(), newcap)
  504. reflect.Copy(newv, v)
  505. v.Set(newv)
  506. }
  507. if i >= v.Len() {
  508. v.SetLen(i + 1)
  509. }
  510. }
  511. if i < v.Len() {
  512. // Decode into element.
  513. if err := d.value(v.Index(i)); err != nil {
  514. return err
  515. }
  516. } else {
  517. // Ran out of fixed array: skip.
  518. if err := d.value(reflect.Value{}); err != nil {
  519. return err
  520. }
  521. }
  522. i++
  523. // Next token must be , or ].
  524. if d.opcode == scanSkipSpace {
  525. d.scanWhile(scanSkipSpace)
  526. }
  527. if d.opcode == scanEndArray {
  528. break
  529. }
  530. if d.opcode != scanArrayValue {
  531. panic(phasePanicMsg)
  532. }
  533. }
  534. if i < v.Len() {
  535. if v.Kind() == reflect.Array {
  536. // Array. Zero the rest.
  537. z := reflect.Zero(v.Type().Elem())
  538. for ; i < v.Len(); i++ {
  539. v.Index(i).Set(z)
  540. }
  541. } else {
  542. v.SetLen(i)
  543. }
  544. }
  545. if i == 0 && v.Kind() == reflect.Slice {
  546. v.Set(reflect.MakeSlice(v.Type(), 0, 0))
  547. }
  548. return nil
  549. }
  550. var nullLiteral = []byte("null")
  551. var textUnmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
  552. // object consumes an object from d.data[d.off-1:], decoding into v.
  553. // The first byte ('{') of the object has been read already.
  554. func (d *decodeState) object(v reflect.Value) error {
  555. // Check for unmarshaler.
  556. u, ut, pv := indirect(v, false)
  557. if u != nil {
  558. start := d.readIndex()
  559. d.skip()
  560. return u.UnmarshalJSON(d.data[start:d.off])
  561. }
  562. if ut != nil {
  563. d.saveError(&UnmarshalTypeError{Value: "object", Type: v.Type(), Offset: int64(d.off)})
  564. d.skip()
  565. return nil
  566. }
  567. v = pv
  568. t := v.Type()
  569. // Decoding into nil interface? Switch to non-reflect code.
  570. if v.Kind() == reflect.Interface && v.NumMethod() == 0 && !d.isDiscriminatorSet() {
  571. oi := d.objectInterface()
  572. v.Set(reflect.ValueOf(oi))
  573. return nil
  574. }
  575. var fields structFields
  576. // Check type of target:
  577. // struct or
  578. // map[T1]T2 where T1 is string, an integer type,
  579. // or an encoding.TextUnmarshaler
  580. switch v.Kind() {
  581. case reflect.Map:
  582. // Map key must either have string kind, have an integer kind,
  583. // or be an encoding.TextUnmarshaler.
  584. switch t.Key().Kind() {
  585. case reflect.String,
  586. reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
  587. reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  588. default:
  589. if !reflect.PtrTo(t.Key()).Implements(textUnmarshalerType) {
  590. d.saveError(&UnmarshalTypeError{Value: "object", Type: t, Offset: int64(d.off)})
  591. d.skip()
  592. return nil
  593. }
  594. }
  595. if v.IsNil() {
  596. v.Set(reflect.MakeMap(t))
  597. }
  598. case reflect.Struct:
  599. fields = cachedTypeFields(t)
  600. // ok
  601. default:
  602. if d.isDiscriminatorSet() {
  603. return d.discriminatorInterfaceDecode(t, v)
  604. }
  605. d.saveError(&UnmarshalTypeError{Value: "object", Type: t, Offset: int64(d.off)})
  606. d.skip()
  607. return nil
  608. }
  609. var mapElem reflect.Value
  610. var origErrorContext errorContext
  611. if d.errorContext != nil {
  612. origErrorContext = *d.errorContext
  613. }
  614. for {
  615. // Read opening " of string key or closing }.
  616. d.scanWhile(scanSkipSpace)
  617. if d.opcode == scanEndObject {
  618. // closing } - can only happen on first iteration.
  619. break
  620. }
  621. if d.opcode != scanBeginLiteral {
  622. panic(phasePanicMsg)
  623. }
  624. // Read key.
  625. start := d.readIndex()
  626. d.rescanLiteral()
  627. item := d.data[start:d.readIndex()]
  628. key, ok := unquoteBytes(item)
  629. if !ok {
  630. panic(phasePanicMsg)
  631. }
  632. // Figure out field corresponding to key.
  633. var subv reflect.Value
  634. destring := false // whether the value is wrapped in a string to be decoded first
  635. if v.Kind() == reflect.Map {
  636. elemType := t.Elem()
  637. if !mapElem.IsValid() {
  638. mapElem = reflect.New(elemType).Elem()
  639. } else {
  640. mapElem.Set(reflect.Zero(elemType))
  641. }
  642. subv = mapElem
  643. } else {
  644. var f *field
  645. if i, ok := fields.nameIndex[string(key)]; ok {
  646. // Found an exact name match.
  647. f = &fields.list[i]
  648. } else {
  649. // Fall back to the expensive case-insensitive
  650. // linear search.
  651. for i := range fields.list {
  652. ff := &fields.list[i]
  653. if ff.equalFold(ff.nameBytes, key) {
  654. f = ff
  655. break
  656. }
  657. }
  658. }
  659. if f != nil {
  660. subv = v
  661. destring = f.quoted
  662. for _, i := range f.index {
  663. if subv.Kind() == reflect.Ptr {
  664. if subv.IsNil() {
  665. // If a struct embeds a pointer to an unexported type,
  666. // it is not possible to set a newly allocated value
  667. // since the field is unexported.
  668. //
  669. // See https://golang.org/issue/21357
  670. if !subv.CanSet() {
  671. d.saveError(fmt.Errorf("json: cannot set embedded pointer to unexported struct: %v", subv.Type().Elem()))
  672. // Invalidate subv to ensure d.value(subv) skips over
  673. // the JSON value without assigning it to subv.
  674. subv = reflect.Value{}
  675. destring = false
  676. break
  677. }
  678. subv.Set(reflect.New(subv.Type().Elem()))
  679. }
  680. subv = subv.Elem()
  681. }
  682. subv = subv.Field(i)
  683. }
  684. if d.errorContext == nil {
  685. d.errorContext = new(errorContext)
  686. }
  687. d.errorContext.FieldStack = append(d.errorContext.FieldStack, f.name)
  688. d.errorContext.Struct = t
  689. } else if d.disallowUnknownFields {
  690. d.saveError(fmt.Errorf("json: unknown field %q", key))
  691. }
  692. }
  693. // Read : before value.
  694. if d.opcode == scanSkipSpace {
  695. d.scanWhile(scanSkipSpace)
  696. }
  697. if d.opcode != scanObjectKey {
  698. panic(phasePanicMsg)
  699. }
  700. d.scanWhile(scanSkipSpace)
  701. if destring {
  702. switch qv := d.valueQuoted().(type) {
  703. case nil:
  704. if err := d.literalStore(nullLiteral, subv, false); err != nil {
  705. return err
  706. }
  707. case string:
  708. if err := d.literalStore([]byte(qv), subv, true); err != nil {
  709. return err
  710. }
  711. default:
  712. d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal unquoted value into %v", subv.Type()))
  713. }
  714. } else {
  715. if err := d.value(subv); err != nil {
  716. return err
  717. }
  718. }
  719. // Write value back to map;
  720. // if using struct, subv points into struct already.
  721. if v.Kind() == reflect.Map {
  722. kt := t.Key()
  723. var kv reflect.Value
  724. switch {
  725. case reflect.PtrTo(kt).Implements(textUnmarshalerType):
  726. kv = reflect.New(kt)
  727. if err := d.literalStore(item, kv, true); err != nil {
  728. return err
  729. }
  730. kv = kv.Elem()
  731. case kt.Kind() == reflect.String:
  732. kv = reflect.ValueOf(key).Convert(kt)
  733. default:
  734. switch kt.Kind() {
  735. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  736. s := string(key)
  737. n, err := strconv.ParseInt(s, 10, 64)
  738. if err != nil || reflect.Zero(kt).OverflowInt(n) {
  739. d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: kt, Offset: int64(start + 1)})
  740. break
  741. }
  742. kv = reflect.ValueOf(n).Convert(kt)
  743. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  744. s := string(key)
  745. n, err := strconv.ParseUint(s, 10, 64)
  746. if err != nil || reflect.Zero(kt).OverflowUint(n) {
  747. d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: kt, Offset: int64(start + 1)})
  748. break
  749. }
  750. kv = reflect.ValueOf(n).Convert(kt)
  751. default:
  752. panic("json: Unexpected key type") // should never occur
  753. }
  754. }
  755. if kv.IsValid() {
  756. if !d.isDiscriminatorSet() || kv.String() != d.discriminatorTypeFieldName {
  757. v.SetMapIndex(kv, subv)
  758. }
  759. }
  760. }
  761. // Next token must be , or }.
  762. if d.opcode == scanSkipSpace {
  763. d.scanWhile(scanSkipSpace)
  764. }
  765. if d.errorContext != nil {
  766. // Reset errorContext to its original state.
  767. // Keep the same underlying array for FieldStack, to reuse the
  768. // space and avoid unnecessary allocs.
  769. d.errorContext.FieldStack = d.errorContext.FieldStack[:len(origErrorContext.FieldStack)]
  770. d.errorContext.Struct = origErrorContext.Struct
  771. }
  772. if d.opcode == scanEndObject {
  773. break
  774. }
  775. if d.opcode != scanObjectValue {
  776. panic(phasePanicMsg)
  777. }
  778. }
  779. return nil
  780. }
  781. // convertNumber converts the number literal s to a float64 or a Number
  782. // depending on the setting of d.useNumber.
  783. func (d *decodeState) convertNumber(s string) (interface{}, error) {
  784. if d.useNumber {
  785. return Number(s), nil
  786. }
  787. f, err := strconv.ParseFloat(s, 64)
  788. if err != nil {
  789. return nil, &UnmarshalTypeError{Value: "number " + s, Type: reflect.TypeOf(0.0), Offset: int64(d.off)}
  790. }
  791. return f, nil
  792. }
  793. var numberType = reflect.TypeOf(Number(""))
  794. // literalStore decodes a literal stored in item into v.
  795. //
  796. // fromQuoted indicates whether this literal came from unwrapping a
  797. // string from the ",string" struct tag option. this is used only to
  798. // produce more helpful error messages.
  799. func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool) error {
  800. // Check for unmarshaler.
  801. if len(item) == 0 {
  802. //Empty string given
  803. d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
  804. return nil
  805. }
  806. isNull := item[0] == 'n' // null
  807. u, ut, pv := indirect(v, isNull)
  808. if u != nil {
  809. return u.UnmarshalJSON(item)
  810. }
  811. if ut != nil {
  812. if item[0] != '"' {
  813. if fromQuoted {
  814. d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
  815. return nil
  816. }
  817. val := "number"
  818. switch item[0] {
  819. case 'n':
  820. val = "null"
  821. case 't', 'f':
  822. val = "bool"
  823. }
  824. d.saveError(&UnmarshalTypeError{Value: val, Type: v.Type(), Offset: int64(d.readIndex())})
  825. return nil
  826. }
  827. s, ok := unquoteBytes(item)
  828. if !ok {
  829. if fromQuoted {
  830. return fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())
  831. }
  832. panic(phasePanicMsg)
  833. }
  834. return ut.UnmarshalText(s)
  835. }
  836. v = pv
  837. switch c := item[0]; c {
  838. case 'n': // null
  839. // The main parser checks that only true and false can reach here,
  840. // but if this was a quoted string input, it could be anything.
  841. if fromQuoted && string(item) != "null" {
  842. d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
  843. break
  844. }
  845. switch v.Kind() {
  846. case reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice:
  847. v.Set(reflect.Zero(v.Type()))
  848. // otherwise, ignore null for primitives/string
  849. }
  850. case 't', 'f': // true, false
  851. value := item[0] == 't'
  852. // The main parser checks that only true and false can reach here,
  853. // but if this was a quoted string input, it could be anything.
  854. if fromQuoted && string(item) != "true" && string(item) != "false" {
  855. d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
  856. break
  857. }
  858. switch v.Kind() {
  859. default:
  860. if fromQuoted {
  861. d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
  862. } else {
  863. d.saveError(&UnmarshalTypeError{Value: "bool", Type: v.Type(), Offset: int64(d.readIndex())})
  864. }
  865. case reflect.Bool:
  866. v.SetBool(value)
  867. case reflect.Interface:
  868. if v.NumMethod() == 0 {
  869. v.Set(reflect.ValueOf(value))
  870. } else {
  871. d.saveError(&UnmarshalTypeError{Value: "bool", Type: v.Type(), Offset: int64(d.readIndex())})
  872. }
  873. }
  874. case '"': // string
  875. s, ok := unquoteBytes(item)
  876. if !ok {
  877. if fromQuoted {
  878. return fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())
  879. }
  880. panic(phasePanicMsg)
  881. }
  882. switch v.Kind() {
  883. default:
  884. d.saveError(&UnmarshalTypeError{Value: "string", Type: v.Type(), Offset: int64(d.readIndex())})
  885. case reflect.Slice:
  886. if v.Type().Elem().Kind() != reflect.Uint8 {
  887. d.saveError(&UnmarshalTypeError{Value: "string", Type: v.Type(), Offset: int64(d.readIndex())})
  888. break
  889. }
  890. b := make([]byte, base64.StdEncoding.DecodedLen(len(s)))
  891. n, err := base64.StdEncoding.Decode(b, s)
  892. if err != nil {
  893. d.saveError(err)
  894. break
  895. }
  896. v.SetBytes(b[:n])
  897. case reflect.String:
  898. if v.Type() == numberType && !isValidNumber(string(s)) {
  899. return fmt.Errorf("json: invalid number literal, trying to unmarshal %q into Number", item)
  900. }
  901. v.SetString(string(s))
  902. case reflect.Interface:
  903. if v.NumMethod() == 0 {
  904. v.Set(reflect.ValueOf(string(s)))
  905. } else {
  906. d.saveError(&UnmarshalTypeError{Value: "string", Type: v.Type(), Offset: int64(d.readIndex())})
  907. }
  908. }
  909. default: // number
  910. if c != '-' && (c < '0' || c > '9') {
  911. if fromQuoted {
  912. return fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())
  913. }
  914. panic(phasePanicMsg)
  915. }
  916. s := string(item)
  917. switch v.Kind() {
  918. default:
  919. if v.Kind() == reflect.String && v.Type() == numberType {
  920. // s must be a valid number, because it's
  921. // already been tokenized.
  922. v.SetString(s)
  923. break
  924. }
  925. if fromQuoted {
  926. return fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())
  927. }
  928. d.saveError(&UnmarshalTypeError{Value: "number", Type: v.Type(), Offset: int64(d.readIndex())})
  929. case reflect.Interface:
  930. n, err := d.convertNumber(s)
  931. if err != nil {
  932. d.saveError(err)
  933. break
  934. }
  935. if v.NumMethod() != 0 {
  936. d.saveError(&UnmarshalTypeError{Value: "number", Type: v.Type(), Offset: int64(d.readIndex())})
  937. break
  938. }
  939. v.Set(reflect.ValueOf(n))
  940. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  941. n, err := strconv.ParseInt(s, 10, 64)
  942. if err != nil || v.OverflowInt(n) {
  943. d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: v.Type(), Offset: int64(d.readIndex())})
  944. break
  945. }
  946. v.SetInt(n)
  947. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  948. n, err := strconv.ParseUint(s, 10, 64)
  949. if err != nil || v.OverflowUint(n) {
  950. d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: v.Type(), Offset: int64(d.readIndex())})
  951. break
  952. }
  953. v.SetUint(n)
  954. case reflect.Float32, reflect.Float64:
  955. n, err := strconv.ParseFloat(s, v.Type().Bits())
  956. if err != nil || v.OverflowFloat(n) {
  957. d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: v.Type(), Offset: int64(d.readIndex())})
  958. break
  959. }
  960. v.SetFloat(n)
  961. }
  962. }
  963. return nil
  964. }
  965. // The xxxInterface routines build up a value to be stored
  966. // in an empty interface. They are not strictly necessary,
  967. // but they avoid the weight of reflection in this common case.
  968. // valueInterface is like value but returns interface{}
  969. func (d *decodeState) valueInterface() (val interface{}) {
  970. switch d.opcode {
  971. default:
  972. panic(phasePanicMsg)
  973. case scanBeginArray:
  974. val = d.arrayInterface()
  975. d.scanNext()
  976. case scanBeginObject:
  977. val = d.objectInterface()
  978. d.scanNext()
  979. case scanBeginLiteral:
  980. val = d.literalInterface()
  981. }
  982. return
  983. }
  984. // arrayInterface is like array but returns []interface{}.
  985. func (d *decodeState) arrayInterface() []interface{} {
  986. var v = make([]interface{}, 0)
  987. for {
  988. // Look ahead for ] - can only happen on first iteration.
  989. d.scanWhile(scanSkipSpace)
  990. if d.opcode == scanEndArray {
  991. break
  992. }
  993. v = append(v, d.valueInterface())
  994. // Next token must be , or ].
  995. if d.opcode == scanSkipSpace {
  996. d.scanWhile(scanSkipSpace)
  997. }
  998. if d.opcode == scanEndArray {
  999. break
  1000. }
  1001. if d.opcode != scanArrayValue {
  1002. panic(phasePanicMsg)
  1003. }
  1004. }
  1005. return v
  1006. }
  1007. // objectInterface is like object but returns map[string]interface{}.
  1008. func (d *decodeState) objectInterface() map[string]interface{} {
  1009. m := make(map[string]interface{})
  1010. for {
  1011. // Read opening " of string key or closing }.
  1012. d.scanWhile(scanSkipSpace)
  1013. if d.opcode == scanEndObject {
  1014. // closing } - can only happen on first iteration.
  1015. break
  1016. }
  1017. if d.opcode != scanBeginLiteral {
  1018. panic(phasePanicMsg)
  1019. }
  1020. // Read string key.
  1021. start := d.readIndex()
  1022. d.rescanLiteral()
  1023. item := d.data[start:d.readIndex()]
  1024. key, ok := unquote(item)
  1025. if !ok {
  1026. panic(phasePanicMsg)
  1027. }
  1028. // Read : before value.
  1029. if d.opcode == scanSkipSpace {
  1030. d.scanWhile(scanSkipSpace)
  1031. }
  1032. if d.opcode != scanObjectKey {
  1033. panic(phasePanicMsg)
  1034. }
  1035. d.scanWhile(scanSkipSpace)
  1036. // Read value.
  1037. m[key] = d.valueInterface()
  1038. // Next token must be , or }.
  1039. if d.opcode == scanSkipSpace {
  1040. d.scanWhile(scanSkipSpace)
  1041. }
  1042. if d.opcode == scanEndObject {
  1043. break
  1044. }
  1045. if d.opcode != scanObjectValue {
  1046. panic(phasePanicMsg)
  1047. }
  1048. }
  1049. return m
  1050. }
  1051. // literalInterface consumes and returns a literal from d.data[d.off-1:] and
  1052. // it reads the following byte ahead. The first byte of the literal has been
  1053. // read already (that's how the caller knows it's a literal).
  1054. func (d *decodeState) literalInterface() interface{} {
  1055. // All bytes inside literal return scanContinue op code.
  1056. start := d.readIndex()
  1057. d.rescanLiteral()
  1058. item := d.data[start:d.readIndex()]
  1059. switch c := item[0]; c {
  1060. case 'n': // null
  1061. return nil
  1062. case 't', 'f': // true, false
  1063. return c == 't'
  1064. case '"': // string
  1065. s, ok := unquote(item)
  1066. if !ok {
  1067. panic(phasePanicMsg)
  1068. }
  1069. return s
  1070. default: // number
  1071. if c != '-' && (c < '0' || c > '9') {
  1072. panic(phasePanicMsg)
  1073. }
  1074. n, err := d.convertNumber(string(item))
  1075. if err != nil {
  1076. d.saveError(err)
  1077. }
  1078. return n
  1079. }
  1080. }
  1081. // getu4 decodes \uXXXX from the beginning of s, returning the hex value,
  1082. // or it returns -1.
  1083. func getu4(s []byte) rune {
  1084. if len(s) < 6 || s[0] != '\\' || s[1] != 'u' {
  1085. return -1
  1086. }
  1087. var r rune
  1088. for _, c := range s[2:6] {
  1089. switch {
  1090. case '0' <= c && c <= '9':
  1091. c = c - '0'
  1092. case 'a' <= c && c <= 'f':
  1093. c = c - 'a' + 10
  1094. case 'A' <= c && c <= 'F':
  1095. c = c - 'A' + 10
  1096. default:
  1097. return -1
  1098. }
  1099. r = r*16 + rune(c)
  1100. }
  1101. return r
  1102. }
  1103. // unquote converts a quoted JSON string literal s into an actual string t.
  1104. // The rules are different than for Go, so cannot use strconv.Unquote.
  1105. func unquote(s []byte) (t string, ok bool) {
  1106. s, ok = unquoteBytes(s)
  1107. t = string(s)
  1108. return
  1109. }
  1110. func unquoteBytes(s []byte) (t []byte, ok bool) {
  1111. if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' {
  1112. return
  1113. }
  1114. s = s[1 : len(s)-1]
  1115. // Check for unusual characters. If there are none,
  1116. // then no unquoting is needed, so return a slice of the
  1117. // original bytes.
  1118. r := 0
  1119. for r < len(s) {
  1120. c := s[r]
  1121. if c == '\\' || c == '"' || c < ' ' {
  1122. break
  1123. }
  1124. if c < utf8.RuneSelf {
  1125. r++
  1126. continue
  1127. }
  1128. rr, size := utf8.DecodeRune(s[r:])
  1129. if rr == utf8.RuneError && size == 1 {
  1130. break
  1131. }
  1132. r += size
  1133. }
  1134. if r == len(s) {
  1135. return s, true
  1136. }
  1137. b := make([]byte, len(s)+2*utf8.UTFMax)
  1138. w := copy(b, s[0:r])
  1139. for r < len(s) {
  1140. // Out of room? Can only happen if s is full of
  1141. // malformed UTF-8 and we're replacing each
  1142. // byte with RuneError.
  1143. if w >= len(b)-2*utf8.UTFMax {
  1144. nb := make([]byte, (len(b)+utf8.UTFMax)*2)
  1145. copy(nb, b[0:w])
  1146. b = nb
  1147. }
  1148. switch c := s[r]; {
  1149. case c == '\\':
  1150. r++
  1151. if r >= len(s) {
  1152. return
  1153. }
  1154. switch s[r] {
  1155. default:
  1156. return
  1157. case '"', '\\', '/', '\'':
  1158. b[w] = s[r]
  1159. r++
  1160. w++
  1161. case 'b':
  1162. b[w] = '\b'
  1163. r++
  1164. w++
  1165. case 'f':
  1166. b[w] = '\f'
  1167. r++
  1168. w++
  1169. case 'n':
  1170. b[w] = '\n'
  1171. r++
  1172. w++
  1173. case 'r':
  1174. b[w] = '\r'
  1175. r++
  1176. w++
  1177. case 't':
  1178. b[w] = '\t'
  1179. r++
  1180. w++
  1181. case 'u':
  1182. r--
  1183. rr := getu4(s[r:])
  1184. if rr < 0 {
  1185. return
  1186. }
  1187. r += 6
  1188. if utf16.IsSurrogate(rr) {
  1189. rr1 := getu4(s[r:])
  1190. if dec := utf16.DecodeRune(rr, rr1); dec != unicode.ReplacementChar {
  1191. // A valid pair; consume.
  1192. r += 6
  1193. w += utf8.EncodeRune(b[w:], dec)
  1194. break
  1195. }
  1196. // Invalid surrogate; fall back to replacement rune.
  1197. rr = unicode.ReplacementChar
  1198. }
  1199. w += utf8.EncodeRune(b[w:], rr)
  1200. }
  1201. // Quote, control characters are invalid.
  1202. case c == '"', c < ' ':
  1203. return
  1204. // ASCII
  1205. case c < utf8.RuneSelf:
  1206. b[w] = c
  1207. r++
  1208. w++
  1209. // Coerce to well-formed UTF-8.
  1210. default:
  1211. rr, size := utf8.DecodeRune(s[r:])
  1212. r += size
  1213. w += utf8.EncodeRune(b[w:], rr)
  1214. }
  1215. }
  1216. return b[0:w], true
  1217. }