json.go 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  1. // Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a MIT license found in the LICENSE file.
  3. package codec
  4. // By default, this json support uses base64 encoding for bytes, because you cannot
  5. // store and read any arbitrary string in json (only unicode).
  6. // However, the user can configre how to encode/decode bytes.
  7. //
  8. // This library specifically supports UTF-8 for encoding and decoding only.
  9. //
  10. // Note that the library will happily encode/decode things which are not valid
  11. // json e.g. a map[int64]string. We do it for consistency. With valid json,
  12. // we will encode and decode appropriately.
  13. // Users can specify their map type if necessary to force it.
  14. //
  15. // Note:
  16. // - we cannot use strconv.Quote and strconv.Unquote because json quotes/unquotes differently.
  17. // We implement it here.
  18. // - Also, strconv.ParseXXX for floats and integers
  19. // - only works on strings resulting in unnecessary allocation and []byte-string conversion.
  20. // - it does a lot of redundant checks, because json numbers are simpler that what it supports.
  21. // - We parse numbers (floats and integers) directly here.
  22. // We only delegate parsing floats if it is a hairy float which could cause a loss of precision.
  23. // In that case, we delegate to strconv.ParseFloat.
  24. //
  25. // Note:
  26. // - encode does not beautify. There is no whitespace when encoding.
  27. // - rpc calls which take single integer arguments or write single numeric arguments will need care.
  28. // Top-level methods of json(End|Dec)Driver (which are implementations of (en|de)cDriver
  29. // MUST not call one-another.
  30. import (
  31. "bytes"
  32. "encoding/base64"
  33. "reflect"
  34. "strconv"
  35. "unicode"
  36. "unicode/utf16"
  37. "unicode/utf8"
  38. )
  39. //--------------------------------
  40. var jsonLiterals = [...]byte{
  41. '"',
  42. 't', 'r', 'u', 'e',
  43. '"',
  44. '"',
  45. 'f', 'a', 'l', 's', 'e',
  46. '"',
  47. '"',
  48. 'n', 'u', 'l', 'l',
  49. '"',
  50. }
  51. const (
  52. jsonLitTrueQ = 0
  53. jsonLitTrue = 1
  54. jsonLitFalseQ = 6
  55. jsonLitFalse = 7
  56. jsonLitNullQ = 13
  57. jsonLitNull = 14
  58. )
  59. var (
  60. // jsonFloat64Pow10 = [...]float64{
  61. // 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
  62. // 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
  63. // 1e20, 1e21, 1e22,
  64. // }
  65. // jsonUint64Pow10 = [...]uint64{
  66. // 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
  67. // 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
  68. // }
  69. // jsonTabs and jsonSpaces are used as caches for indents
  70. jsonTabs, jsonSpaces string
  71. jsonCharHtmlSafeSet bitset128
  72. jsonCharSafeSet bitset128
  73. jsonCharWhitespaceSet bitset256
  74. jsonNumSet bitset256
  75. // jsonIsFloatSet bitset256
  76. jsonU4Set [256]byte
  77. )
  78. const (
  79. // If !jsonValidateSymbols, decoding will be faster, by skipping some checks:
  80. // - If we see first character of null, false or true,
  81. // do not validate subsequent characters.
  82. // - e.g. if we see a n, assume null and skip next 3 characters,
  83. // and do not validate they are ull.
  84. // P.S. Do not expect a significant decoding boost from this.
  85. jsonValidateSymbols = true
  86. jsonSpacesOrTabsLen = 128
  87. jsonU4SetErrVal = 128
  88. jsonAlwaysReturnInternString = false
  89. )
  90. func init() {
  91. var bs [jsonSpacesOrTabsLen]byte
  92. for i := 0; i < jsonSpacesOrTabsLen; i++ {
  93. bs[i] = ' '
  94. }
  95. jsonSpaces = string(bs[:])
  96. for i := 0; i < jsonSpacesOrTabsLen; i++ {
  97. bs[i] = '\t'
  98. }
  99. jsonTabs = string(bs[:])
  100. // populate the safe values as true: note: ASCII control characters are (0-31)
  101. // jsonCharSafeSet: all true except (0-31) " \
  102. // jsonCharHtmlSafeSet: all true except (0-31) " \ < > &
  103. var i byte
  104. for i = 32; i < utf8.RuneSelf; i++ {
  105. switch i {
  106. case '"', '\\':
  107. case '<', '>', '&':
  108. jsonCharSafeSet.set(i) // = true
  109. default:
  110. jsonCharSafeSet.set(i)
  111. jsonCharHtmlSafeSet.set(i)
  112. }
  113. }
  114. for i = 0; i <= utf8.RuneSelf; i++ {
  115. switch i {
  116. case ' ', '\t', '\r', '\n':
  117. jsonCharWhitespaceSet.set(i)
  118. case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'e', 'E', '.', '+', '-':
  119. jsonNumSet.set(i)
  120. }
  121. }
  122. for j := range jsonU4Set {
  123. switch i = byte(j); i {
  124. case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  125. jsonU4Set[i] = i - '0'
  126. case 'a', 'b', 'c', 'd', 'e', 'f':
  127. jsonU4Set[i] = i - 'a' + 10
  128. case 'A', 'B', 'C', 'D', 'E', 'F':
  129. jsonU4Set[i] = i - 'A' + 10
  130. default:
  131. jsonU4Set[i] = jsonU4SetErrVal
  132. }
  133. // switch i = byte(j); i {
  134. // case 'e', 'E', '.':
  135. // jsonIsFloatSet.set(i)
  136. // }
  137. }
  138. // jsonU4Set[255] = jsonU4SetErrVal
  139. }
  140. type jsonEncDriver struct {
  141. e *Encoder
  142. w encWriter
  143. h *JsonHandle
  144. b [64]byte // scratch
  145. bs []byte // scratch
  146. se setExtWrapper
  147. ds string // indent string
  148. dl uint16 // indent level
  149. dt bool // indent using tabs
  150. d bool // indent
  151. c containerState
  152. noBuiltInTypes
  153. }
  154. // indent is done as below:
  155. // - newline and indent are added before each mapKey or arrayElem
  156. // - newline and indent are added before each ending,
  157. // except there was no entry (so we can have {} or [])
  158. func (e *jsonEncDriver) WriteArrayStart(length int) {
  159. if e.d {
  160. e.dl++
  161. }
  162. e.w.writen1('[')
  163. e.c = containerArrayStart
  164. }
  165. func (e *jsonEncDriver) WriteArrayElem() {
  166. if e.c != containerArrayStart {
  167. e.w.writen1(',')
  168. }
  169. if e.d {
  170. e.writeIndent()
  171. }
  172. e.c = containerArrayElem
  173. }
  174. func (e *jsonEncDriver) WriteArrayEnd() {
  175. if e.d {
  176. e.dl--
  177. if e.c != containerArrayStart {
  178. e.writeIndent()
  179. }
  180. }
  181. e.w.writen1(']')
  182. e.c = containerArrayEnd
  183. }
  184. func (e *jsonEncDriver) WriteMapStart(length int) {
  185. if e.d {
  186. e.dl++
  187. }
  188. e.w.writen1('{')
  189. e.c = containerMapStart
  190. }
  191. func (e *jsonEncDriver) WriteMapElemKey() {
  192. if e.c != containerMapStart {
  193. e.w.writen1(',')
  194. }
  195. if e.d {
  196. e.writeIndent()
  197. }
  198. e.c = containerMapKey
  199. }
  200. func (e *jsonEncDriver) WriteMapElemValue() {
  201. if e.d {
  202. e.w.writen2(':', ' ')
  203. } else {
  204. e.w.writen1(':')
  205. }
  206. e.c = containerMapValue
  207. }
  208. func (e *jsonEncDriver) WriteMapEnd() {
  209. if e.d {
  210. e.dl--
  211. if e.c != containerMapStart {
  212. e.writeIndent()
  213. }
  214. }
  215. e.w.writen1('}')
  216. e.c = containerMapEnd
  217. }
  218. func (e *jsonEncDriver) writeIndent() {
  219. e.w.writen1('\n')
  220. if x := len(e.ds) * int(e.dl); x <= jsonSpacesOrTabsLen {
  221. if e.dt {
  222. e.w.writestr(jsonTabs[:x])
  223. } else {
  224. e.w.writestr(jsonSpaces[:x])
  225. }
  226. } else {
  227. for i := uint16(0); i < e.dl; i++ {
  228. e.w.writestr(e.ds)
  229. }
  230. }
  231. }
  232. func (e *jsonEncDriver) EncodeNil() {
  233. // We always encode nil as just null (never in quotes)
  234. // This allows us to easily decode if a nil in the json stream
  235. // ie if initial token is n.
  236. e.w.writeb(jsonLiterals[jsonLitNull : jsonLitNull+4])
  237. // if e.h.MapKeyAsString && e.c == containerMapKey {
  238. // e.w.writeb(jsonLiterals[jsonLitNullQ : jsonLitNullQ+6])
  239. // } else {
  240. // e.w.writeb(jsonLiterals[jsonLitNull : jsonLitNull+4])
  241. // }
  242. }
  243. func (e *jsonEncDriver) EncodeBool(b bool) {
  244. if e.h.MapKeyAsString && e.c == containerMapKey {
  245. if b {
  246. e.w.writeb(jsonLiterals[jsonLitTrueQ : jsonLitTrueQ+6])
  247. } else {
  248. e.w.writeb(jsonLiterals[jsonLitFalseQ : jsonLitFalseQ+7])
  249. }
  250. } else {
  251. if b {
  252. e.w.writeb(jsonLiterals[jsonLitTrue : jsonLitTrue+4])
  253. } else {
  254. e.w.writeb(jsonLiterals[jsonLitFalse : jsonLitFalse+5])
  255. }
  256. }
  257. }
  258. func (e *jsonEncDriver) EncodeFloat32(f float32) {
  259. e.encodeFloat(float64(f), 32)
  260. }
  261. func (e *jsonEncDriver) EncodeFloat64(f float64) {
  262. e.encodeFloat(f, 64)
  263. }
  264. func (e *jsonEncDriver) encodeFloat(f float64, numbits int) {
  265. var blen int
  266. var x []byte
  267. if e.h.MapKeyAsString && e.c == containerMapKey {
  268. e.b[0] = '"'
  269. x = strconv.AppendFloat(e.b[1:1], f, 'G', -1, numbits)
  270. blen = 1 + len(x)
  271. if jsonIsFloatBytesB2(x) {
  272. e.b[blen] = '"'
  273. blen += 1
  274. } else {
  275. e.b[blen] = '.'
  276. e.b[blen+1] = '0'
  277. e.b[blen+2] = '"'
  278. blen += 3
  279. }
  280. } else {
  281. x = strconv.AppendFloat(e.b[:0], f, 'G', -1, numbits)
  282. blen = len(x)
  283. if !jsonIsFloatBytesB2(x) {
  284. e.b[blen] = '.'
  285. e.b[blen+1] = '0'
  286. blen += 2
  287. }
  288. }
  289. e.w.writeb(e.b[:blen])
  290. }
  291. func (e *jsonEncDriver) EncodeInt(v int64) {
  292. x := e.h.IntegerAsString
  293. if x == 'A' || x == 'L' && (v > 1<<53 || v < -(1<<53)) || (e.h.MapKeyAsString && e.c == containerMapKey) {
  294. blen := 2 + len(strconv.AppendInt(e.b[1:1], v, 10))
  295. e.b[0] = '"'
  296. e.b[blen-1] = '"'
  297. e.w.writeb(e.b[:blen])
  298. return
  299. }
  300. e.w.writeb(strconv.AppendInt(e.b[:0], v, 10))
  301. }
  302. func (e *jsonEncDriver) EncodeUint(v uint64) {
  303. x := e.h.IntegerAsString
  304. if x == 'A' || x == 'L' && v > 1<<53 || (e.h.MapKeyAsString && e.c == containerMapKey) {
  305. blen := 2 + len(strconv.AppendUint(e.b[1:1], v, 10))
  306. e.b[0] = '"'
  307. e.b[blen-1] = '"'
  308. e.w.writeb(e.b[:blen])
  309. return
  310. }
  311. e.w.writeb(strconv.AppendUint(e.b[:0], v, 10))
  312. }
  313. func (e *jsonEncDriver) EncodeExt(rv interface{}, xtag uint64, ext Ext, en *Encoder) {
  314. if v := ext.ConvertExt(rv); v == nil {
  315. e.EncodeNil()
  316. } else {
  317. en.encode(v)
  318. }
  319. }
  320. func (e *jsonEncDriver) EncodeRawExt(re *RawExt, en *Encoder) {
  321. // only encodes re.Value (never re.Data)
  322. if re.Value == nil {
  323. e.EncodeNil()
  324. } else {
  325. en.encode(re.Value)
  326. }
  327. }
  328. func (e *jsonEncDriver) EncodeString(c charEncoding, v string) {
  329. e.quoteStr(v)
  330. }
  331. func (e *jsonEncDriver) EncodeSymbol(v string) {
  332. e.quoteStr(v)
  333. }
  334. func (e *jsonEncDriver) EncodeStringBytes(c charEncoding, v []byte) {
  335. // if encoding raw bytes and RawBytesExt is configured, use it to encode
  336. if c == cRAW {
  337. if e.se.i != nil {
  338. e.EncodeExt(v, 0, &e.se, e.e)
  339. return
  340. }
  341. slen := base64.StdEncoding.EncodedLen(len(v))
  342. if cap(e.bs) >= slen {
  343. e.bs = e.bs[:slen]
  344. } else {
  345. e.bs = make([]byte, slen)
  346. }
  347. base64.StdEncoding.Encode(e.bs, v)
  348. e.w.writen1('"')
  349. e.w.writeb(e.bs)
  350. e.w.writen1('"')
  351. } else {
  352. e.quoteStr(stringView(v))
  353. }
  354. }
  355. func (e *jsonEncDriver) EncodeAsis(v []byte) {
  356. e.w.writeb(v)
  357. }
  358. func (e *jsonEncDriver) quoteStr(s string) {
  359. // adapted from std pkg encoding/json
  360. const hex = "0123456789abcdef"
  361. w := e.w
  362. w.writen1('"')
  363. var start int
  364. for i, slen := 0, len(s); i < slen; {
  365. // encode all bytes < 0x20 (except \r, \n).
  366. // also encode < > & to prevent security holes when served to some browsers.
  367. if b := s[i]; b < utf8.RuneSelf {
  368. // if 0x20 <= b && b != '\\' && b != '"' && b != '<' && b != '>' && b != '&' {
  369. if jsonCharHtmlSafeSet.isset(b) || (e.h.HTMLCharsAsIs && jsonCharSafeSet.isset(b)) {
  370. i++
  371. continue
  372. }
  373. if start < i {
  374. w.writestr(s[start:i])
  375. }
  376. switch b {
  377. case '\\', '"':
  378. w.writen2('\\', b)
  379. case '\n':
  380. w.writen2('\\', 'n')
  381. case '\r':
  382. w.writen2('\\', 'r')
  383. case '\b':
  384. w.writen2('\\', 'b')
  385. case '\f':
  386. w.writen2('\\', 'f')
  387. case '\t':
  388. w.writen2('\\', 't')
  389. default:
  390. w.writestr(`\u00`)
  391. w.writen2(hex[b>>4], hex[b&0xF])
  392. }
  393. i++
  394. start = i
  395. continue
  396. }
  397. c, size := utf8.DecodeRuneInString(s[i:])
  398. if c == utf8.RuneError && size == 1 {
  399. if start < i {
  400. w.writestr(s[start:i])
  401. }
  402. w.writestr(`\ufffd`)
  403. i += size
  404. start = i
  405. continue
  406. }
  407. // U+2028 is LINE SEPARATOR. U+2029 is PARAGRAPH SEPARATOR.
  408. // Both technically valid JSON, but bomb on JSONP, so fix here unconditionally.
  409. if c == '\u2028' || c == '\u2029' {
  410. if start < i {
  411. w.writestr(s[start:i])
  412. }
  413. w.writestr(`\u202`)
  414. w.writen1(hex[c&0xF])
  415. i += size
  416. start = i
  417. continue
  418. }
  419. i += size
  420. }
  421. if start < len(s) {
  422. w.writestr(s[start:])
  423. }
  424. w.writen1('"')
  425. }
  426. func (e *jsonEncDriver) atEndOfEncode() {
  427. if e.h.TermWhitespace {
  428. if e.d {
  429. e.w.writen1('\n')
  430. } else {
  431. e.w.writen1(' ')
  432. }
  433. }
  434. }
  435. type jsonDecDriver struct {
  436. noBuiltInTypes
  437. d *Decoder
  438. h *JsonHandle
  439. r decReader
  440. c containerState
  441. // tok is used to store the token read right after skipWhiteSpace.
  442. tok uint8
  443. fnull bool // found null from appendStringAsBytes
  444. bstr [8]byte // scratch used for string \UXXX parsing
  445. b [64]byte // scratch, used for parsing strings or numbers
  446. b2 [64]byte // scratch, used only for decodeBytes (after base64)
  447. bs []byte // scratch. Initialized from b. Used for parsing strings or numbers.
  448. se setExtWrapper
  449. // n jsonNum
  450. }
  451. func jsonIsWS(b byte) bool {
  452. // return b == ' ' || b == '\t' || b == '\r' || b == '\n'
  453. return jsonCharWhitespaceSet.isset(b)
  454. }
  455. func (d *jsonDecDriver) uncacheRead() {
  456. if d.tok != 0 {
  457. d.r.unreadn1()
  458. d.tok = 0
  459. }
  460. }
  461. func (d *jsonDecDriver) ReadMapStart() int {
  462. if d.tok == 0 {
  463. d.tok = d.r.skip(&jsonCharWhitespaceSet)
  464. }
  465. if d.tok != '{' {
  466. d.d.errorf("json: expect char '%c' but got char '%c'", '{', d.tok)
  467. }
  468. d.tok = 0
  469. d.c = containerMapStart
  470. return -1
  471. }
  472. func (d *jsonDecDriver) ReadArrayStart() int {
  473. if d.tok == 0 {
  474. d.tok = d.r.skip(&jsonCharWhitespaceSet)
  475. }
  476. if d.tok != '[' {
  477. d.d.errorf("json: expect char '%c' but got char '%c'", '[', d.tok)
  478. }
  479. d.tok = 0
  480. d.c = containerArrayStart
  481. return -1
  482. }
  483. func (d *jsonDecDriver) CheckBreak() bool {
  484. if d.tok == 0 {
  485. d.tok = d.r.skip(&jsonCharWhitespaceSet)
  486. }
  487. return d.tok == '}' || d.tok == ']'
  488. }
  489. func (d *jsonDecDriver) ReadArrayElem() {
  490. if d.tok == 0 {
  491. d.tok = d.r.skip(&jsonCharWhitespaceSet)
  492. }
  493. if d.c != containerArrayStart {
  494. const xc uint8 = ','
  495. if d.tok != xc {
  496. d.d.errorf("json: expect char '%c' but got char '%c'", xc, d.tok)
  497. }
  498. d.tok = 0
  499. }
  500. d.c = containerArrayElem
  501. }
  502. func (d *jsonDecDriver) ReadArrayEnd() {
  503. if d.tok == 0 {
  504. d.tok = d.r.skip(&jsonCharWhitespaceSet)
  505. }
  506. const xc uint8 = ']'
  507. if d.tok != xc {
  508. d.d.errorf("json: expect char '%c' but got char '%c'", xc, d.tok)
  509. }
  510. d.tok = 0
  511. d.c = containerArrayEnd
  512. }
  513. func (d *jsonDecDriver) ReadMapElemKey() {
  514. if d.tok == 0 {
  515. d.tok = d.r.skip(&jsonCharWhitespaceSet)
  516. }
  517. if d.c != containerMapStart {
  518. const xc uint8 = ','
  519. if d.tok != xc {
  520. d.d.errorf("json: expect char '%c' but got char '%c'", xc, d.tok)
  521. }
  522. d.tok = 0
  523. }
  524. d.c = containerMapKey
  525. }
  526. func (d *jsonDecDriver) ReadMapElemValue() {
  527. if d.tok == 0 {
  528. d.tok = d.r.skip(&jsonCharWhitespaceSet)
  529. }
  530. const xc uint8 = ':'
  531. if d.tok != xc {
  532. d.d.errorf("json: expect char '%c' but got char '%c'", xc, d.tok)
  533. }
  534. d.tok = 0
  535. d.c = containerMapValue
  536. }
  537. func (d *jsonDecDriver) ReadMapEnd() {
  538. if d.tok == 0 {
  539. d.tok = d.r.skip(&jsonCharWhitespaceSet)
  540. }
  541. const xc uint8 = '}'
  542. if d.tok != xc {
  543. d.d.errorf("json: expect char '%c' but got char '%c'", xc, d.tok)
  544. }
  545. d.tok = 0
  546. d.c = containerMapEnd
  547. }
  548. // func (d *jsonDecDriver) readContainerState(c containerState, xc uint8, check bool) {
  549. // if d.tok == 0 {
  550. // d.tok = d.r.skip(&jsonCharWhitespaceSet)
  551. // }
  552. // if check {
  553. // if d.tok != xc {
  554. // d.d.errorf("json: expect char '%c' but got char '%c'", xc, d.tok)
  555. // }
  556. // d.tok = 0
  557. // }
  558. // d.c = c
  559. // }
  560. func (d *jsonDecDriver) readLit(length, fromIdx uint8) {
  561. bs := d.r.readx(int(length))
  562. d.tok = 0
  563. if jsonValidateSymbols && !bytes.Equal(bs, jsonLiterals[fromIdx:fromIdx+length]) {
  564. d.d.errorf("json: expecting %s: got %s", jsonLiterals[fromIdx:fromIdx+length], bs)
  565. return
  566. }
  567. }
  568. func (d *jsonDecDriver) TryDecodeAsNil() bool {
  569. if d.tok == 0 {
  570. d.tok = d.r.skip(&jsonCharWhitespaceSet)
  571. }
  572. // TODO: we shouldn't try to see if "null" was here, right?
  573. // only "null" denotes a nil
  574. if d.tok == 'n' {
  575. d.readLit(3, jsonLitNull+1) // (n)ull
  576. return true
  577. }
  578. return false
  579. }
  580. func (d *jsonDecDriver) DecodeBool() (v bool) {
  581. if d.tok == 0 {
  582. d.tok = d.r.skip(&jsonCharWhitespaceSet)
  583. }
  584. fquot := d.c == containerMapKey && d.tok == '"'
  585. if fquot {
  586. d.tok = d.r.readn1()
  587. }
  588. switch d.tok {
  589. case 'f':
  590. d.readLit(4, jsonLitFalse+1) // (f)alse
  591. // v = false
  592. case 't':
  593. d.readLit(3, jsonLitTrue+1) // (t)rue
  594. v = true
  595. default:
  596. d.d.errorf("json: decode bool: got first char %c", d.tok)
  597. // v = false // "unreachable"
  598. }
  599. if fquot {
  600. d.r.readn1()
  601. }
  602. return
  603. }
  604. func (d *jsonDecDriver) ContainerType() (vt valueType) {
  605. // check container type by checking the first char
  606. if d.tok == 0 {
  607. d.tok = d.r.skip(&jsonCharWhitespaceSet)
  608. }
  609. if b := d.tok; b == '{' {
  610. return valueTypeMap
  611. } else if b == '[' {
  612. return valueTypeArray
  613. } else if b == 'n' {
  614. return valueTypeNil
  615. } else if b == '"' {
  616. return valueTypeString
  617. }
  618. return valueTypeUnset
  619. // d.d.errorf("isContainerType: unsupported parameter: %v", vt)
  620. // return false // "unreachable"
  621. }
  622. func (d *jsonDecDriver) decNumBytes() (bs []byte) {
  623. // stores num bytes in d.bs
  624. if d.tok == 0 {
  625. d.tok = d.r.skip(&jsonCharWhitespaceSet)
  626. }
  627. if d.tok == '"' {
  628. bs = d.r.readUntil(d.b2[:0], '"')
  629. bs = bs[:len(bs)-1]
  630. } else {
  631. d.r.unreadn1()
  632. bs = d.r.readTo(d.bs[:0], &jsonNumSet)
  633. }
  634. d.tok = 0
  635. return bs
  636. }
  637. func (d *jsonDecDriver) DecodeUint(bitsize uint8) (u uint64) {
  638. bs := d.decNumBytes()
  639. u, err := strconv.ParseUint(stringView(bs), 10, int(bitsize))
  640. if err != nil {
  641. d.d.errorf("json: decode uint from %s: %v", bs, err)
  642. return
  643. }
  644. return
  645. }
  646. func (d *jsonDecDriver) DecodeInt(bitsize uint8) (i int64) {
  647. bs := d.decNumBytes()
  648. i, err := strconv.ParseInt(stringView(bs), 10, int(bitsize))
  649. if err != nil {
  650. d.d.errorf("json: decode int from %s: %v", bs, err)
  651. return
  652. }
  653. return
  654. }
  655. func (d *jsonDecDriver) DecodeFloat(chkOverflow32 bool) (f float64) {
  656. bs := d.decNumBytes()
  657. bitsize := 64
  658. if chkOverflow32 {
  659. bitsize = 32
  660. }
  661. f, err := strconv.ParseFloat(stringView(bs), bitsize)
  662. if err != nil {
  663. d.d.errorf("json: decode float from %s: %v", bs, err)
  664. return
  665. }
  666. return
  667. }
  668. func (d *jsonDecDriver) DecodeExt(rv interface{}, xtag uint64, ext Ext) (realxtag uint64) {
  669. if ext == nil {
  670. re := rv.(*RawExt)
  671. re.Tag = xtag
  672. d.d.decode(&re.Value)
  673. } else {
  674. var v interface{}
  675. d.d.decode(&v)
  676. ext.UpdateExt(rv, v)
  677. }
  678. return
  679. }
  680. func (d *jsonDecDriver) DecodeBytes(bs []byte, zerocopy bool) (bsOut []byte) {
  681. // if decoding into raw bytes, and the RawBytesExt is configured, use it to decode.
  682. if d.se.i != nil {
  683. bsOut = bs
  684. d.DecodeExt(&bsOut, 0, &d.se)
  685. return
  686. }
  687. d.appendStringAsBytes()
  688. // base64 encodes []byte{} as "", and we encode nil []byte as null.
  689. // Consequently, base64 should decode null as a nil []byte, and "" as an empty []byte{}.
  690. // appendStringAsBytes returns a zero-len slice for both, so as not to reset d.bs.
  691. // However, it sets a fnull field to true, so we can check if a null was found.
  692. if len(d.bs) == 0 {
  693. if d.fnull {
  694. return nil
  695. }
  696. return []byte{}
  697. }
  698. bs0 := d.bs
  699. slen := base64.StdEncoding.DecodedLen(len(bs0))
  700. if slen <= cap(bs) {
  701. bsOut = bs[:slen]
  702. } else if zerocopy && slen <= cap(d.b2) {
  703. bsOut = d.b2[:slen]
  704. } else {
  705. bsOut = make([]byte, slen)
  706. }
  707. slen2, err := base64.StdEncoding.Decode(bsOut, bs0)
  708. if err != nil {
  709. d.d.errorf("json: error decoding base64 binary '%s': %v", bs0, err)
  710. return nil
  711. }
  712. if slen != slen2 {
  713. bsOut = bsOut[:slen2]
  714. }
  715. return
  716. }
  717. func (d *jsonDecDriver) DecodeString() (s string) {
  718. d.appendStringAsBytes()
  719. return d.bsToString()
  720. }
  721. func (d *jsonDecDriver) DecodeStringAsBytes() (s []byte) {
  722. d.appendStringAsBytes()
  723. return d.bs
  724. }
  725. func (d *jsonDecDriver) appendStringAsBytes() {
  726. if d.tok == 0 {
  727. d.tok = d.r.skip(&jsonCharWhitespaceSet)
  728. }
  729. d.fnull = false
  730. if d.tok != '"' {
  731. // d.d.errorf("json: expect char '%c' but got char '%c'", '"', d.tok)
  732. // handle non-string scalar: null, true, false or a number
  733. switch d.tok {
  734. case 'n':
  735. d.readLit(3, jsonLitNull+1) // (n)ull
  736. d.bs = d.bs[:0]
  737. d.fnull = true
  738. case 'f':
  739. d.readLit(4, jsonLitFalse+1) // (f)alse
  740. d.bs = d.bs[:5]
  741. copy(d.bs, "false")
  742. case 't':
  743. d.readLit(3, jsonLitTrue+1) // (t)rue
  744. d.bs = d.bs[:4]
  745. copy(d.bs, "true")
  746. default:
  747. // try to parse a valid number
  748. bs := d.decNumBytes()
  749. d.bs = d.bs[:len(bs)]
  750. copy(d.bs, bs)
  751. }
  752. return
  753. }
  754. d.tok = 0
  755. r := d.r
  756. var cs = r.readUntil(d.b2[:0], '"')
  757. var cslen = len(cs)
  758. var c uint8
  759. v := d.bs[:0]
  760. // append on each byte seen can be expensive, so we just
  761. // keep track of where we last read a contiguous set of
  762. // non-special bytes (using cursor variable),
  763. // and when we see a special byte
  764. // e.g. end-of-slice, " or \,
  765. // we will append the full range into the v slice before proceeding
  766. for i, cursor := 0, 0; ; {
  767. if i == cslen {
  768. v = append(v, cs[cursor:]...)
  769. cs = r.readUntil(d.b2[:0], '"')
  770. cslen = len(cs)
  771. i, cursor = 0, 0
  772. }
  773. c = cs[i]
  774. if c == '"' {
  775. v = append(v, cs[cursor:i]...)
  776. break
  777. }
  778. if c != '\\' {
  779. i++
  780. continue
  781. }
  782. v = append(v, cs[cursor:i]...)
  783. i++
  784. c = cs[i]
  785. switch c {
  786. case '"', '\\', '/', '\'':
  787. v = append(v, c)
  788. case 'b':
  789. v = append(v, '\b')
  790. case 'f':
  791. v = append(v, '\f')
  792. case 'n':
  793. v = append(v, '\n')
  794. case 'r':
  795. v = append(v, '\r')
  796. case 't':
  797. v = append(v, '\t')
  798. case 'u':
  799. var r rune
  800. var rr uint32
  801. if len(cs) < i+4 { // may help reduce bounds-checking
  802. d.d.errorf(`json: need at least 4 more bytes for unicode sequence`)
  803. }
  804. // c = cs[i+4] // may help reduce bounds-checking
  805. for j := 1; j < 5; j++ {
  806. c = jsonU4Set[cs[i+j]]
  807. if c == jsonU4SetErrVal {
  808. // d.d.errorf(`json: unquoteStr: invalid hex char in \u unicode sequence: %q`, c)
  809. r = unicode.ReplacementChar
  810. i += 4
  811. goto encode_rune
  812. }
  813. rr = rr*16 + uint32(c)
  814. }
  815. r = rune(rr)
  816. i += 4
  817. if utf16.IsSurrogate(r) {
  818. if len(cs) >= i+6 && cs[i+2] == 'u' && cs[i+1] == '\\' {
  819. i += 2
  820. // c = cs[i+4] // may help reduce bounds-checking
  821. var rr1 uint32
  822. for j := 1; j < 5; j++ {
  823. c = jsonU4Set[cs[i+j]]
  824. if c == jsonU4SetErrVal {
  825. // d.d.errorf(`json: unquoteStr: invalid hex char in \u unicode sequence: %q`, c)
  826. r = unicode.ReplacementChar
  827. i += 4
  828. goto encode_rune
  829. }
  830. rr1 = rr1*16 + uint32(c)
  831. }
  832. r = utf16.DecodeRune(r, rune(rr1))
  833. i += 4
  834. } else {
  835. r = unicode.ReplacementChar
  836. goto encode_rune
  837. }
  838. }
  839. encode_rune:
  840. w2 := utf8.EncodeRune(d.bstr[:], r)
  841. v = append(v, d.bstr[:w2]...)
  842. default:
  843. d.d.errorf("json: unsupported escaped value: %c", c)
  844. }
  845. i++
  846. cursor = i
  847. }
  848. d.bs = v
  849. }
  850. func (d *jsonDecDriver) nakedNum(z *decNaked, bs []byte) (err error) {
  851. if d.h.PreferFloat || jsonIsFloatBytesB3(bs) { // bytes.IndexByte(bs, '.') != -1 ||...
  852. // } else if d.h.PreferFloat || bytes.ContainsAny(bs, ".eE") {
  853. z.v = valueTypeFloat
  854. z.f, err = strconv.ParseFloat(stringView(bs), 64)
  855. } else if d.h.SignedInteger || bs[0] == '-' {
  856. z.v = valueTypeInt
  857. z.i, err = strconv.ParseInt(stringView(bs), 10, 64)
  858. } else {
  859. z.v = valueTypeUint
  860. z.u, err = strconv.ParseUint(stringView(bs), 10, 64)
  861. }
  862. if err != nil && z.v != valueTypeFloat {
  863. if v, ok := err.(*strconv.NumError); ok && (v.Err == strconv.ErrRange || v.Err == strconv.ErrSyntax) {
  864. z.v = valueTypeFloat
  865. z.f, err = strconv.ParseFloat(stringView(bs), 64)
  866. }
  867. }
  868. return
  869. }
  870. func (d *jsonDecDriver) bsToString() string {
  871. // if x := d.s.sc; x != nil && x.so && x.st == '}' { // map key
  872. if jsonAlwaysReturnInternString || d.c == containerMapKey {
  873. return d.d.string(d.bs)
  874. }
  875. return string(d.bs)
  876. }
  877. func (d *jsonDecDriver) DecodeNaked() {
  878. z := d.d.n
  879. // var decodeFurther bool
  880. if d.tok == 0 {
  881. d.tok = d.r.skip(&jsonCharWhitespaceSet)
  882. }
  883. switch d.tok {
  884. case 'n':
  885. d.readLit(3, jsonLitNull+1) // (n)ull
  886. z.v = valueTypeNil
  887. case 'f':
  888. d.readLit(4, jsonLitFalse+1) // (f)alse
  889. z.v = valueTypeBool
  890. z.b = false
  891. case 't':
  892. d.readLit(3, jsonLitTrue+1) // (t)rue
  893. z.v = valueTypeBool
  894. z.b = true
  895. case '{':
  896. z.v = valueTypeMap // don't consume. kInterfaceNaked will call ReadMapStart
  897. case '[':
  898. z.v = valueTypeArray // don't consume. kInterfaceNaked will call ReadArrayStart
  899. case '"':
  900. // if a string, and MapKeyAsString, then try to decode it as a nil, bool or number first
  901. d.appendStringAsBytes()
  902. if len(d.bs) > 0 && d.c == containerMapKey && d.h.MapKeyAsString {
  903. switch stringView(d.bs) {
  904. case "null":
  905. z.v = valueTypeNil
  906. case "true":
  907. z.v = valueTypeBool
  908. z.b = true
  909. case "false":
  910. z.v = valueTypeBool
  911. z.b = false
  912. default:
  913. // check if a number: float, int or uint
  914. if err := d.nakedNum(z, d.bs); err != nil {
  915. z.v = valueTypeString
  916. z.s = d.bsToString()
  917. }
  918. }
  919. } else {
  920. z.v = valueTypeString
  921. z.s = d.bsToString()
  922. }
  923. default: // number
  924. bs := d.decNumBytes()
  925. if len(bs) == 0 {
  926. d.d.errorf("json: decode number from empty string")
  927. return
  928. }
  929. if err := d.nakedNum(z, bs); err != nil {
  930. d.d.errorf("json: decode number from %s: %v", bs, err)
  931. return
  932. }
  933. }
  934. // if decodeFurther {
  935. // d.s.sc.retryRead()
  936. // }
  937. return
  938. }
  939. //----------------------
  940. // JsonHandle is a handle for JSON encoding format.
  941. //
  942. // Json is comprehensively supported:
  943. // - decodes numbers into interface{} as int, uint or float64
  944. // - configurable way to encode/decode []byte .
  945. // by default, encodes and decodes []byte using base64 Std Encoding
  946. // - UTF-8 support for encoding and decoding
  947. //
  948. // It has better performance than the json library in the standard library,
  949. // by leveraging the performance improvements of the codec library and
  950. // minimizing allocations.
  951. //
  952. // In addition, it doesn't read more bytes than necessary during a decode, which allows
  953. // reading multiple values from a stream containing json and non-json content.
  954. // For example, a user can read a json value, then a cbor value, then a msgpack value,
  955. // all from the same stream in sequence.
  956. //
  957. // Note that, when decoding quoted strings, invalid UTF-8 or invalid UTF-16 surrogate pairs
  958. // are not treated as an error.
  959. // Instead, they are replaced by the Unicode replacement character U+FFFD.
  960. type JsonHandle struct {
  961. textEncodingType
  962. BasicHandle
  963. // RawBytesExt, if configured, is used to encode and decode raw bytes in a custom way.
  964. // If not configured, raw bytes are encoded to/from base64 text.
  965. RawBytesExt InterfaceExt
  966. // Indent indicates how a value is encoded.
  967. // - If positive, indent by that number of spaces.
  968. // - If negative, indent by that number of tabs.
  969. Indent int8
  970. // IntegerAsString controls how integers (signed and unsigned) are encoded.
  971. //
  972. // Per the JSON Spec, JSON numbers are 64-bit floating point numbers.
  973. // Consequently, integers > 2^53 cannot be represented as a JSON number without losing precision.
  974. // This can be mitigated by configuring how to encode integers.
  975. //
  976. // IntegerAsString interpretes the following values:
  977. // - if 'L', then encode integers > 2^53 as a json string.
  978. // - if 'A', then encode all integers as a json string
  979. // containing the exact integer representation as a decimal.
  980. // - else encode all integers as a json number (default)
  981. IntegerAsString uint8
  982. // HTMLCharsAsIs controls how to encode some special characters to html: < > &
  983. //
  984. // By default, we encode them as \uXXX
  985. // to prevent security holes when served from some browsers.
  986. HTMLCharsAsIs bool
  987. // PreferFloat says that we will default to decoding a number as a float.
  988. // If not set, we will examine the characters of the number and decode as an
  989. // integer type if it doesn't have any of the characters [.eE].
  990. PreferFloat bool
  991. // TermWhitespace says that we add a whitespace character
  992. // at the end of an encoding.
  993. //
  994. // The whitespace is important, especially if using numbers in a context
  995. // where multiple items are written to a stream.
  996. TermWhitespace bool
  997. // MapKeyAsString says to encode all map keys as strings.
  998. //
  999. // Use this to enforce strict json output.
  1000. // The only caveat is that nil value is ALWAYS written as null (never as "null")
  1001. MapKeyAsString bool
  1002. }
  1003. func (h *JsonHandle) hasElemSeparators() bool { return true }
  1004. // SetInterfaceExt sets an extension
  1005. func (h *JsonHandle) SetInterfaceExt(rt reflect.Type, tag uint64, ext InterfaceExt) (err error) {
  1006. return h.SetExt(rt, tag, &setExtWrapper{i: ext})
  1007. }
  1008. func (h *JsonHandle) newEncDriver(e *Encoder) encDriver {
  1009. hd := jsonEncDriver{e: e, h: h}
  1010. hd.bs = hd.b[:0]
  1011. hd.reset()
  1012. return &hd
  1013. }
  1014. func (h *JsonHandle) newDecDriver(d *Decoder) decDriver {
  1015. // d := jsonDecDriver{r: r.(*bytesDecReader), h: h}
  1016. hd := jsonDecDriver{d: d, h: h}
  1017. hd.bs = hd.b[:0]
  1018. hd.reset()
  1019. return &hd
  1020. }
  1021. func (e *jsonEncDriver) reset() {
  1022. e.w = e.e.w
  1023. e.se.i = e.h.RawBytesExt
  1024. if e.bs != nil {
  1025. e.bs = e.bs[:0]
  1026. }
  1027. e.d, e.dt, e.dl, e.ds = false, false, 0, ""
  1028. e.c = 0
  1029. if e.h.Indent > 0 {
  1030. e.d = true
  1031. e.ds = jsonSpaces[:e.h.Indent]
  1032. } else if e.h.Indent < 0 {
  1033. e.d = true
  1034. e.dt = true
  1035. e.ds = jsonTabs[:-(e.h.Indent)]
  1036. }
  1037. }
  1038. func (d *jsonDecDriver) reset() {
  1039. d.r = d.d.r
  1040. d.se.i = d.h.RawBytesExt
  1041. if d.bs != nil {
  1042. d.bs = d.bs[:0]
  1043. }
  1044. d.c, d.tok = 0, 0
  1045. // d.n.reset()
  1046. }
  1047. // func jsonIsFloatBytes(bs []byte) bool {
  1048. // for _, v := range bs {
  1049. // // if v == '.' || v == 'e' || v == 'E' {
  1050. // if jsonIsFloatSet.isset(v) {
  1051. // return true
  1052. // }
  1053. // }
  1054. // return false
  1055. // }
  1056. func jsonIsFloatBytesB2(bs []byte) bool {
  1057. return bytes.IndexByte(bs, '.') != -1 ||
  1058. bytes.IndexByte(bs, 'E') != -1
  1059. }
  1060. func jsonIsFloatBytesB3(bs []byte) bool {
  1061. return bytes.IndexByte(bs, '.') != -1 ||
  1062. bytes.IndexByte(bs, 'E') != -1 ||
  1063. bytes.IndexByte(bs, 'e') != -1
  1064. }
  1065. var _ decDriver = (*jsonDecDriver)(nil)
  1066. var _ encDriver = (*jsonEncDriver)(nil)