25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

517 satır
17KB

  1. <%
  2. '=======================================================================================================================
  3. ' JsonLite: small pure-VBScript JSON reader/encoder for request bodies.
  4. '
  5. ' Why not G3JSON? Under the AxonASP HTTP server, G3JSON.Parse results and G3DB interfered with each other: parsing before the
  6. ' first database open made the first INSERT silently do nothing, and opening the database first made Parse return empty
  7. ' dictionaries. Plain VBScript classes avoid native objects altogether (see AGENTS.md).
  8. '
  9. ' Set obj = JsonLite_Parse(text) ' Nothing when text is not a valid JSON object
  10. ' obj.Exists("Name"), obj.Item("Name")
  11. ' JsonLite_Quote(s) ' "text" with JSON escaping
  12. ' JsonLite_Num(n) ' locale-independent number literal
  13. ' JsonLite_SurrogateSeen ' True after JsonLite_Parse met a backslash-u escape for a UTF-16 surrogate
  14. '
  15. ' Speed: on AxonASP every builtin call costs time in proportion to the length of the string it is given (Len, Mid and
  16. ' InStr on a 200K string take 0.4-5 ms each), so character loops over a large body are quadratic and timed out. The text
  17. ' is therefore split into tokens with ONE call to the AxonASP RegExp engine (JsonLite_Tokens) and the parser walks the
  18. ' token array; string tokens are decoded on their own (one Split on the backslash when they contain escapes).
  19. '
  20. ' Characters above U+FFFF: on the pinned AxonASP build ChrW(cp > 65535) raises "Invalid procedure call" and a
  21. ' surrogate pair is written out as CESU-8 (invalid UTF-8). Surrogate escapes therefore decode to U+FFFD and set
  22. ' JsonLite_SurrogateSeen; Api_ReadBody rejects such requests with 422 instead of storing damaged text.
  23. '
  24. ' Values: String, Double (numbers), Boolean, Null. Nested objects/arrays are parsed and stored as Null, but the raw
  25. ' text of top-level ones is kept: obj.HasRaw(k) / obj.Raw(k) (tokens joined without whitespace);
  26. ' JsonLite_ParseStringArray(raw) reads an array of strings, JsonLite_SplitObjectArray(raw) an array of objects.
  27. '=======================================================================================================================
  28. Class JsonLite_Object
  29. Private m_keys(), m_vals(), m_count
  30. Private m_rawKeys(), m_rawVals(), m_rawCount
  31. Private Sub Class_Initialize()
  32. m_count = 0
  33. ReDim m_keys(7)
  34. ReDim m_vals(7)
  35. m_rawCount = 0
  36. ReDim m_rawKeys(3)
  37. ReDim m_rawVals(3)
  38. End Sub
  39. ' Raw JSON text of a top-level nested object/array value (Item returns Null for those).
  40. Public Sub AddRaw(k, rawText)
  41. If m_rawCount > UBound(m_rawKeys) Then
  42. ReDim Preserve m_rawKeys(m_rawCount * 2)
  43. ReDim Preserve m_rawVals(m_rawCount * 2)
  44. End If
  45. m_rawKeys(m_rawCount) = k
  46. m_rawVals(m_rawCount) = rawText
  47. m_rawCount = m_rawCount + 1
  48. End Sub
  49. Public Function HasRaw(k)
  50. Dim i
  51. HasRaw = False
  52. For i = 0 To m_rawCount - 1
  53. If LCase(m_rawKeys(i)) = LCase(k) Then
  54. HasRaw = True
  55. Exit Function
  56. End If
  57. Next
  58. End Function
  59. Public Function Raw(k)
  60. Dim i
  61. Raw = Null
  62. For i = 0 To m_rawCount - 1
  63. If LCase(m_rawKeys(i)) = LCase(k) Then
  64. Raw = m_rawVals(i)
  65. Exit Function
  66. End If
  67. Next
  68. End Function
  69. Public Sub Add(k, v)
  70. Dim i
  71. For i = 0 To m_count - 1
  72. If LCase(m_keys(i)) = LCase(k) Then
  73. m_vals(i) = v
  74. Exit Sub
  75. End If
  76. Next
  77. If m_count > UBound(m_keys) Then
  78. ReDim Preserve m_keys(m_count * 2)
  79. ReDim Preserve m_vals(m_count * 2)
  80. End If
  81. m_keys(m_count) = k
  82. m_vals(m_count) = v
  83. m_count = m_count + 1
  84. End Sub
  85. Public Function Exists(k)
  86. Dim i
  87. Exists = False
  88. For i = 0 To m_count - 1
  89. If LCase(m_keys(i)) = LCase(k) Then
  90. Exists = True
  91. Exit Function
  92. End If
  93. Next
  94. End Function
  95. Public Function Item(k)
  96. Dim i
  97. Item = Null
  98. For i = 0 To m_count - 1
  99. If LCase(m_keys(i)) = LCase(k) Then
  100. Item = m_vals(i)
  101. Exit Function
  102. End If
  103. Next
  104. End Function
  105. End Class
  106. ' Tokens of a JSON text, in order: strings (with their quotes and escapes), numbers, true/false/null, punctuation, and
  107. ' any other single non-whitespace character (which the parser then rejects). Whitespace between tokens is dropped.
  108. ' Returns Empty when there is no token. In the pattern the backslash is written \x5C: AxonASP's pattern translation
  109. ' rejected a literal backslash inside a character class.
  110. Function JsonLite_Tokens(text)
  111. Dim re, ms, m, arr, i
  112. JsonLite_Tokens = Empty
  113. Set re = Server.CreateObject("VBScript.RegExp")
  114. re.Global = True
  115. re.Pattern = """(?:[^""\x5C]|\x5C.)*""|-?[0-9]+(?:[.][0-9]+)?(?:[eE][+-]?[0-9]+)?|true|false|null|[{}:,]|\x5B|\x5D|[^ \t\r\n]"
  116. Set ms = re.Execute("" & text)
  117. If ms.Count = 0 Then
  118. Exit Function
  119. End If
  120. ReDim arr(ms.Count - 1)
  121. i = 0
  122. For Each m In ms
  123. arr(i) = m.Value
  124. i = i + 1
  125. Next
  126. JsonLite_Tokens = arr
  127. End Function
  128. ' Raw text of tokens a..b (joined without whitespace)
  129. Function JsonLite_JoinTokens(t, a, b)
  130. Dim arr, i
  131. If b < a Then
  132. JsonLite_JoinTokens = ""
  133. Exit Function
  134. End If
  135. ReDim arr(b - a)
  136. For i = a To b
  137. arr(i - a) = t(i)
  138. Next
  139. JsonLite_JoinTokens = Join(arr, "")
  140. End Function
  141. Class JsonLite_Parser
  142. Private m_t, m_n, m_i, m_ok, m_sur
  143. Public Property Get SurrogateSeen()
  144. SurrogateSeen = (m_sur = True)
  145. End Property
  146. Private Function Load(text)
  147. m_sur = False
  148. m_ok = True
  149. m_i = 0
  150. m_t = JsonLite_Tokens(text)
  151. If IsEmpty(m_t) Then
  152. m_n = 0
  153. Load = False
  154. Else
  155. m_n = UBound(m_t) + 1
  156. Load = True
  157. End If
  158. End Function
  159. Private Function Peek()
  160. If m_i < m_n Then
  161. Peek = m_t(m_i)
  162. Else
  163. Peek = ""
  164. End If
  165. End Function
  166. Public Function Run(text)
  167. Dim obj
  168. Set Run = Nothing
  169. If Not Load(text) Then Exit Function
  170. If m_t(0) <> "{" Then Exit Function
  171. Set obj = ReadObject()
  172. If Not m_ok Then Exit Function
  173. If m_i < m_n Then Exit Function ' trailing garbage
  174. Set Run = obj
  175. End Function
  176. ' Parses text that must be a JSON array of strings. Returns an array of strings, or Null if it is anything else.
  177. Public Function RunStringArray(text)
  178. Dim items(), n, tok
  179. RunStringArray = Null
  180. If Not Load(text) Then Exit Function
  181. If m_t(0) <> "[" Then Exit Function
  182. m_i = 1
  183. n = 0
  184. ReDim items(-1)
  185. If Peek() = "]" Then
  186. m_i = m_i + 1
  187. If m_i < m_n Then Exit Function
  188. RunStringArray = items
  189. Exit Function
  190. End If
  191. Do
  192. tok = Peek()
  193. If Left(tok, 1) <> """" Then Exit Function
  194. ReDim Preserve items(n)
  195. items(n) = DecodeString(tok)
  196. If Not m_ok Then Exit Function
  197. n = n + 1
  198. m_i = m_i + 1
  199. tok = Peek()
  200. m_i = m_i + 1
  201. If tok = "]" Then Exit Do
  202. If tok <> "," Then Exit Function
  203. Loop
  204. If m_i < m_n Then Exit Function
  205. RunStringArray = items
  206. End Function
  207. Private Function ReadObject()
  208. Dim obj, k, v, tok, rawStart, c0
  209. Set obj = New JsonLite_Object
  210. Set ReadObject = obj
  211. m_i = m_i + 1 ' {
  212. If Peek() = "}" Then
  213. m_i = m_i + 1
  214. Exit Function
  215. End If
  216. Do
  217. tok = Peek()
  218. If Left(tok, 1) <> """" Then
  219. m_ok = False
  220. Exit Function
  221. End If
  222. k = DecodeString(tok)
  223. If Not m_ok Then Exit Function
  224. m_i = m_i + 1
  225. If Peek() <> ":" Then
  226. m_ok = False
  227. Exit Function
  228. End If
  229. m_i = m_i + 1
  230. rawStart = m_i
  231. c0 = Peek()
  232. v = ReadValue()
  233. If Not m_ok Then Exit Function
  234. obj.Add k, v
  235. If c0 = "{" Or c0 = "[" Then
  236. obj.AddRaw k, JsonLite_JoinTokens(m_t, rawStart, m_i - 1)
  237. End If
  238. tok = Peek()
  239. m_i = m_i + 1
  240. If tok = "}" Then Exit Function
  241. If tok <> "," Then
  242. m_ok = False
  243. Exit Function
  244. End If
  245. Loop
  246. End Function
  247. ' Reads any value; returns String, Double, Boolean or Null (nested containers are consumed and returned as Null)
  248. Private Function ReadValue()
  249. Dim tok, c, dummy, lit
  250. ReadValue = Null
  251. tok = Peek()
  252. If Len(tok) = 0 Then
  253. m_ok = False
  254. Exit Function
  255. End If
  256. c = Left(tok, 1)
  257. If c = """" Then
  258. ReadValue = DecodeString(tok)
  259. m_i = m_i + 1
  260. ElseIf c = "{" Then
  261. Set dummy = ReadObject()
  262. ElseIf c = "[" Then
  263. SkipArray
  264. ElseIf tok = "true" Then
  265. ReadValue = True
  266. m_i = m_i + 1
  267. ElseIf tok = "false" Then
  268. ReadValue = False
  269. m_i = m_i + 1
  270. ElseIf tok = "null" Then
  271. m_i = m_i + 1
  272. ElseIf (c = "-" And Len(tok) > 1) Or (c >= "0" And c <= "9") Then
  273. ' CDbl uses the locale's decimal separator, JSON always uses "."
  274. lit = Replace(UCase(tok), ".", Mid(CStr(1.5), 2, 1))
  275. On Error Resume Next
  276. ReadValue = CDbl(lit)
  277. If Err.Number <> 0 Then
  278. Err.Clear
  279. m_ok = False
  280. End If
  281. On Error GoTo 0
  282. m_i = m_i + 1
  283. Else
  284. m_ok = False
  285. End If
  286. End Function
  287. Private Sub SkipArray()
  288. Dim tok, v
  289. m_i = m_i + 1 ' [
  290. If Peek() = "]" Then
  291. m_i = m_i + 1
  292. Exit Sub
  293. End If
  294. Do
  295. v = ReadValue()
  296. If Not m_ok Then Exit Sub
  297. tok = Peek()
  298. m_i = m_i + 1
  299. If tok = "]" Then Exit Sub
  300. If tok <> "," Then
  301. m_ok = False
  302. Exit Sub
  303. End If
  304. Loop
  305. End Sub
  306. ' Decodes one string token (with its quotes). Without escapes this is a single Mid. With escapes the content is split
  307. ' on the backslash: every part after the first starts with an escape character, except that "\\" gives an empty
  308. ' part (the escaped backslash), after which the next part is plain text.
  309. Private Function DecodeString(tok)
  310. Dim inner, parts, out(), i, p, e, h, cu, h2, lo, ub
  311. inner = Mid(tok, 2, Len(tok) - 2)
  312. If InStr(inner, "\") = 0 Then
  313. DecodeString = inner
  314. Exit Function
  315. End If
  316. DecodeString = ""
  317. parts = Split(inner, "\")
  318. ub = UBound(parts)
  319. ReDim out(ub)
  320. For i = 0 To ub
  321. out(i) = ""
  322. Next
  323. out(0) = parts(0)
  324. i = 1
  325. Do While i <= ub
  326. p = parts(i)
  327. If Len(p) = 0 Then
  328. out(i) = "\"
  329. If i + 1 > ub Then
  330. m_ok = False ' a lone trailing backslash
  331. Exit Function
  332. End If
  333. out(i + 1) = parts(i + 1)
  334. i = i + 2
  335. Else
  336. e = Left(p, 1)
  337. Select Case e
  338. Case """": out(i) = """" & Mid(p, 2)
  339. Case "/": out(i) = "/" & Mid(p, 2)
  340. Case "b": out(i) = Chr(8) & Mid(p, 2)
  341. Case "f": out(i) = Chr(12) & Mid(p, 2)
  342. Case "n": out(i) = vbLf & Mid(p, 2)
  343. Case "r": out(i) = vbCr & Mid(p, 2)
  344. Case "t": out(i) = vbTab & Mid(p, 2)
  345. Case "u"
  346. h = Mid(p, 2, 4)
  347. If Len(h) < 4 Or Not IsHex(h) Then
  348. m_ok = False
  349. Exit Function
  350. End If
  351. cu = CLng("&H" & h)
  352. ' "&H8000".."&HFFFF" are read as signed 16-bit (e.g. D83D -> -10179)
  353. If cu < 0 Then
  354. cu = cu + 65536
  355. End If
  356. ' A UTF-16 surrogate pair (backslash-u D83D, backslash-u DE00) is one code point above U+FFFF,
  357. ' which this runtime cannot hold (see header): consume the pair, flag it, emit U+FFFD.
  358. If cu >= 55296 And cu <= 56319 And Len(p) = 5 And i + 1 <= ub Then
  359. If Left(parts(i + 1), 1) = "u" Then
  360. h2 = Mid(parts(i + 1), 2, 4)
  361. If Len(h2) = 4 And IsHex(h2) Then
  362. lo = CLng("&H" & h2)
  363. If lo < 0 Then
  364. lo = lo + 65536
  365. End If
  366. If lo >= 56320 And lo <= 57343 Then
  367. i = i + 1
  368. p = "u" & h & Mid(parts(i), 6)
  369. End If
  370. End If
  371. End If
  372. End If
  373. If cu >= 55296 And cu <= 57343 Then
  374. m_sur = True
  375. cu = 65533
  376. End If
  377. out(i) = ChrW(cu) & Mid(p, 6)
  378. Case Else
  379. m_ok = False
  380. Exit Function
  381. End Select
  382. i = i + 1
  383. End If
  384. Loop
  385. DecodeString = Join(out, "")
  386. End Function
  387. Private Function IsHex(h)
  388. Dim i, c
  389. IsHex = True
  390. For i = 1 To Len(h)
  391. c = LCase(Mid(h, i, 1))
  392. If Not ((c >= "0" And c <= "9") Or (c >= "a" And c <= "f")) Then
  393. IsHex = False
  394. Exit Function
  395. End If
  396. Next
  397. End Function
  398. End Class
  399. ' Array of strings from the raw text of a JSON array, or Null when it is not an array of strings
  400. Function JsonLite_ParseStringArray(text)
  401. Dim p
  402. Set p = New JsonLite_Parser
  403. JsonLite_ParseStringArray = p.RunStringArray(text)
  404. End Function
  405. ' Raw texts of the objects in a JSON array of objects ("[{...},{...}]"), each parseable with JsonLite_Parse.
  406. ' Returns an empty array for "[]", Null when the text is not an array whose elements are all objects.
  407. ' The text has already been validated as JSON by the body parse, so only depth is tracked (strings are single tokens).
  408. Function JsonLite_SplitObjectArray(text)
  409. Dim t, n, i, tok, depth, start, items(), cnt
  410. JsonLite_SplitObjectArray = Null
  411. t = JsonLite_Tokens(text)
  412. If IsEmpty(t) Then
  413. Exit Function
  414. End If
  415. n = UBound(t) + 1
  416. If n < 2 Then
  417. Exit Function
  418. End If
  419. If t(0) <> "[" Or t(n - 1) <> "]" Then
  420. Exit Function
  421. End If
  422. cnt = 0
  423. ReDim items(7)
  424. depth = 0
  425. start = 0
  426. For i = 1 To n - 2
  427. tok = t(i)
  428. If tok = "{" Or tok = "[" Then
  429. If depth = 0 Then
  430. If tok = "[" Then
  431. Exit Function ' a nested array element
  432. End If
  433. start = i
  434. End If
  435. depth = depth + 1
  436. ElseIf tok = "}" Or tok = "]" Then
  437. depth = depth - 1
  438. If depth = 0 Then
  439. If cnt > UBound(items) Then
  440. ReDim Preserve items(cnt * 2)
  441. End If
  442. items(cnt) = JsonLite_JoinTokens(t, start, i)
  443. cnt = cnt + 1
  444. End If
  445. ElseIf depth = 0 Then
  446. If tok <> "," Then
  447. Exit Function ' a string/number/true/false/null element
  448. End If
  449. End If
  450. Next
  451. If cnt = 0 Then
  452. JsonLite_SplitObjectArray = Array()
  453. Else
  454. ReDim Preserve items(cnt - 1)
  455. JsonLite_SplitObjectArray = items
  456. End If
  457. End Function
  458. Dim JsonLite_SurrogateSeen
  459. JsonLite_SurrogateSeen = False
  460. Function JsonLite_Parse(text)
  461. Dim p
  462. Set p = New JsonLite_Parser
  463. Set JsonLite_Parse = p.Run(text)
  464. JsonLite_SurrogateSeen = p.SurrogateSeen
  465. End Function
  466. ' "text" with JSON escaping (non-ASCII is left as is; the response is UTF-8)
  467. ' Built from native Replace calls: a per-character loop with string concatenation was quadratic (seconds per 100 KB).
  468. Function JsonLite_Quote(s)
  469. Dim t, n
  470. t = Replace("" & s, "\", "\\")
  471. t = Replace(t, """", "\""")
  472. t = Replace(t, ChrW(10), "\n")
  473. t = Replace(t, ChrW(13), "\r")
  474. t = Replace(t, ChrW(9), "\t")
  475. For n = 0 To 31
  476. If n <> 9 Then
  477. If n <> 10 Then
  478. If n <> 13 Then
  479. t = Replace(t, ChrW(n), "\u" & Right("0000" & Hex(n), 4))
  480. End If
  481. End If
  482. End If
  483. Next
  484. JsonLite_Quote = """" & t & """"
  485. End Function
  486. ' Locale-independent number literal (CStr uses the locale's decimal separator)
  487. Function JsonLite_Num(n)
  488. JsonLite_Num = Replace(CStr(CDbl(n)), ",", ".")
  489. End Function
  490. %>

Powered by TurnKey Linux.