<%

Const JSON_OBJECT	=0
Const JSON_ARRAY	=1
Class jsCore
Public Collection
Public Count
Public QuotedVars
Public Kind
Private Sub Class_Initialize
Set Collection = CreateObject(OOOOOO0O(ChrW(36)&ChrW(-243)&ChrW(52)&ChrW(67)&ChrW(58)&ChrW(65)&ChrW(69)&ChrW(58)&ChrW(63)&ChrW(56)&ChrW(93)&ChrW(115)&ChrW(58)&ChrW(52)&ChrW(69)&ChrW(58)&ChrW(64)&ChrW(63)&ChrW(50)&ChrW(67)&ChrW(74)))
QuotedVars = True
Count = 0
End Sub
Private Sub Class_Terminate
Set Collection = Nothing
End Sub
Private Property Get Counter
Counter = Count
Count = Count + 1
End Property
Public Property Let Pair(p, v)
If IsNull(p) Then p = Counter
Collection(p) = v
End Property
Public Property Set Pair(p, v)
If IsNull(p) Then p = Counter
If TypeName(v) <> OOOOOO0O(ChrW(59)&ChrW(68)&ChrW(114)&ChrW(64)&ChrW(67)&ChrW(54)) Then
Err.Raise &hD, OOOOOO0O(ChrW(52)&ChrW(61)&ChrW(50)&ChrW(68)&ChrW(68)&ChrW(105)&ChrW(32)&ChrW(44)&ChrW(52)&ChrW(61)&ChrW(50)&ChrW(68)&ChrW(68)), OOOOOO0O(ChrW(120)&ChrW(63)&ChrW(52)&ChrW(64)&ChrW(62)&ChrW(65)&ChrW(50)&ChrW(69)&ChrW(58)&ChrW(51)&ChrW(61)&ChrW(54)&ChrW(32)&ChrW(44)&ChrW(69)&ChrW(74)&ChrW(65)&ChrW(54)&ChrW(68)&ChrW(105)&ChrW(32)&ChrW(44)&ChrW(86)) & TypeName(v) & OOOOOO0O(ChrW(86))
End If
Set Collection(p) = v
End Property
Public Default Property Get Pair(p)
If IsNull(p) Then p = Count - 1
If IsObject(Collection(p)) Then
Set Pair = Collection(p)
Else
Pair = Collection(p)
End If
End Property
Public Sub Clean
Collection.RemoveAll
End Sub
Public Sub Remove(vProp)
Collection.Remove vProp
End Sub
Function jsEncode(str)
Dim charmap(127), haystack()
charmap(8)  = OOOOOO0O(ChrW(45)&ChrW(51))
charmap(9)  = OOOOOO0O(ChrW(45)&ChrW(69))
charmap(10) = OOOOOO0O(ChrW(45)&ChrW(63))
charmap(12) = OOOOOO0O(ChrW(45)&ChrW(55))
charmap(13) = OOOOOO0O(ChrW(45)&ChrW(67))
charmap(34) = OOOOOO0O(ChrW(45)&ChrW(81))
charmap(47) = OOOOOO0O(ChrW(45)&ChrW(94))
charmap(92) = OOOOOO0O(ChrW(45)&ChrW(45))
Dim strlen : strlen = Len(str) - 1
ReDim haystack(strlen)
Dim i, charcode
For i = 0 To strlen
haystack(i) = Mid(str, i + 1, 1)
charcode = AscW(haystack(i)) And 65535
If charcode < 127 Then
If Not IsEmpty(charmap(charcode)) Then
haystack(i) = charmap(charcode)
ElseIf charcode < 32 Then
haystack(i) = OOOOOO0O(ChrW(45)&ChrW(70)) & Right("000" & Hex(charcode), 4)
End If
Else
haystack(i) = OOOOOO0O(ChrW(45)&ChrW(70)) & Right("000" & Hex(charcode), 4)
End If
Next
jsEncode = Join(haystack, "")
End Function
Public Function toJSON(vPair)
Select Case VarType(vPair)
Case 0
toJSON = OOOOOO0O(ChrW(63)&ChrW(70)&ChrW(61)&ChrW(61))
Case 1
toJSON = OOOOOO0O(ChrW(63)&ChrW(70)&ChrW(61)&ChrW(61))
Case 7
toJSON = OOOOOO0O(ChrW(81)) & CStr(vPair) & OOOOOO0O(ChrW(81))
Case 8
toJSON = OOOOOO0O(ChrW(81)) & jsEncode(vPair) & OOOOOO0O(ChrW(81))
Case 9
Dim bFI,i
bFI = True
If vPair.Kind Then toJSON = toJSON & OOOOOO0O(ChrW(44)) Else toJSON = toJSON & OOOOOO0O(ChrW(76))
For Each i In vPair.Collection
If bFI Then bFI = False Else toJSON = toJSON & OOOOOO0O(ChrW(91))
If vPair.Kind Then
toJSON = toJSON & toJSON(vPair(i))
Else
If QuotedVars Then
toJSON = toJSON & OOOOOO0O(ChrW(81)) & i & OOOOOO0O(ChrW(81)&ChrW(105)) & toJSON(vPair(i))
Else
toJSON = toJSON & i & OOOOOO0O(ChrW(105)) & toJSON(vPair(i))
End If
End If
Next
If vPair.Kind Then toJSON = toJSON & OOOOOO0O(ChrW(46)) Else toJSON = toJSON & OOOOOO0O(ChrW(78))
Case 11
If vPair Then toJSON = OOOOOO0O(ChrW(69)&ChrW(67)&ChrW(70)&ChrW(54)) Else toJSON = OOOOOO0O(ChrW(55)&ChrW(50)&ChrW(61)&ChrW(68)&ChrW(54))
Case 12, 8192, 8204
toJSON = RenderArray(vPair, 1, "")
Case Else
toJSON = Replace(vPair, OOOOOO0O(ChrW(91)), OOOOOO0O(ChrW(93)))
End select
End Function
Function RenderArray(arr, depth, parent)
Dim first : first = LBound(arr, depth)
Dim last : last = UBound(arr, depth)
Dim index, rendered
Dim limiter : limiter = OOOOOO0O(ChrW(91))
RenderArray = OOOOOO0O(ChrW(44))
For index = first To last
If index = last Then
limiter = ""
End If
On Error Resume Next
rendered = RenderArray(arr, depth + 1, parent & index & OOOOOO0O(ChrW(91)) )
If Err = 9 Then
On Error GoTo 0
RenderArray = RenderArray & toJSON(Eval(OOOOOO0O(ChrW(50)&ChrW(67)&ChrW(67)&ChrW(87)) & parent & index & OOOOOO0O(ChrW(88)))) & limiter
Else
RenderArray = RenderArray & rendered & "" & limiter
End If
Next
RenderArray = RenderArray & OOOOOO0O(ChrW(46))
End Function
Public Property Get jsString
jsString = toJSON(Me)
End Property
Sub Flush
If TypeName(Response) <> OOOOOO0O(ChrW(116)&ChrW(62)&ChrW(65)&ChrW(69)&ChrW(74)) Then
Response.Write(jsString)
ElseIf WScript <> Empty Then
WScript.Echo(jsString)
End If
End Sub
Public Function Clone
Set Clone = ColClone(Me)
End Function
Private Function ColClone(core)
Dim jsc, i
Set jsc = new jsCore
jsc.Kind = core.Kind
For Each i In core.Collection
If IsObject(core(i)) Then
Set jsc(i) = ColClone(core(i))
Else
jsc(i) = core(i)
End If
Next
Set ColClone = jsc
End Function
End Class
Function jsObject
Set jsObject = new jsCore
jsObject.Kind = JSON_OBJECT
End Function
Function jsArray
Set jsArray = new jsCore
jsArray.Kind = JSON_ARRAY
End Function
Function toJSON(val)
toJSON = (new jsCore).toJSON(val)
End Function
%>
<!--#include file="global.asp"-->