Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

430 linhas
13KB

  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. '
  14. ' Values: String, Double (numbers), Boolean, Null. Nested objects/arrays are parsed and stored as Null, but the raw
  15. ' text of top-level ones is kept: obj.HasRaw(k) / obj.Raw(k); JsonLite_ParseStringArray(raw) reads an array of strings.
  16. '=======================================================================================================================
  17. Class JsonLite_Object
  18. Private m_keys(), m_vals(), m_count
  19. Private m_rawKeys(), m_rawVals(), m_rawCount
  20. Private Sub Class_Initialize()
  21. m_count = 0
  22. ReDim m_keys(7)
  23. ReDim m_vals(7)
  24. m_rawCount = 0
  25. ReDim m_rawKeys(3)
  26. ReDim m_rawVals(3)
  27. End Sub
  28. ' Raw JSON text of a top-level nested object/array value (Item returns Null for those).
  29. Public Sub AddRaw(k, rawText)
  30. If m_rawCount > UBound(m_rawKeys) Then
  31. ReDim Preserve m_rawKeys(m_rawCount * 2)
  32. ReDim Preserve m_rawVals(m_rawCount * 2)
  33. End If
  34. m_rawKeys(m_rawCount) = k
  35. m_rawVals(m_rawCount) = rawText
  36. m_rawCount = m_rawCount + 1
  37. End Sub
  38. Public Function HasRaw(k)
  39. Dim i
  40. HasRaw = False
  41. For i = 0 To m_rawCount - 1
  42. If LCase(m_rawKeys(i)) = LCase(k) Then
  43. HasRaw = True
  44. Exit Function
  45. End If
  46. Next
  47. End Function
  48. Public Function Raw(k)
  49. Dim i
  50. Raw = Null
  51. For i = 0 To m_rawCount - 1
  52. If LCase(m_rawKeys(i)) = LCase(k) Then
  53. Raw = m_rawVals(i)
  54. Exit Function
  55. End If
  56. Next
  57. End Function
  58. Public Sub Add(k, v)
  59. Dim i
  60. For i = 0 To m_count - 1
  61. If LCase(m_keys(i)) = LCase(k) Then
  62. m_vals(i) = v
  63. Exit Sub
  64. End If
  65. Next
  66. If m_count > UBound(m_keys) Then
  67. ReDim Preserve m_keys(m_count * 2)
  68. ReDim Preserve m_vals(m_count * 2)
  69. End If
  70. m_keys(m_count) = k
  71. m_vals(m_count) = v
  72. m_count = m_count + 1
  73. End Sub
  74. Public Function Exists(k)
  75. Dim i
  76. Exists = False
  77. For i = 0 To m_count - 1
  78. If LCase(m_keys(i)) = LCase(k) Then
  79. Exists = True
  80. Exit Function
  81. End If
  82. Next
  83. End Function
  84. Public Function Item(k)
  85. Dim i
  86. Item = Null
  87. For i = 0 To m_count - 1
  88. If LCase(m_keys(i)) = LCase(k) Then
  89. Item = m_vals(i)
  90. Exit Function
  91. End If
  92. Next
  93. End Function
  94. End Class
  95. Class JsonLite_Parser
  96. Private m_s, m_pos, m_len, m_ok
  97. Public Function Run(text)
  98. Dim obj
  99. m_s = "" & text
  100. m_len = Len(m_s)
  101. m_pos = 1
  102. m_ok = True
  103. Set Run = Nothing
  104. SkipWs
  105. If m_pos > m_len Then Exit Function
  106. If Mid(m_s, m_pos, 1) <> "{" Then Exit Function
  107. Set obj = ReadObject()
  108. If Not m_ok Then Exit Function
  109. SkipWs
  110. If m_pos <= m_len Then Exit Function ' trailing garbage
  111. Set Run = obj
  112. End Function
  113. ' Parses text that must be a JSON array of strings. Returns an array of strings, or Null if it is anything else.
  114. Public Function RunStringArray(text)
  115. Dim items(), n, c, v
  116. m_s = "" & text
  117. m_len = Len(m_s)
  118. m_pos = 1
  119. m_ok = True
  120. RunStringArray = Null
  121. n = 0
  122. ReDim items(-1)
  123. SkipWs
  124. If m_pos > m_len Then Exit Function
  125. If Mid(m_s, m_pos, 1) <> "[" Then Exit Function
  126. m_pos = m_pos + 1
  127. SkipWs
  128. If m_pos <= m_len Then
  129. If Mid(m_s, m_pos, 1) = "]" Then
  130. m_pos = m_pos + 1
  131. SkipWs
  132. If m_pos <= m_len Then Exit Function
  133. RunStringArray = items
  134. Exit Function
  135. End If
  136. End If
  137. Do
  138. SkipWs
  139. If m_pos > m_len Then Exit Function
  140. If Mid(m_s, m_pos, 1) <> """" Then Exit Function
  141. v = ReadString()
  142. If Not m_ok Then Exit Function
  143. ReDim Preserve items(n)
  144. items(n) = v
  145. n = n + 1
  146. SkipWs
  147. If m_pos > m_len Then Exit Function
  148. c = Mid(m_s, m_pos, 1)
  149. m_pos = m_pos + 1
  150. If c = "]" Then Exit Do
  151. If c <> "," Then Exit Function
  152. Loop
  153. SkipWs
  154. If m_pos <= m_len Then Exit Function
  155. RunStringArray = items
  156. End Function
  157. Private Sub SkipWs()
  158. Dim c
  159. Do While m_pos <= m_len
  160. c = Mid(m_s, m_pos, 1)
  161. If c = " " Or c = vbTab Or c = vbCr Or c = vbLf Then
  162. m_pos = m_pos + 1
  163. Else
  164. Exit Do
  165. End If
  166. Loop
  167. End Sub
  168. Private Function ReadObject()
  169. Dim obj, k, v, c, rawStart, c0
  170. Set obj = New JsonLite_Object
  171. Set ReadObject = obj
  172. m_pos = m_pos + 1 ' {
  173. SkipWs
  174. If m_pos <= m_len Then
  175. If Mid(m_s, m_pos, 1) = "}" Then
  176. m_pos = m_pos + 1
  177. Exit Function
  178. End If
  179. End If
  180. Do
  181. SkipWs
  182. If m_pos > m_len Then
  183. m_ok = False
  184. Exit Function
  185. End If
  186. If Mid(m_s, m_pos, 1) <> """" Then
  187. m_ok = False
  188. Exit Function
  189. End If
  190. k = ReadString()
  191. If Not m_ok Then Exit Function
  192. SkipWs
  193. If m_pos > m_len Then
  194. m_ok = False
  195. Exit Function
  196. End If
  197. If Mid(m_s, m_pos, 1) <> ":" Then
  198. m_ok = False
  199. Exit Function
  200. End If
  201. m_pos = m_pos + 1
  202. SkipWs
  203. rawStart = m_pos
  204. c0 = Mid(m_s, m_pos, 1)
  205. v = ReadValue()
  206. If Not m_ok Then Exit Function
  207. obj.Add k, v
  208. If c0 = "{" Or c0 = "[" Then
  209. obj.AddRaw k, Mid(m_s, rawStart, m_pos - rawStart)
  210. End If
  211. SkipWs
  212. If m_pos > m_len Then
  213. m_ok = False
  214. Exit Function
  215. End If
  216. c = Mid(m_s, m_pos, 1)
  217. m_pos = m_pos + 1
  218. If c = "}" Then Exit Function
  219. If c <> "," Then
  220. m_ok = False
  221. Exit Function
  222. End If
  223. Loop
  224. End Function
  225. ' Reads any value; returns String, Double, Boolean or Null (nested containers are consumed and returned as Null)
  226. Private Function ReadValue()
  227. Dim c, t, start, lit, dummy
  228. ReadValue = Null
  229. SkipWs
  230. If m_pos > m_len Then
  231. m_ok = False
  232. Exit Function
  233. End If
  234. c = Mid(m_s, m_pos, 1)
  235. If c = """" Then
  236. ReadValue = ReadString()
  237. ElseIf c = "{" Then
  238. Set dummy = ReadObject()
  239. ElseIf c = "[" Then
  240. SkipArray
  241. ElseIf c = "-" Or (c >= "0" And c <= "9") Then
  242. start = m_pos
  243. Do While m_pos <= m_len
  244. t = Mid(m_s, m_pos, 1)
  245. If (t >= "0" And t <= "9") Or t = "-" Or t = "+" Or t = "." Or t = "e" Or t = "E" Then
  246. m_pos = m_pos + 1
  247. Else
  248. Exit Do
  249. End If
  250. Loop
  251. lit = Mid(m_s, start, m_pos - start)
  252. ' CDbl uses the locale's decimal separator, JSON always uses "."
  253. lit = Replace(UCase(lit), ".", Mid(CStr(1.5), 2, 1))
  254. On Error Resume Next
  255. ReadValue = CDbl(lit)
  256. If Err.Number <> 0 Then
  257. Err.Clear
  258. m_ok = False
  259. End If
  260. On Error GoTo 0
  261. Else
  262. If LCase(Mid(m_s, m_pos, 4)) = "true" Then
  263. ReadValue = True
  264. m_pos = m_pos + 4
  265. ElseIf LCase(Mid(m_s, m_pos, 5)) = "false" Then
  266. ReadValue = False
  267. m_pos = m_pos + 5
  268. ElseIf LCase(Mid(m_s, m_pos, 4)) = "null" Then
  269. ReadValue = Null
  270. m_pos = m_pos + 4
  271. Else
  272. m_ok = False
  273. End If
  274. End If
  275. End Function
  276. Private Sub SkipArray()
  277. Dim c, v
  278. m_pos = m_pos + 1 ' [
  279. SkipWs
  280. If m_pos <= m_len Then
  281. If Mid(m_s, m_pos, 1) = "]" Then
  282. m_pos = m_pos + 1
  283. Exit Sub
  284. End If
  285. End If
  286. Do
  287. v = ReadValue()
  288. If Not m_ok Then Exit Sub
  289. SkipWs
  290. If m_pos > m_len Then
  291. m_ok = False
  292. Exit Sub
  293. End If
  294. c = Mid(m_s, m_pos, 1)
  295. m_pos = m_pos + 1
  296. If c = "]" Then Exit Sub
  297. If c <> "," Then
  298. m_ok = False
  299. Exit Sub
  300. End If
  301. Loop
  302. End Sub
  303. Private Function ReadString()
  304. Dim out, c, e, h, cu, h2, lo
  305. out = ""
  306. m_pos = m_pos + 1 ' opening quote
  307. Do While m_pos <= m_len
  308. c = Mid(m_s, m_pos, 1)
  309. m_pos = m_pos + 1
  310. If c = """" Then
  311. ReadString = out
  312. Exit Function
  313. ElseIf c = "\" Then
  314. If m_pos > m_len Then Exit Do
  315. e = Mid(m_s, m_pos, 1)
  316. m_pos = m_pos + 1
  317. Select Case e
  318. Case """": out = out & """"
  319. Case "\": out = out & "\"
  320. Case "/": out = out & "/"
  321. Case "b": out = out & Chr(8)
  322. Case "f": out = out & Chr(12)
  323. Case "n": out = out & vbLf
  324. Case "r": out = out & vbCr
  325. Case "t": out = out & vbTab
  326. Case "u"
  327. h = Mid(m_s, m_pos, 4)
  328. If Len(h) < 4 Or Not IsHex(h) Then
  329. m_ok = False
  330. Exit Function
  331. End If
  332. cu = CLng("&H" & h)
  333. m_pos = m_pos + 4
  334. ' A UTF-16 surrogate pair (backslash-u D83D, backslash-u DE00) is one code point; AxonASP strings need it combined.
  335. If cu >= 55296 And cu <= 56319 And Mid(m_s, m_pos, 2) = "\u" Then
  336. h2 = Mid(m_s, m_pos + 2, 4)
  337. If Len(h2) = 4 And IsHex(h2) Then
  338. lo = CLng("&H" & h2)
  339. If lo >= 56320 And lo <= 57343 Then
  340. cu = 65536 + (cu - 55296) * 1024 + (lo - 56320)
  341. m_pos = m_pos + 6
  342. End If
  343. End If
  344. End If
  345. out = out & ChrW(cu)
  346. Case Else
  347. m_ok = False
  348. Exit Function
  349. End Select
  350. Else
  351. out = out & c
  352. End If
  353. Loop
  354. m_ok = False ' unterminated string
  355. End Function
  356. Private Function IsHex(h)
  357. Dim i, c
  358. IsHex = True
  359. For i = 1 To Len(h)
  360. c = LCase(Mid(h, i, 1))
  361. If Not ((c >= "0" And c <= "9") Or (c >= "a" And c <= "f")) Then
  362. IsHex = False
  363. Exit Function
  364. End If
  365. Next
  366. End Function
  367. End Class
  368. ' Array of strings from the raw text of a JSON array, or Null when it is not an array of strings
  369. Function JsonLite_ParseStringArray(text)
  370. Dim p
  371. Set p = New JsonLite_Parser
  372. JsonLite_ParseStringArray = p.RunStringArray(text)
  373. End Function
  374. Function JsonLite_Parse(text)
  375. Dim p
  376. Set p = New JsonLite_Parser
  377. Set JsonLite_Parse = p.Run(text)
  378. End Function
  379. ' "text" with JSON escaping (non-ASCII is left as is; the response is UTF-8)
  380. Function JsonLite_Quote(s)
  381. Dim t, i, c, n, out
  382. t = "" & s
  383. out = ""
  384. For i = 1 To Len(t)
  385. c = Mid(t, i, 1)
  386. n = AscW(c)
  387. If c = """" Then
  388. out = out & "\"""
  389. ElseIf c = "\" Then
  390. out = out & "\\"
  391. ElseIf n = 10 Then
  392. out = out & "\n"
  393. ElseIf n = 13 Then
  394. out = out & "\r"
  395. ElseIf n = 9 Then
  396. out = out & "\t"
  397. ElseIf n < 32 And n >= 0 Then
  398. out = out & "\u" & Right("0000" & Hex(n), 4)
  399. Else
  400. out = out & c
  401. End If
  402. Next
  403. JsonLite_Quote = """" & out & """"
  404. End Function
  405. ' Locale-independent number literal (CStr uses the locale's decimal separator)
  406. Function JsonLite_Num(n)
  407. JsonLite_Num = Replace(CStr(CDbl(n)), ",", ".")
  408. End Function
  409. %>

Powered by TurnKey Linux.