<% '======================================================================================================================= ' 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 ' JsonLite_SurrogateSeen ' True after JsonLite_Parse met a backslash-u escape for a UTF-16 surrogate ' ' Characters above U+FFFF: on the pinned AxonASP build ChrW(cp > 65535) raises "Invalid procedure call" and a ' surrogate pair is written out as CESU-8 (invalid UTF-8). Surrogate escapes therefore decode to U+FFFD and set ' JsonLite_SurrogateSeen; Api_ReadBody rejects such requests with 422 instead of storing damaged text. ' ' Values: String, Double (numbers), Boolean, Null. Nested objects/arrays are parsed and stored as Null, but the raw ' text of top-level ones is kept: obj.HasRaw(k) / obj.Raw(k); JsonLite_ParseStringArray(raw) reads an array of strings. '======================================================================================================================= Class JsonLite_Object Private m_keys(), m_vals(), m_count Private m_rawKeys(), m_rawVals(), m_rawCount Private Sub Class_Initialize() m_count = 0 ReDim m_keys(7) ReDim m_vals(7) m_rawCount = 0 ReDim m_rawKeys(3) ReDim m_rawVals(3) End Sub ' Raw JSON text of a top-level nested object/array value (Item returns Null for those). Public Sub AddRaw(k, rawText) If m_rawCount > UBound(m_rawKeys) Then ReDim Preserve m_rawKeys(m_rawCount * 2) ReDim Preserve m_rawVals(m_rawCount * 2) End If m_rawKeys(m_rawCount) = k m_rawVals(m_rawCount) = rawText m_rawCount = m_rawCount + 1 End Sub Public Function HasRaw(k) Dim i HasRaw = False For i = 0 To m_rawCount - 1 If LCase(m_rawKeys(i)) = LCase(k) Then HasRaw = True Exit Function End If Next End Function Public Function Raw(k) Dim i Raw = Null For i = 0 To m_rawCount - 1 If LCase(m_rawKeys(i)) = LCase(k) Then Raw = m_rawVals(i) Exit Function End If Next End Function 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, m_sur Public Property Get SurrogateSeen() SurrogateSeen = (m_sur = True) End Property Public Function Run(text) Dim obj m_sur = False 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 ' Parses text that must be a JSON array of strings. Returns an array of strings, or Null if it is anything else. Public Function RunStringArray(text) Dim items(), n, c, v m_s = "" & text m_len = Len(m_s) m_pos = 1 m_ok = True RunStringArray = Null n = 0 ReDim items(-1) SkipWs If m_pos > m_len Then Exit Function If Mid(m_s, m_pos, 1) <> "[" Then Exit Function 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 SkipWs If m_pos <= m_len Then Exit Function RunStringArray = items Exit Function End If End If Do SkipWs If m_pos > m_len Then Exit Function If Mid(m_s, m_pos, 1) <> """" Then Exit Function v = ReadString() If Not m_ok Then Exit Function ReDim Preserve items(n) items(n) = v n = n + 1 SkipWs If m_pos > m_len Then Exit Function c = Mid(m_s, m_pos, 1) m_pos = m_pos + 1 If c = "]" Then Exit Do If c <> "," Then Exit Function Loop SkipWs If m_pos <= m_len Then Exit Function RunStringArray = items 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, rawStart, c0 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 SkipWs rawStart = m_pos c0 = Mid(m_s, m_pos, 1) v = ReadValue() If Not m_ok Then Exit Function obj.Add k, v If c0 = "{" Or c0 = "[" Then obj.AddRaw k, Mid(m_s, rawStart, m_pos - rawStart) End If 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, cu, h2, lo 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 cu = CLng("&H" & h) ' "&H8000".."&HFFFF" are read as signed 16-bit (e.g. D83D -> -10179) If cu < 0 Then cu = cu + 65536 End If m_pos = m_pos + 4 ' A UTF-16 surrogate pair (backslash-u D83D, backslash-u DE00) is one code point above U+FFFF, ' which this runtime cannot hold (see header): consume the pair, flag it, emit U+FFFD. If cu >= 55296 And cu <= 56319 And Mid(m_s, m_pos, 2) = "\u" Then h2 = Mid(m_s, m_pos + 2, 4) If Len(h2) = 4 And IsHex(h2) Then lo = CLng("&H" & h2) If lo < 0 Then lo = lo + 65536 End If If lo >= 56320 And lo <= 57343 Then m_pos = m_pos + 6 End If End If End If If cu >= 55296 And cu <= 57343 Then m_sur = True cu = 65533 End If out = out & ChrW(cu) 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 ' Array of strings from the raw text of a JSON array, or Null when it is not an array of strings Function JsonLite_ParseStringArray(text) Dim p Set p = New JsonLite_Parser JsonLite_ParseStringArray = p.RunStringArray(text) End Function Dim JsonLite_SurrogateSeen JsonLite_SurrogateSeen = False Function JsonLite_Parse(text) Dim p Set p = New JsonLite_Parser Set JsonLite_Parse = p.Run(text) JsonLite_SurrogateSeen = p.SurrogateSeen 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 %>