-
Notifications
You must be signed in to change notification settings - Fork 0
/
asp3tojson_reflector.asp
369 lines (305 loc) · 11.7 KB
/
asp3tojson_reflector.asp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<%
''**************************************************************************************************************
'* RSoft Web Copyright (C) 2009
'* Under GPL License
'**************************************************************************************************************
'**************************************************************************************************************
'' @CLASSTITLE: JSON
'' @CREATOR: Paulo Rodrigues
'' @CREATEDON: 2009-04-16
'' @CDESCRIPTION: JSON Reflector Methods to ASP3
'' @OPTIONEXPLICIT: yes
'' @VERSION: 0.0.1.1
'Use as include for class files only
'-------JSON Alternative------------------
'Pseudo call for JSON class (avoid bug)
'-----------------------------------------
public function toJSON(val)
out = "null"
If isArray(val) then
out = generateArray(val)
ElseIf isObject(val) then
If typename(val) = "Recordset" then
out = generateRecordset(val)
ElseIf typename(val) = "Dictionary" then
out = readDictionary(val, true)
Else
out = readDictionary(val.reflect(), true)
End If
End If
toJSON = out
end function
'******************************************************************************************
'Escape characters
'******************************************************************************************
public function escape(val)
dim cDoubleQuote, cRevSolidus, cSolidus
cDoubleQuote = &h22
cRevSolidus = &h5C
cSolidus = &h2F
dim i, currentDigit
for i = 1 to (len(val))
currentDigit = mid(val, i, 1)
if asc(currentDigit) > &h00 and asc(currentDigit) < &h1F then
currentDigit = escapequence(currentDigit)
elseif asc(currentDigit) >= &hC280 and asc(currentDigit) <= &hC2BF then
currentDigit = "\u00" + right(padLeft(hex(asc(currentDigit) - &hC200), 2, 0), 2)
elseif asc(currentDigit) >= &hC380 and asc(currentDigit) <= &hC3BF then
currentDigit = "\u00" + right(padLeft(hex(asc(currentDigit) - &hC2C0), 2, 0), 2)
else
select case asc(currentDigit)
case cDoubleQuote: currentDigit = escapequence(currentDigit)
case cRevSolidus: currentDigit = escapequence(currentDigit)
case cSolidus: currentDigit = escapequence(currentDigit)
end select
end if
escape = escape & currentDigit
next
end function
'******************************************************************************************************************
'* generateArray
'******************************************************************************************************************
Private Function generateArray(val)
dim item, i
out = out & "["
i = 0
'the for each allows us to support also multi dimensional arrays
for each item in val
if i > 0 then out = out & ","
out = out & getValueEmptyNull(item)
i = i + 1
next
out = out & "]"
generateArray = out
End Function
'******************************************************************************************************************
'* readDictionary
'******************************************************************************************************************
Private Function readDictionary(avalue, isInner)
cont = 0
out = ""
If isInner Then out = out & "{"
for each c in avalue
If isInner Then out = out & ""
if isNull(avalue.Item(c)) then
out = out & """" & c &""" : null"
elseif isArray(avalue.Item(c)) then
val = avalue.Item(c)
out = out & """" & c &""":"& generateArray(val)
elseif isObject(avalue.Item(c)) then
set aa = avalue.Item(c)
if avalue.Item(c) is nothing then
out = out & """" & c &""" : null"
elseif typename(avalue.Item(c)) = "Recordset" then
out = out &"""" & c &""":"& generateRecordset(avalue.Item(c))
elseif typename(avalue.Item(c)) = "Dictionary" then
'Nested Array
out = out & """" & c &""":[" & readDictionary(aa, false) & "]"
else
set bb = avalue.Item(c).reflect()
if typename(bb) = "Dictionary" then
If isInner Then out = out &"""" & c &""":"
If isInner Then out = out &"["
out = out &"{"& readDictionary(bb, false) &"}"
If isInner Then out = out &"]"
end if
end if
else
'bool
dim varTyp
varTyp = varType(avalue.Item(c))
if varTyp = 11 then
if avalue.Item(c) then
out = out & """" & c &""":true"
else
out = out & """" & c &""":false"
end if
'int, long, byte
elseif varTyp = 2 or varTyp = 3 or varTyp = 17 or varTyp = 19 then
out = out & """" & c &""":"& cLng(avalue.Item(c))
'single, double, currency
elseif varTyp = 4 or varTyp = 5 or varTyp = 6 or varTyp = 14 then
out = out & """" & c &""":"& replace(cDbl(avalue.Item(c)), ",", ".")
else
out = out & """" & c &""":""" & escape(avalue.Item(c)) &""""
end if
end if
If isInner Then out = out & ""
If cont < avalue.Count - 1 Then out = out & ","
out = out & ""
cont = cont + 1
next
If isInner Then out = out & "}"
readDictionary = out
End Function
'******************************************************************************************************************
'* generateRecordset
'******************************************************************************************************************
Private Function generateRecordset(val)
dim i
out = ""
out = out & "["
while not val.eof
innerCall = innerCall + 1
out = out & "{"
for i = 0 to val.fields.count - 1
if i > 0 and i < val.fields.count then out = out & ","
out = out & """"& lCase(val.fields(i).name) &""":"& getValueEmptyNull(val.fields(i).value) &""
next
out = out & "}"
val.movenext()
if not val.eof then out = out & ","
innerCall = innerCall - 1
wend
out = out & "]"
generateRecordset = out
end Function
'******************************************************************************************
'* JsonEscapeSquence
'******************************************************************************************
private function escapequence(digit)
escapequence = "\u00" + right(padLeft(hex(asc(digit)), 2, 0), 2)
end function
'******************************************************************************************
'* getValueEmptyNull
'******************************************************************************************
Private function getValueEmptyNull(val)
value = """"""
If isEmpty(val) or isNull(val) or val = "" Then
value = "null"
else
'bool
varTyp = varType(val)
if varTyp = 11 then
if val then value = "true" else value = "false"
'int, long, byte
elseif varTyp = 2 or varTyp = 3 or varTyp = 17 or varTyp = 19 then
value = cLng(val)
'single, double, currency
elseif varTyp = 4 or varTyp = 5 or varTyp = 6 or varTyp = 14 then
value = replace(cDbl(val), ",", ".")
else
value = """" & escape(val) & """"
end if
end if
getValueEmptyNull = value
end function
'******************************************************************************************
'* padLeft
'******************************************************************************************
Private function padLeft(value, totalLength, paddingChar)
padLeft = right(clone(paddingChar, totalLength) & value, totalLength)
end function
'******************************************************************************************
'* clone
'******************************************************************************************
private function clone(byVal str, n)
dim i
for i = 1 to n : clone = clone & str : next
end function
'-------JSON Alternative------------------
'Pseudo call for JSON class (avoid bug)
'-------End ------------------
'******************************************************************************************
'* Prevent SQL Inject Atack
'******************************************************************************************
Public Function scanSQL(str)
lixo = Array("select" ,"drop" ,";" ,"–","--" ,"insert" ,"delete" ,"xp_", "'")
for i = lBound(lixo) to uBound(lixo)
str = replace(str, lixo(i), "")
next
scanSQL = str
end function
'******************************************************************************************
'******************************************************************************************
'* Write the Methods ASP to JavaScript
'******************************************************************************************
Public Function writeMethodsToJsonA(aType, path)
tmpType = aType
Set obj = eval("(new "& aType &")")
Set r = obj.reflectMethod()
tmpMethod = ""
tmpMethod = tmpMethod & chr(13) &("<script>")
a = 0
tmpMethod = tmpMethod & chr(13) & "var _" & tmpType & " = {" & chr(13)
for each c in r
arrParams = Split(r(c),",")
arrMethod = Split(c," ")
withReturn = false
aMethod = ""
If(Ubound(arrMethod) > 0) Then
tmpMethod = tmpMethod & ""& arrMethod(1) & " : function"
aMethod = arrMethod(1)
If(lcase(arrMethod(0)) = "function") Then
withReturn = true
End If
ElseIf(Ubound(arrMethod) = 0) Then
tmpMethod = tmpMethod & ""& arrMethod(0) & " : function"
aMethod = arrMethod(0)
End If
tmpMethod = tmpMethod & "("
tmpParam = ""
If(Ubound(arrParams) > -1) Then
tmpMethod = tmpMethod & "method,"
for n = 0 to Ubound(arrParams)
tmpMethod = tmpMethod & arrParams(n)
tmpParam = tmpParam & "_param= ""' + "& arrParams(n) &" + '"" "
If(n < Ubound(arrParams)) Then
tmpMethod = tmpMethod & ","
tmpParam = tmpParam & "&"
End If
next
ElseIf(Ubound(arrParams) = -1) Then
tmpMethod = tmpMethod & "method"
End If
tmpMethod = tmpMethod & "){" & chr(13)
tmpMethod = tmpMethod & " var ___ajaxurl = '"& path &"?m="& aMethod &"&o="& aType &"';" & chr(13)
tmpMethod = tmpMethod & " a = new objectAjax.Ajax(___ajaxurl,{update:"""", onComplete:" & chr(13)
tmpMethod = tmpMethod & " function(_text,_xml)" & chr(13)
tmpMethod = tmpMethod & " {" & chr(13)
If withReturn = true Then
tmpMethod = tmpMethod & " method(_text);"& chr(13)
Else
tmpMethod = tmpMethod & " method();" & chr(13)
End If
tmpMethod = tmpMethod & " }" & chr(13)
tmpMethod = tmpMethod & " });" & chr(13)
If tmpParam <> "" Then
tmpParam = "'"& tmpParam &"'"
End If
tmpMethod = tmpMethod & " a.post("& tmpParam &");" & chr(13)
tmpMethod = tmpMethod & "}"
If a < r.count - 1 Then
tmpMethod = tmpMethod & "," & chr(13)
Else
tmpMethod = tmpMethod & "" & chr(13)
End If
a = a + 1
next
tmpMethod = tmpMethod & "};" & chr(13)
tmpMethod = tmpMethod & ""
tmpMethod = tmpMethod &("</script>" & chr(13))
writeMethodsToJsonA = tmpMethod
End Function
'Execute
If request.querystring("m") <> "" Then
atmp = ""
aType_ = request.querystring("o")
functionName = request.querystring("m")
params_ = scanSQL(request.form("_param"))
Public Function writeMethodsToJsonB(aType,params)
if isEmpty(params) then
atmp = eval("(new "& aType &")."& functionName & "()")
else
atmp = eval("(new "& aType &")."& functionName & "(" & params & ")")
end if
response.write atmp
End Function
writeMethodsToJsonB aType_,params_
'Write
ElseIf request.querystring("m") = "" Then
Public Function writeMethodsToJson(aType, path)
response.write writeMethodsToJsonA(aType, path)
End Function
End If
%>