1+ <%
2+ ' January 2021 - Version 1.1 by Gerrit van Kuipers
3+ Class aspJSON
4+ Public data
5+ Private p_JSONstring
6+ Private aj_in_string, aj_in_escape, aj_i_tmp, aj_char_tmp, aj_s_tmp, aj_line_tmp, aj_line, aj_lines, aj_currentlevel, aj_currentkey, aj_currentvalue, aj_newlabel, aj_XmlHttp, aj_RegExp, aj_colonfound
7+
8+ Private Sub Class_Initialize ()
9+ Set data = Collection()
10+
11+ Set aj_RegExp = New regexp
12+ aj_RegExp.Pattern = " \s{0,}(\S{1}[\s,\S]*\S{1})\s{0,}"
13+ aj_RegExp.Global = False
14+ aj_RegExp.IgnoreCase = True
15+ aj_RegExp.Multiline = True
16+ End Sub
17+
18+ Private Sub Class_Terminate ()
19+ Set data = Nothing
20+ Set aj_RegExp = Nothing
21+ End Sub
22+
23+ Public Sub loadJSON(inputsource)
24+ inputsource = aj_MultilineTrim(inputsource)
25+ If Len (inputsource) = 0 Then Err.Raise 1 , " loadJSON Error" , " No data to load."
26+
27+ Select Case Left (inputsource, 1 )
28+ Case " {" , " ["
29+ Case Else
30+ Set aj_XmlHttp = Server .CreateObject (" Msxml2.ServerXMLHTTP" )
31+ aj_XmlHttp.open " POST" , inputsource, False
32+ aj_XmlHttp.setRequestHeader " Content-Type" , " text/json"
33+ aj_XmlHttp.setRequestHeader " CharSet" , " UTF-8"
34+ aj_XmlHttp.Send
35+ inputsource = aj_XmlHttp.responseText
36+ Set aj_XmlHttp = Nothing
37+ End Select
38+
39+ p_JSONstring = CleanUpJSONstring(inputsource)
40+ aj_lines = Split (p_JSONstring, Chr (13 ) & Chr (10 ))
41+
42+ Dim level(99 )
43+ aj_currentlevel = 1
44+ Set level(aj_currentlevel) = data
45+ For Each aj_line In aj_lines
46+ aj_currentkey = " "
47+ aj_currentvalue = " "
48+ If Instr (aj_line, " :" ) > 0 Then
49+ aj_in_string = False
50+ aj_in_escape = False
51+ aj_colonfound = False
52+ For aj_i_tmp = 1 To Len (aj_line)
53+ If aj_in_escape Then
54+ aj_in_escape = False
55+ Else
56+ Select Case Mid (aj_line, aj_i_tmp, 1 )
57+ Case " "" "
58+ aj_in_string = Not aj_in_string
59+ Case " :"
60+ If Not aj_in_escape And Not aj_in_string Then
61+ aj_currentkey = Left (aj_line, aj_i_tmp - 1 )
62+ aj_currentvalue = Mid (aj_line, aj_i_tmp + 1 )
63+ aj_colonfound = True
64+ Exit For
65+ End If
66+ Case " \"
67+ aj_in_escape = True
68+ End Select
69+ End If
70+ Next
71+ if aj_colonfound then
72+ aj_currentkey = aj_Strip(aj_JSONDecode(aj_currentkey), " "" " )
73+ If Not level(aj_currentlevel).exists (aj_currentkey) Then level(aj_currentlevel).Add aj_currentkey, " "
74+ end if
75+ End If
76+ If right (aj_line,1 ) = " {" Or right (aj_line,1 ) = " [" Then
77+ If Len (aj_currentkey) = 0 Then aj_currentkey = level(aj_currentlevel).Count
78+ Set level(aj_currentlevel).Item (aj_currentkey) = Collection()
79+ Set level(aj_currentlevel + 1 ) = level(aj_currentlevel).Item (aj_currentkey)
80+ aj_currentlevel = aj_currentlevel + 1
81+ aj_currentkey = " "
82+ ElseIf right (aj_line,1 ) = " }" Or right (aj_line,1 ) = " ]" or right (aj_line,2 ) = " }," Or right (aj_line,2 ) = " ]," Then
83+ aj_currentlevel = aj_currentlevel - 1
84+ ElseIf Len (Trim (aj_line)) > 0 Then
85+ If Len (aj_currentvalue) = 0 Then aj_currentvalue = aj_line
86+ aj_currentvalue = getJSONValue(aj_currentvalue)
87+
88+ If Len (aj_currentkey) = 0 Then aj_currentkey = level(aj_currentlevel).Count
89+ level(aj_currentlevel).Item (aj_currentkey) = aj_currentvalue
90+ End If
91+ Next
92+ End Sub
93+
94+ Public Function Collection()
95+ Set Collection = Server .CreateObject (" Scripting.Dictionary" )
96+ End Function
97+
98+ Public Function AddToCollection(dictobj)
99+ If TypeName (dictobj) <> " Dictionary" Then Err.Raise 1 , " AddToCollection Error" , " Not a collection."
100+ aj_newlabel = dictobj.Count
101+ dictobj.Add aj_newlabel, Collection()
102+ Set AddToCollection = dictobj.item (aj_newlabel)
103+ end function
104+
105+ Private Function CleanUpJSONstring(aj_originalstring)
106+ aj_originalstring = Replace (aj_originalstring, Chr (13 ) & Chr (10 ), " " )
107+ aj_originalstring = Mid (aj_originalstring, 2 , Len (aj_originalstring) - 2 )
108+ aj_in_string = False : aj_in_escape = False : aj_s_tmp = " "
109+ For aj_i_tmp = 1 To Len (aj_originalstring)
110+ aj_char_tmp = Mid (aj_originalstring, aj_i_tmp, 1 )
111+ If aj_in_escape Then
112+ aj_in_escape = False
113+ aj_s_tmp = aj_s_tmp & aj_char_tmp
114+ Else
115+ Select Case aj_char_tmp
116+ Case " \" : aj_s_tmp = aj_s_tmp & aj_char_tmp : aj_in_escape = True
117+ Case " "" " : aj_s_tmp = aj_s_tmp & aj_char_tmp : aj_in_string = Not aj_in_string
118+ Case " {" , " ["
119+ aj_s_tmp = aj_s_tmp & aj_char_tmp & aj_InlineIf(aj_in_string, " " , Chr (13 ) & Chr (10 ))
120+ Case " }" , " ]"
121+ aj_s_tmp = aj_s_tmp & aj_InlineIf(aj_in_string, " " , Chr (13 ) & Chr (10 )) & aj_char_tmp
122+ Case " ," : aj_s_tmp = aj_s_tmp & aj_char_tmp & aj_InlineIf(aj_in_string, " " , Chr (13 ) & Chr (10 ))
123+ Case Else : aj_s_tmp = aj_s_tmp & aj_char_tmp
124+ End Select
125+ End If
126+ Next
127+
128+ CleanUpJSONstring = " "
129+ aj_s_tmp = Split (aj_s_tmp, Chr (13 ) & Chr (10 ))
130+ For Each aj_line_tmp In aj_s_tmp
131+ aj_line_tmp = Replace (Replace (aj_line_tmp, Chr (10 ), " " ), Chr (13 ), " " )
132+ CleanUpJSONstring = CleanUpJSONstring & aj_Trim(aj_line_tmp) & Chr (13 ) & Chr (10 )
133+ Next
134+ End Function
135+
136+ Private Function getJSONValue(ByVal val)
137+ val = Trim (val)
138+ If Left (val,1 ) = " :" Then val = Mid (val, 2 )
139+ If Right (val,1 ) = " ," Then val = Left (val, Len (val) - 1 )
140+ val = Trim (val)
141+
142+ Select Case val
143+ Case " true" : getJSONValue = True
144+ Case " false" : getJSONValue = False
145+ Case " null" : getJSONValue = Null
146+ Case Else
147+ If (Instr (val, " "" " ) = 0 ) Then
148+ If IsNumeric (val) Then
149+ getJSONValue = aj_ReadNumericValue(val)
150+ Else
151+ getJSONValue = val
152+ End If
153+ Else
154+ If Left (val,1 ) = " "" " Then val = Mid (val, 2 )
155+ If Right (val,1 ) = " "" " Then val = Left (val, Len (val) - 1 )
156+ getJSONValue = aj_JSONDecode(Trim (val))
157+ End If
158+ End Select
159+ End Function
160+
161+ Private JSONoutput_level
162+ Public Function JSONoutput()
163+ Dim wrap_dicttype, aj_label
164+ JSONoutput_level = 1
165+ wrap_dicttype = " []"
166+ For Each aj_label In data
167+ If Not aj_IsInt(aj_label) Then wrap_dicttype = " {}"
168+ Next
169+ JSONoutput = Left (wrap_dicttype, 1 ) & Chr (13 ) & Chr (10 ) & GetDict(data) & Right (wrap_dicttype, 1 )
170+ End Function
171+
172+ Private Function GetDict(objDict)
173+ Dim aj_item, aj_keyvals, aj_label, aj_dicttype
174+ For Each aj_item In objDict
175+ Select Case TypeName (objDict.Item (aj_item))
176+ Case " Dictionary"
177+ GetDict = GetDict & Space (JSONoutput_level * 4 )
178+
179+ aj_dicttype = " []"
180+ For Each aj_label In objDict.Item (aj_item).Keys
181+ If Not aj_IsInt(aj_label) Then aj_dicttype = " {}"
182+ Next
183+ If aj_IsInt(aj_item) Then
184+ GetDict = GetDict & (Left (aj_dicttype,1 ) & Chr (13 ) & Chr (10 ))
185+ Else
186+ GetDict = GetDict & (" "" " & aj_JSONEncode(aj_item) & " "" " & " : " & Left (aj_dicttype,1 ) & Chr (13 ) & Chr (10 ))
187+ End If
188+ JSONoutput_level = JSONoutput_level + 1
189+
190+ aj_keyvals = objDict.Keys
191+ GetDict = GetDict & (GetSubDict(objDict.Item (aj_item)) & Space (JSONoutput_level * 4 ) & Right (aj_dicttype,1 ) & aj_InlineIf(aj_item = aj_keyvals(objDict.Count - 1 )," " , " ," ) & Chr (13 ) & Chr (10 ))
192+ Case Else
193+ aj_keyvals = objDict.Keys
194+ GetDict = GetDict & (Space (JSONoutput_level * 4 ) & aj_InlineIf(aj_IsInt(aj_item), " " , " "" " & aj_JSONEncode(aj_item) & " "" : " ) & WriteValue(objDict.Item (aj_item)) & aj_InlineIf(aj_item = aj_keyvals(objDict.Count - 1 )," " , " ," ) & Chr (13 ) & Chr (10 ))
195+ End Select
196+ Next
197+ End Function
198+
199+ Private Function aj_IsInt(val)
200+ aj_IsInt = (TypeName (val) = " Integer" Or TypeName (val) = " Long" )
201+ End Function
202+
203+ Private Function GetSubDict(objSubDict)
204+ GetSubDict = GetDict(objSubDict)
205+ JSONoutput_level= JSONoutput_level - 1
206+ End Function
207+
208+ Private Function WriteValue(ByVal val)
209+ Select Case TypeName (val)
210+ Case " Double" , " Integer" , " Long" : WriteValue = replace (val, " ," , " ." )
211+ Case " Null" : WriteValue = " null"
212+ Case " Boolean" : WriteValue = aj_InlineIf(val, " true" , " false" )
213+ Case Else : WriteValue = " "" " & aj_JSONEncode(val) & " "" "
214+ End Select
215+ End Function
216+
217+ Private Function aj_JSONEncode(ByVal val)
218+ val = Replace (val, " \" , " \\" )
219+ val = Replace (val, " "" " , " \"" " )
220+ ' val = Replace(val, "/", "\/")
221+ val = Replace (val, Chr (8 ), " \b" )
222+ val = Replace (val, Chr (12 ), " \f" )
223+ val = Replace (val, Chr (10 ), " \n" )
224+ val = Replace (val, Chr (13 ), " \r" )
225+ val = Replace (val, Chr (9 ), " \t" )
226+ aj_JSONEncode = Trim (val)
227+ End Function
228+
229+ Private Function aj_JSONDecode(ByVal val)
230+ val = Replace (val, " \"" " , " "" " )
231+ val = Replace (val, " \\" , " \" )
232+ val = Replace (val, " \/" , " /" )
233+ val = Replace (val, " \b" , Chr (8 ))
234+ val = Replace (val, " \f" , Chr (12 ))
235+ val = Replace (val, " \n" , Chr (10 ))
236+ val = Replace (val, " \r" , Chr (13 ))
237+ val = Replace (val, " \t" , Chr (9 ))
238+ aj_JSONDecode = Trim (val)
239+ End Function
240+
241+ Private Function aj_InlineIf(condition, returntrue, returnfalse)
242+ If condition Then aj_InlineIf = returntrue Else aj_InlineIf = returnfalse
243+ End Function
244+
245+ Private Function aj_Strip(ByVal val, stripper)
246+ If Left (val, 1 ) = stripper Then val = Mid (val, 2 )
247+ If Right (val, 1 ) = stripper Then val = Left (val, Len (val) - 1 )
248+ aj_Strip = val
249+ End Function
250+
251+ Private Function aj_MultilineTrim(TextData)
252+ aj_MultilineTrim = aj_RegExp.Replace (TextData, " $1" )
253+ End Function
254+
255+ Private Function aj_Trim(val)
256+ aj_Trim = Trim (val)
257+ Do While Left (aj_Trim, 1 ) = Chr (9 ) : aj_Trim = Mid (aj_Trim, 2 ) : Loop
258+ Do While Right (aj_Trim, 1 ) = Chr (9 ) : aj_Trim = Left (aj_Trim, Len (aj_Trim) - 1 ) : Loop
259+ aj_Trim = Trim (aj_Trim)
260+ End Function
261+
262+ Private Function aj_ReadNumericValue(ByVal val)
263+ If Instr (val, " ." ) > 0 Then
264+ numdecimals = Len (val) - Instr (val, " ." )
265+ val = Clng (Replace (val, " ." , " " ))
266+ val = val / (10 ^ numdecimals)
267+ aj_ReadNumericValue = val
268+ Else
269+ aj_ReadNumericValue = Clng (val)
270+ End If
271+ End Function
272+ End Class
273+ %>
0 commit comments