No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

521 líneas
17KB

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

Powered by TurnKey Linux.