Consolidated ASP Classic MVC framework from best components
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

322 lignes
9.6KB

  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.
  15. '=======================================================================================================================
  16. Class JsonLite_Object
  17. Private m_keys(), m_vals(), m_count
  18. Private Sub Class_Initialize()
  19. m_count = 0
  20. ReDim m_keys(7)
  21. ReDim m_vals(7)
  22. End Sub
  23. Public Sub Add(k, v)
  24. Dim i
  25. For i = 0 To m_count - 1
  26. If LCase(m_keys(i)) = LCase(k) Then
  27. m_vals(i) = v
  28. Exit Sub
  29. End If
  30. Next
  31. If m_count > UBound(m_keys) Then
  32. ReDim Preserve m_keys(m_count * 2)
  33. ReDim Preserve m_vals(m_count * 2)
  34. End If
  35. m_keys(m_count) = k
  36. m_vals(m_count) = v
  37. m_count = m_count + 1
  38. End Sub
  39. Public Function Exists(k)
  40. Dim i
  41. Exists = False
  42. For i = 0 To m_count - 1
  43. If LCase(m_keys(i)) = LCase(k) Then
  44. Exists = True
  45. Exit Function
  46. End If
  47. Next
  48. End Function
  49. Public Function Item(k)
  50. Dim i
  51. Item = Null
  52. For i = 0 To m_count - 1
  53. If LCase(m_keys(i)) = LCase(k) Then
  54. Item = m_vals(i)
  55. Exit Function
  56. End If
  57. Next
  58. End Function
  59. End Class
  60. Class JsonLite_Parser
  61. Private m_s, m_pos, m_len, m_ok
  62. Public Function Run(text)
  63. Dim obj
  64. m_s = "" & text
  65. m_len = Len(m_s)
  66. m_pos = 1
  67. m_ok = True
  68. Set Run = Nothing
  69. SkipWs
  70. If m_pos > m_len Then Exit Function
  71. If Mid(m_s, m_pos, 1) <> "{" Then Exit Function
  72. Set obj = ReadObject()
  73. If Not m_ok Then Exit Function
  74. SkipWs
  75. If m_pos <= m_len Then Exit Function ' trailing garbage
  76. Set Run = obj
  77. End Function
  78. Private Sub SkipWs()
  79. Dim c
  80. Do While m_pos <= m_len
  81. c = Mid(m_s, m_pos, 1)
  82. If c = " " Or c = vbTab Or c = vbCr Or c = vbLf Then
  83. m_pos = m_pos + 1
  84. Else
  85. Exit Do
  86. End If
  87. Loop
  88. End Sub
  89. Private Function ReadObject()
  90. Dim obj, k, v, c
  91. Set obj = New JsonLite_Object
  92. Set ReadObject = obj
  93. m_pos = m_pos + 1 ' {
  94. SkipWs
  95. If m_pos <= m_len Then
  96. If Mid(m_s, m_pos, 1) = "}" Then
  97. m_pos = m_pos + 1
  98. Exit Function
  99. End If
  100. End If
  101. Do
  102. SkipWs
  103. If m_pos > m_len Then
  104. m_ok = False
  105. Exit Function
  106. End If
  107. If Mid(m_s, m_pos, 1) <> """" Then
  108. m_ok = False
  109. Exit Function
  110. End If
  111. k = ReadString()
  112. If Not m_ok Then Exit Function
  113. SkipWs
  114. If m_pos > m_len Then
  115. m_ok = False
  116. Exit Function
  117. End If
  118. If Mid(m_s, m_pos, 1) <> ":" Then
  119. m_ok = False
  120. Exit Function
  121. End If
  122. m_pos = m_pos + 1
  123. v = ReadValue()
  124. If Not m_ok Then Exit Function
  125. obj.Add k, v
  126. SkipWs
  127. If m_pos > m_len Then
  128. m_ok = False
  129. Exit Function
  130. End If
  131. c = Mid(m_s, m_pos, 1)
  132. m_pos = m_pos + 1
  133. If c = "}" Then Exit Function
  134. If c <> "," Then
  135. m_ok = False
  136. Exit Function
  137. End If
  138. Loop
  139. End Function
  140. ' Reads any value; returns String, Double, Boolean or Null (nested containers are consumed and returned as Null)
  141. Private Function ReadValue()
  142. Dim c, t, start, lit, dummy
  143. ReadValue = Null
  144. SkipWs
  145. If m_pos > m_len Then
  146. m_ok = False
  147. Exit Function
  148. End If
  149. c = Mid(m_s, m_pos, 1)
  150. If c = """" Then
  151. ReadValue = ReadString()
  152. ElseIf c = "{" Then
  153. Set dummy = ReadObject()
  154. ElseIf c = "[" Then
  155. SkipArray
  156. ElseIf c = "-" Or (c >= "0" And c <= "9") Then
  157. start = m_pos
  158. Do While m_pos <= m_len
  159. t = Mid(m_s, m_pos, 1)
  160. If (t >= "0" And t <= "9") Or t = "-" Or t = "+" Or t = "." Or t = "e" Or t = "E" Then
  161. m_pos = m_pos + 1
  162. Else
  163. Exit Do
  164. End If
  165. Loop
  166. lit = Mid(m_s, start, m_pos - start)
  167. ' CDbl uses the locale's decimal separator, JSON always uses "."
  168. lit = Replace(UCase(lit), ".", Mid(CStr(1.5), 2, 1))
  169. On Error Resume Next
  170. ReadValue = CDbl(lit)
  171. If Err.Number <> 0 Then
  172. Err.Clear
  173. m_ok = False
  174. End If
  175. On Error GoTo 0
  176. Else
  177. If LCase(Mid(m_s, m_pos, 4)) = "true" Then
  178. ReadValue = True
  179. m_pos = m_pos + 4
  180. ElseIf LCase(Mid(m_s, m_pos, 5)) = "false" Then
  181. ReadValue = False
  182. m_pos = m_pos + 5
  183. ElseIf LCase(Mid(m_s, m_pos, 4)) = "null" Then
  184. ReadValue = Null
  185. m_pos = m_pos + 4
  186. Else
  187. m_ok = False
  188. End If
  189. End If
  190. End Function
  191. Private Sub SkipArray()
  192. Dim c, v
  193. m_pos = m_pos + 1 ' [
  194. SkipWs
  195. If m_pos <= m_len Then
  196. If Mid(m_s, m_pos, 1) = "]" Then
  197. m_pos = m_pos + 1
  198. Exit Sub
  199. End If
  200. End If
  201. Do
  202. v = ReadValue()
  203. If Not m_ok Then Exit Sub
  204. SkipWs
  205. If m_pos > m_len Then
  206. m_ok = False
  207. Exit Sub
  208. End If
  209. c = Mid(m_s, m_pos, 1)
  210. m_pos = m_pos + 1
  211. If c = "]" Then Exit Sub
  212. If c <> "," Then
  213. m_ok = False
  214. Exit Sub
  215. End If
  216. Loop
  217. End Sub
  218. Private Function ReadString()
  219. Dim out, c, e, h
  220. out = ""
  221. m_pos = m_pos + 1 ' opening quote
  222. Do While m_pos <= m_len
  223. c = Mid(m_s, m_pos, 1)
  224. m_pos = m_pos + 1
  225. If c = """" Then
  226. ReadString = out
  227. Exit Function
  228. ElseIf c = "\" Then
  229. If m_pos > m_len Then Exit Do
  230. e = Mid(m_s, m_pos, 1)
  231. m_pos = m_pos + 1
  232. Select Case e
  233. Case """": out = out & """"
  234. Case "\": out = out & "\"
  235. Case "/": out = out & "/"
  236. Case "b": out = out & Chr(8)
  237. Case "f": out = out & Chr(12)
  238. Case "n": out = out & vbLf
  239. Case "r": out = out & vbCr
  240. Case "t": out = out & vbTab
  241. Case "u"
  242. h = Mid(m_s, m_pos, 4)
  243. If Len(h) < 4 Or Not IsHex(h) Then
  244. m_ok = False
  245. Exit Function
  246. End If
  247. out = out & ChrW(CLng("&H" & h))
  248. m_pos = m_pos + 4
  249. Case Else
  250. m_ok = False
  251. Exit Function
  252. End Select
  253. Else
  254. out = out & c
  255. End If
  256. Loop
  257. m_ok = False ' unterminated string
  258. End Function
  259. Private Function IsHex(h)
  260. Dim i, c
  261. IsHex = True
  262. For i = 1 To Len(h)
  263. c = LCase(Mid(h, i, 1))
  264. If Not ((c >= "0" And c <= "9") Or (c >= "a" And c <= "f")) Then
  265. IsHex = False
  266. Exit Function
  267. End If
  268. Next
  269. End Function
  270. End Class
  271. Function JsonLite_Parse(text)
  272. Dim p
  273. Set p = New JsonLite_Parser
  274. Set JsonLite_Parse = p.Run(text)
  275. End Function
  276. ' "text" with JSON escaping (non-ASCII is left as is; the response is UTF-8)
  277. Function JsonLite_Quote(s)
  278. Dim t, i, c, n, out
  279. t = "" & s
  280. out = ""
  281. For i = 1 To Len(t)
  282. c = Mid(t, i, 1)
  283. n = AscW(c)
  284. If c = """" Then
  285. out = out & "\"""
  286. ElseIf c = "\" Then
  287. out = out & "\\"
  288. ElseIf n = 10 Then
  289. out = out & "\n"
  290. ElseIf n = 13 Then
  291. out = out & "\r"
  292. ElseIf n = 9 Then
  293. out = out & "\t"
  294. ElseIf n < 32 And n >= 0 Then
  295. out = out & "\u" & Right("0000" & Hex(n), 4)
  296. Else
  297. out = out & c
  298. End If
  299. Next
  300. JsonLite_Quote = """" & out & """"
  301. End Function
  302. ' Locale-independent number literal (CStr uses the locale's decimal separator)
  303. Function JsonLite_Num(n)
  304. JsonLite_Num = Replace(CStr(CDbl(n)), ",", ".")
  305. End Function
  306. %>

Powered by TurnKey Linux.