Subversion Repositories DevTools

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3892 dpurdie 1
<%
2
'June 2013 - Version 1.1 by Gerrit van Kuipers
3
Class aspJSON
4
	Public data
5
	Private p_JSONstring
6
	Private p_datatype
7
	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
8
 
9
	Private Sub Class_Initialize()
10
		Set data = Collection()
11
		p_datatype = "{}"
12
	End Sub
13
 
14
	Private Sub Class_Terminate()
15
		Set data = Nothing
16
	End Sub
17
 
18
	Public Function loadJSON(strInput)
19
		if len(trim(strInput)) = 0 then Err.Raise 1, "loadJSON Error", "No data to load."
20
		p_JSONstring = CleanUpJSONstring(Trim(strInput))
21
		aj_lines = Split(p_JSONstring, Chr(13) & Chr(10))
22
 
23
		Dim level(99)
24
		aj_currentlevel = 1
25
		Set level(aj_currentlevel) = data
26
		For Each aj_line In aj_lines
27
			aj_currentkey = ""
28
			aj_currentvalue = ""
29
			If Instr(aj_line, ":") > 0 Then
30
				aj_in_string = False
31
				aj_in_escape = False
32
				For aj_i_tmp = 1 To Len(aj_line)
33
					If aj_in_escape Then
34
						aj_in_escape = False
35
					Else
36
						Select Case Mid(aj_line, aj_i_tmp, 1)
37
							Case """"
38
								aj_in_string = Not aj_in_string
39
							Case ":"
40
								If Not aj_in_escape Then
41
									aj_currentkey = Left(aj_line, aj_i_tmp - 1)
42
									aj_currentvalue = Mid(aj_line, aj_i_tmp + 1)
43
									Exit For
44
								End If
45
							Case "\"
46
								aj_in_escape = True
47
						End Select
48
					End If
49
				Next
50
				aj_currentkey = aj_Strip(aj_JSONDecode(aj_currentkey), """")
51
				If Not level(aj_currentlevel).exists(aj_currentkey) Then level(aj_currentlevel).Add aj_currentkey, ""
52
			End If
53
			If right(aj_line,1) = "{" Or right(aj_line,1) = "[" Then
54
				If Len(aj_currentkey) = 0 Then aj_currentkey = level(aj_currentlevel).Count
55
				Set level(aj_currentlevel).Item(aj_currentkey) = Collection()
56
				Set level(aj_currentlevel + 1) = level(aj_currentlevel).Item(aj_currentkey)
57
				aj_currentlevel = aj_currentlevel + 1
58
				aj_currentkey = ""
59
			ElseIf right(aj_line,1) = "}" Or right(aj_line,1) = "]" or right(aj_line,2) = "}," Or right(aj_line,2) = "]," Then
60
				aj_currentlevel = aj_currentlevel - 1
61
			ElseIf Len(Trim(aj_line)) > 0 Then
62
				if Len(aj_currentvalue) = 0 Then aj_currentvalue = getJSONValue(aj_line)
63
				aj_currentvalue = getJSONValue(aj_currentvalue)
64
 
65
				If Len(aj_currentkey) = 0 Then aj_currentkey = level(aj_currentlevel).Count
66
				level(aj_currentlevel).Item(aj_currentkey) = aj_currentvalue
67
			End If
68
		Next
69
	End Function
70
 
71
	Public Function Collection()
72
		set Collection = Server.CreateObject("Scripting.Dictionary")
73
	End Function
74
 
75
	Public Function AddToCollection(dictobj)
76
		if TypeName(dictobj) <> "Dictionary" then Err.Raise 1, "AddToCollection Error", "Not a collection."
77
		aj_newlabel = dictobj.Count
78
		dictobj.Add aj_newlabel, Collection()
79
		set AddToCollection = dictobj.item(aj_newlabel)
80
	end function
81
 
82
	Private Function CleanUpJSONstring(aj_originalstring)
83
		aj_originalstring = Replace(aj_originalstring, Chr(13) & Chr(10), "")
84
		p_datatype = Left(aj_originalstring, 1) & Right(aj_originalstring, 1)
85
		aj_originalstring = Mid(aj_originalstring, 2, Len(aj_originalstring) - 2)
86
		aj_in_string = False : aj_in_escape = False : aj_s_tmp = ""
87
		For aj_i_tmp = 1 To Len(aj_originalstring)
88
			aj_char_tmp = Mid(aj_originalstring, aj_i_tmp, 1)
89
			If aj_in_escape Then
90
				aj_in_escape = False
91
				aj_s_tmp = aj_s_tmp & aj_char_tmp
92
			Else
93
				Select Case aj_char_tmp
94
					Case "\" : aj_in_escape = True
95
					Case """" : aj_s_tmp = aj_s_tmp & aj_char_tmp : aj_in_string = Not aj_in_string
96
					Case "{", "["
97
						aj_s_tmp = aj_s_tmp & aj_char_tmp & aj_InlineIf(aj_in_string, "", Chr(13) & Chr(10))
98
					Case "}", "]"
99
						aj_s_tmp = aj_s_tmp & aj_InlineIf(aj_in_string, "", Chr(13) & Chr(10)) & aj_char_tmp
100
					Case "," : aj_s_tmp = aj_s_tmp & aj_char_tmp & aj_InlineIf(aj_in_string, "", Chr(13) & Chr(10))
101
					Case Else : aj_s_tmp = aj_s_tmp & aj_char_tmp
102
				End Select
103
			End If
104
		Next
105
 
106
		CleanUpJSONstring = ""
107
		aj_s_tmp = split(aj_s_tmp, Chr(13) & Chr(10))
108
		For Each aj_line_tmp In aj_s_tmp
109
			aj_line_tmp = replace(replace(aj_line_tmp, chr(10), ""), chr(13), "")
110
			CleanUpJSONstring = CleanUpJSONstring & Trim(aj_line_tmp) & Chr(13) & Chr(10)
111
		Next
112
	End Function
113
 
114
	Private Function getJSONValue(ByVal val)
115
		val = Trim(val)
116
		If Left(val,1) = ":"  Then val = Mid(val, 2)
117
		If Right(val,1) = "," Then val = Left(val, Len(val) - 1)
118
		val = Trim(val)
119
 
120
		Select Case val
121
			Case "true"  : getJSONValue = True
122
			Case "false" : getJSONValue = False
123
			Case "null" : getJSONValue = Null
124
			Case Else
125
				If (Instr(val, """") = 0) Then
126
					If IsNumeric(val) Then
127
						getJSONValue = CDbl(val)
128
					Else
129
						getJSONValue = val
130
					End If
131
				Else
132
					If Left(val,1) = """" Then val = Mid(val, 2)
133
					If Right(val,1) = """" Then val = Left(val, Len(val) - 1)
134
					getJSONValue = aj_JSONDecode(Trim(val))
135
				End If
136
		End Select
137
	End Function
138
 
139
	Private JSONoutput_level
140
	Public Function JSONoutput()
141
		JSONoutput_level = 1
142
		JSONoutput = Left(p_datatype, 1) & Chr(13) & Chr(10) & GetDict(data) & Right(p_datatype, 1)
143
	End Function
144
 
145
	Private Function GetDict(objDict)
146
		dim aj_item, aj_keyvals, aj_label, aj_dicttype
147
		For Each aj_item In objDict
148
			Select Case TypeName(objDict.Item(aj_item))
149
				Case "Dictionary"
150
					GetDict = GetDict & Space(JSONoutput_level * 4)
151
 
152
					aj_dicttype = "[]"
153
					For Each aj_label In objDict.Item(aj_item).Keys
154
						 If Not IsInt(aj_label) Then aj_dicttype = "{}"
155
					Next
156
 
157
					If IsInt(aj_item) Then
158
						GetDict = GetDict & Left(aj_dicttype,1) & Chr(13) & Chr(10)
159
					Else
160
						GetDict = GetDict & """" & aj_JSONEncode(aj_item) & """" & ": " & Left(aj_dicttype,1) & Chr(13) & Chr(10)
161
					End If
162
					JSONoutput_level = JSONoutput_level + 1
163
 
164
					aj_keyvals = objDict.Keys
165
					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)
166
				Case Else
167
					aj_keyvals =  objDict.Keys
168
					GetDict = GetDict & Space(JSONoutput_level * 4) & aj_InlineIf(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)
169
			End Select
170
		Next
171
	End Function
172
 
173
	Private Function IsInt(val)
174
		IsInt = (TypeName(val) = "Integer" Or TypeName(val) = "Long")
175
	End Function
176
 
177
	Private Function GetSubDict(objSubDict)
178
		GetSubDict = GetDict(objSubDict)
179
		JSONoutput_level= JSONoutput_level -1
180
	End Function
181
 
182
	Private Function WriteValue(ByVal val)
183
		Select Case TypeName(val)
184
			Case "Double", "Integer", "Long": WriteValue = val
185
			Case "Null"						: WriteValue = "null"
186
			Case "Boolean"					: WriteValue = aj_InlineIf(val, "true", "false")
187
			Case Else						: WriteValue = """" & aj_JSONEncode(val) & """"
188
		End Select
189
	End Function
190
 
191
	Private Function aj_JSONEncode(ByVal val)
192
		val = Replace(val, "\", "\\")
193
		val = Replace(val, """", "\""")
194
		'val = Replace(val, "/", "\/")
195
		val = Replace(val, Chr(8), "\b")
196
		val = Replace(val, Chr(12), "\f")
197
		val = Replace(val, Chr(10), "\n")
198
		val = Replace(val, Chr(13), "\r")
199
		val = Replace(val, Chr(9), "\t")
200
		aj_JSONEncode = Trim(val)
201
	End Function
202
 
203
	Private Function aj_JSONDecode(ByVal val)
204
		val = Replace(val, "\""", """")
205
		val = Replace(val, "\\", "\")
206
		val = Replace(val, "\/", "/")
207
		val = Replace(val, "\b", Chr(8))
208
		val = Replace(val, "\f", Chr(12))
209
		val = Replace(val, "\n", Chr(10))
210
		val = Replace(val, "\r", Chr(13))
211
		val = Replace(val, "\t", Chr(9))
212
		aj_JSONDecode = Trim(val)
213
	End Function
214
 
215
	Private Function aj_InlineIf(condition, returntrue, returnfalse)
216
		If condition Then aj_InlineIf = returntrue Else aj_InlineIf = returnfalse
217
	End Function
218
 
219
	Private Function aj_Strip(ByVal val, stripper)
220
		If Left(val, 1) = stripper Then val = Mid(val, 2)
221
		If Right(val, 1) = stripper Then val = Left(val, Len(val) - 1)
222
		aj_Strip = val
223
	End Function
224
End Class
225
%>