json.go 29 KB

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