Json for asp
1
'
2
' VBS JSON 2.0.2
3
' Copyright (c) 2008 Turul Topuz
4
' Under the MIT (MIT-LICENSE.txt) license.
5
'
6
7
Const JSON_OBJECT = 0
8
Const JSON_ARRAY = 1
9
10
Class jsCore
11
Public Collection
12
Public Count
13
Public QuotedVars
14
Public Kind ' 0 = object, 1 = array
15
16
Private Sub Class_Initialize
17
Set Collection = CreateObject("Scripting.Dictionary")
18
QuotedVars = True
19
Count = 0
20
End Sub
21
22
Private Sub Class_Terminate
23
Set Collection = Nothing
24
End Sub
25
26
' counter
27
Private Property Get Counter
28
Counter = Count
29
Count = Count + 1
30
End Property
31
32
' - data maluplation
33
' -- pair
34
Public Property Let Pair(p, v)
35
If IsNull(p) Then p = Counter
36
Collection(p) = v
37
End Property
38
39
Public Property Set Pair(p, v)
40
If IsNull(p) Then p = Counter
41
If TypeName(v) <> "jsCore" Then
42
Err.Raise &hD, "class: class", "Tr uyumsuz: '" & TypeName(v) & "'"
43
End If
44
Set Collection(p) = v
45
End Property
46
47
Public Default Property Get Pair(p)
48
If IsNull(p) Then p = Count - 1
49
If IsObject(Collection(p)) Then
50
Set Pair = Collection(p)
51
Else
52
Pair = Collection(p)
53
End If
54
End Property
55
' -- pair
56
Public Sub Clean
57
Collection.RemoveAll
58
End Sub
59
60
Public Sub Remove(vProp)
61
Collection.Remove vProp
62
End Sub
63
' data maluplation
64
65
' encoding
66
Function jsEncode(str)
67
Dim i, j, aL1, aL2, c, p
68
69
aL1 = Array(&h22, &h5C, &h2F, &h08, &h0C, &h0A, &h0D, &h09)
70
aL2 = Array(&h22, &h5C, &h2F, &h62, &h66, &h6E, &h72, &h74)
71
For i = 1 To Len(str)
72
p = True
73
c = Mid(str, i, 1)
74
For j = 0 To 7
75
If c = Chr(aL1(j)) Then
76
jsEncode = jsEncode & "\" & Chr(aL2(j))
77
p = False
78
Exit For
79
End If
80
Next
81
82
If p Then
83
Dim a
84
a = AscW(c)
85
If a > 31 And a < 127 Then
86
jsEncode = jsEncode & c
87
ElseIf a > -1 Or a < 65535 Then
88
jsEncode = jsEncode & "\u" & String(4 - Len(Hex(a)), "0") & Hex(a)
89
End If
90
End If
91
Next
92
End Function
93
94
' converting
95
Public Function toJSON(vPair)
96
Select Case VarType(vPair)
97
Case 1 ' Null
98
toJSON = "null"
99
Case 7 ' Date
100
' yaz saati problemi var
101
' jsValue = "new Date(" & Round((vVal - #01/01/1970 02:00#) * 86400000) & ")"
102
toJSON = """" & CStr(vPair) & """"
103
Case 8 ' String
104
toJSON = """" & jsEncode(vPair) & """"
105
Case 9 ' Object
106
Dim bFI,i
107
bFI = True
108
If vPair.Kind Then toJSON = toJSON & "[" Else toJSON = toJSON & "{"
109
For Each i In vPair.Collection
110
If bFI Then bFI = False Else toJSON = toJSON & ","
111
112
If vPair.Kind Then
113
toJSON = toJSON & toJSON(vPair(i))
114
Else
115
If QuotedVars Then
116
toJSON = toJSON & """" & i & """:" & toJSON(vPair(i))
117
Else
118
toJSON = toJSON & i & ":" & toJSON(vPair(i))
119
End If
120
End If
121
Next
122
If vPair.Kind Then toJSON = toJSON & "]" Else toJSON = toJSON & "}"
123
Case 11
124
If vPair Then toJSON = "true" Else toJSON = "false"
125
Case 12, 8192, 8204
126
Dim sEB
127
toJSON = MultiArray(vPair, 1, "", sEB)
128
Case Else
129
toJSON = Replace(vPair, ",", ".")
130
End select
131
End Function
132
133
Function MultiArray(aBD, iBC, sPS, ByRef sPT) ' Array BoDy, Integer BaseCount, String PoSition
134
Dim iDU, iDL, i ' Integer DimensionUBound, Integer DimensionLBound
135
On Error Resume Next
136
iDL = LBound(aBD, iBC)
137
iDU = UBound(aBD, iBC)
138
139
Dim sPB1, sPB2 ' String PointBuffer1, String PointBuffer2
140
If Err = 9 Then
141
sPB1 = sPT & sPS
142
For i = 1 To Len(sPB1)
143
If i <> 1 Then sPB2 = sPB2 & ","
144
sPB2 = sPB2 & Mid(sPB1, i, 1)
145
Next
146
MultiArray = MultiArray & toJSON(Eval("aBD(" & sPB2 & ")"))
147
Else
148
sPT = sPT & sPS
149
MultiArray = MultiArray & "["
150
For i = iDL To iDU
151
MultiArray = MultiArray & MultiArray(aBD, iBC + 1, i, sPT)
152
If i < iDU Then MultiArray = MultiArray & ","
153
Next
154
MultiArray = MultiArray & "]"
155
sPT = Left(sPT, iBC - 2)
156
End If
157
End Function
158
159
Public Property Get jsString
160
jsString = toJSON(Me)
161
End Property
162
163
Sub Flush
164
If TypeName(Response) <> "Empty" Then
165
Response.Write(jsString)
166
ElseIf WScript <> Empty Then
167
WScript.Echo(jsString)
168
End If
169
End Sub
170
171
Public Function Clone
172
Set Clone = ColClone(Me)
173
End Function
174
175
Private Function ColClone(core)
176
Dim jsc, i
177
Set jsc = new jsCore
178
jsc.Kind = core.Kind
179
For Each i In core.Collection
180
If IsObject(core(i)) Then
181
Set jsc(i) = ColClone(core(i))
182
Else
183
jsc(i) = core(i)
184
End If
185
Next
186
Set ColClone = jsc
187
End Function
188
189
End Class
190
191
Function jsObject
192
Set jsObject = new jsCore
193
jsObject.Kind = JSON_OBJECT
194
End Function
195
196
Function jsArray
197
Set jsArray = new jsCore
198
jsArray.Kind = JSON_ARRAY
199
End Function
200
201
Function toJSON(val)
202
toJSON = (new jsCore).toJSON(val)
203
End Function

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

补充:
Function QueryToJSON(dbc, sql)
Dim rs, jsa
Set rs = dbc.Execute(sql)
Set jsa = jsArray()
While Not (rs.EOF Or rs.BOF)
Set jsa(Null) = jsObject()
For Each col In rs.Fields
jsa(Null)(col.Name) = col.Value
Next
rs.MoveNext
Wend
Set QueryToJSON = jsa
End Function