|
- <%
- '=======================================================================================================================
- ' JsonLite: tiny pure-VBScript JSON reader/encoder for flat request bodies.
- '
- ' Why not G3JSON? Under the AxonASP HTTP server, G3JSON.Parse results and G3DB interfered with each other: parsing before the
- ' first database open made the first INSERT silently do nothing, and opening the database first made Parse return empty
- ' dictionaries. Plain VBScript classes avoid native objects altogether (see AGENTS.md).
- '
- ' Set obj = JsonLite_Parse(text) ' Nothing when text is not a valid JSON object
- ' obj.Exists("Name"), obj.Item("Name")
- ' JsonLite_Quote(s) ' "text" with JSON escaping
- ' JsonLite_Num(n) ' locale-independent number literal
- '
- ' Values: String, Double (numbers), Boolean, Null. Nested objects/arrays are parsed and stored as Null.
- '=======================================================================================================================
- Class JsonLite_Object
- Private m_keys(), m_vals(), m_count
-
- Private Sub Class_Initialize()
- m_count = 0
- ReDim m_keys(7)
- ReDim m_vals(7)
- End Sub
-
- Public Sub Add(k, v)
- Dim i
- For i = 0 To m_count - 1
- If LCase(m_keys(i)) = LCase(k) Then
- m_vals(i) = v
- Exit Sub
- End If
- Next
- If m_count > UBound(m_keys) Then
- ReDim Preserve m_keys(m_count * 2)
- ReDim Preserve m_vals(m_count * 2)
- End If
- m_keys(m_count) = k
- m_vals(m_count) = v
- m_count = m_count + 1
- End Sub
-
- Public Function Exists(k)
- Dim i
- Exists = False
- For i = 0 To m_count - 1
- If LCase(m_keys(i)) = LCase(k) Then
- Exists = True
- Exit Function
- End If
- Next
- End Function
-
- Public Function Item(k)
- Dim i
- Item = Null
- For i = 0 To m_count - 1
- If LCase(m_keys(i)) = LCase(k) Then
- Item = m_vals(i)
- Exit Function
- End If
- Next
- End Function
- End Class
-
- Class JsonLite_Parser
- Private m_s, m_pos, m_len, m_ok
-
- Public Function Run(text)
- Dim obj
- m_s = "" & text
- m_len = Len(m_s)
- m_pos = 1
- m_ok = True
- Set Run = Nothing
- SkipWs
- If m_pos > m_len Then Exit Function
- If Mid(m_s, m_pos, 1) <> "{" Then Exit Function
- Set obj = ReadObject()
- If Not m_ok Then Exit Function
- SkipWs
- If m_pos <= m_len Then Exit Function ' trailing garbage
- Set Run = obj
- End Function
-
- Private Sub SkipWs()
- Dim c
- Do While m_pos <= m_len
- c = Mid(m_s, m_pos, 1)
- If c = " " Or c = vbTab Or c = vbCr Or c = vbLf Then
- m_pos = m_pos + 1
- Else
- Exit Do
- End If
- Loop
- End Sub
-
- Private Function ReadObject()
- Dim obj, k, v, c
- Set obj = New JsonLite_Object
- Set ReadObject = obj
- m_pos = m_pos + 1 ' {
- SkipWs
- If m_pos <= m_len Then
- If Mid(m_s, m_pos, 1) = "}" Then
- m_pos = m_pos + 1
- Exit Function
- End If
- End If
- Do
- SkipWs
- If m_pos > m_len Then
- m_ok = False
- Exit Function
- End If
- If Mid(m_s, m_pos, 1) <> """" Then
- m_ok = False
- Exit Function
- End If
- k = ReadString()
- If Not m_ok Then Exit Function
- SkipWs
- If m_pos > m_len Then
- m_ok = False
- Exit Function
- End If
- If Mid(m_s, m_pos, 1) <> ":" Then
- m_ok = False
- Exit Function
- End If
- m_pos = m_pos + 1
- v = ReadValue()
- If Not m_ok Then Exit Function
- obj.Add k, v
- SkipWs
- If m_pos > m_len Then
- m_ok = False
- Exit Function
- End If
- c = Mid(m_s, m_pos, 1)
- m_pos = m_pos + 1
- If c = "}" Then Exit Function
- If c <> "," Then
- m_ok = False
- Exit Function
- End If
- Loop
- End Function
-
- ' Reads any value; returns String, Double, Boolean or Null (nested containers are consumed and returned as Null)
- Private Function ReadValue()
- Dim c, t, start, lit, dummy
- ReadValue = Null
- SkipWs
- If m_pos > m_len Then
- m_ok = False
- Exit Function
- End If
- c = Mid(m_s, m_pos, 1)
- If c = """" Then
- ReadValue = ReadString()
- ElseIf c = "{" Then
- Set dummy = ReadObject()
- ElseIf c = "[" Then
- SkipArray
- ElseIf c = "-" Or (c >= "0" And c <= "9") Then
- start = m_pos
- Do While m_pos <= m_len
- t = Mid(m_s, m_pos, 1)
- If (t >= "0" And t <= "9") Or t = "-" Or t = "+" Or t = "." Or t = "e" Or t = "E" Then
- m_pos = m_pos + 1
- Else
- Exit Do
- End If
- Loop
- lit = Mid(m_s, start, m_pos - start)
- ' CDbl uses the locale's decimal separator, JSON always uses "."
- lit = Replace(UCase(lit), ".", Mid(CStr(1.5), 2, 1))
- On Error Resume Next
- ReadValue = CDbl(lit)
- If Err.Number <> 0 Then
- Err.Clear
- m_ok = False
- End If
- On Error GoTo 0
- Else
- If LCase(Mid(m_s, m_pos, 4)) = "true" Then
- ReadValue = True
- m_pos = m_pos + 4
- ElseIf LCase(Mid(m_s, m_pos, 5)) = "false" Then
- ReadValue = False
- m_pos = m_pos + 5
- ElseIf LCase(Mid(m_s, m_pos, 4)) = "null" Then
- ReadValue = Null
- m_pos = m_pos + 4
- Else
- m_ok = False
- End If
- End If
- End Function
-
- Private Sub SkipArray()
- Dim c, v
- m_pos = m_pos + 1 ' [
- SkipWs
- If m_pos <= m_len Then
- If Mid(m_s, m_pos, 1) = "]" Then
- m_pos = m_pos + 1
- Exit Sub
- End If
- End If
- Do
- v = ReadValue()
- If Not m_ok Then Exit Sub
- SkipWs
- If m_pos > m_len Then
- m_ok = False
- Exit Sub
- End If
- c = Mid(m_s, m_pos, 1)
- m_pos = m_pos + 1
- If c = "]" Then Exit Sub
- If c <> "," Then
- m_ok = False
- Exit Sub
- End If
- Loop
- End Sub
-
- Private Function ReadString()
- Dim out, c, e, h
- out = ""
- m_pos = m_pos + 1 ' opening quote
- Do While m_pos <= m_len
- c = Mid(m_s, m_pos, 1)
- m_pos = m_pos + 1
- If c = """" Then
- ReadString = out
- Exit Function
- ElseIf c = "\" Then
- If m_pos > m_len Then Exit Do
- e = Mid(m_s, m_pos, 1)
- m_pos = m_pos + 1
- Select Case e
- Case """": out = out & """"
- Case "\": out = out & "\"
- Case "/": out = out & "/"
- Case "b": out = out & Chr(8)
- Case "f": out = out & Chr(12)
- Case "n": out = out & vbLf
- Case "r": out = out & vbCr
- Case "t": out = out & vbTab
- Case "u"
- h = Mid(m_s, m_pos, 4)
- If Len(h) < 4 Or Not IsHex(h) Then
- m_ok = False
- Exit Function
- End If
- out = out & ChrW(CLng("&H" & h))
- m_pos = m_pos + 4
- Case Else
- m_ok = False
- Exit Function
- End Select
- Else
- out = out & c
- End If
- Loop
- m_ok = False ' unterminated string
- End Function
-
- Private Function IsHex(h)
- Dim i, c
- IsHex = True
- For i = 1 To Len(h)
- c = LCase(Mid(h, i, 1))
- If Not ((c >= "0" And c <= "9") Or (c >= "a" And c <= "f")) Then
- IsHex = False
- Exit Function
- End If
- Next
- End Function
- End Class
-
- Function JsonLite_Parse(text)
- Dim p
- Set p = New JsonLite_Parser
- Set JsonLite_Parse = p.Run(text)
- End Function
-
- ' "text" with JSON escaping (non-ASCII is left as is; the response is UTF-8)
- Function JsonLite_Quote(s)
- Dim t, i, c, n, out
- t = "" & s
- out = ""
- For i = 1 To Len(t)
- c = Mid(t, i, 1)
- n = AscW(c)
- If c = """" Then
- out = out & "\"""
- ElseIf c = "\" Then
- out = out & "\\"
- ElseIf n = 10 Then
- out = out & "\n"
- ElseIf n = 13 Then
- out = out & "\r"
- ElseIf n = 9 Then
- out = out & "\t"
- ElseIf n < 32 And n >= 0 Then
- out = out & "\u" & Right("0000" & Hex(n), 4)
- Else
- out = out & c
- End If
- Next
- JsonLite_Quote = """" & out & """"
- End Function
-
- ' Locale-independent number literal (CStr uses the locale's decimal separator)
- Function JsonLite_Num(n)
- JsonLite_Num = Replace(CStr(CDbl(n)), ",", ".")
- End Function
- %>
|