decode.go 29 KB

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