Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
4,214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.17 KB | None | 0 0
  1. <%
  2. 'Februari 2014 - Version 1.17 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 "GET", 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 = CDbl(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. JSONoutput_level = 1
  164. wrap_dicttype = "[]"
  165. For Each aj_label In data
  166. If Not aj_IsInt(aj_label) Then wrap_dicttype = "{}"
  167. Next
  168. JSONoutput = Left(wrap_dicttype, 1) & Chr(13) & Chr(10) & GetDict(data) & Right(wrap_dicttype, 1)
  169. End Function
  170.  
  171. Private Function GetDict(objDict)
  172. dim aj_item, aj_keyvals, aj_label, aj_dicttype
  173. For Each aj_item In objDict
  174. Select Case TypeName(objDict.Item(aj_item))
  175. Case "Dictionary"
  176. GetDict = GetDict & Space(JSONoutput_level * 4)
  177.  
  178. aj_dicttype = "[]"
  179. For Each aj_label In objDict.Item(aj_item).Keys
  180. If Not aj_IsInt(aj_label) Then aj_dicttype = "{}"
  181. Next
  182. If aj_IsInt(aj_item) Then
  183. GetDict = GetDict & (Left(aj_dicttype,1) & Chr(13) & Chr(10))
  184. Else
  185. GetDict = GetDict & ("""" & aj_JSONEncode(aj_item) & """" & ": " & Left(aj_dicttype,1) & Chr(13) & Chr(10))
  186. End If
  187. JSONoutput_level = JSONoutput_level + 1
  188.  
  189. aj_keyvals = objDict.Keys
  190. 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))
  191. Case Else
  192. aj_keyvals = objDict.Keys
  193. 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))
  194. End Select
  195. Next
  196. End Function
  197.  
  198. Private Function aj_IsInt(val)
  199. aj_IsInt = (TypeName(val) = "Integer" Or TypeName(val) = "Long")
  200. End Function
  201.  
  202. Private Function GetSubDict(objSubDict)
  203. GetSubDict = GetDict(objSubDict)
  204. JSONoutput_level= JSONoutput_level -1
  205. End Function
  206.  
  207. Private Function WriteValue(ByVal val)
  208. Select Case TypeName(val)
  209. Case "Double", "Integer", "Long": WriteValue = val
  210. Case "Null" : WriteValue = "null"
  211. Case "Boolean" : WriteValue = aj_InlineIf(val, "true", "false")
  212. Case Else : WriteValue = """" & aj_JSONEncode(val) & """"
  213. End Select
  214. End Function
  215.  
  216. Private Function aj_JSONEncode(ByVal val)
  217. val = Replace(val, "\", "\\")
  218. val = Replace(val, """", "\""")
  219. 'val = Replace(val, "/", "\/")
  220. val = Replace(val, Chr(8), "\b")
  221. val = Replace(val, Chr(12), "\f")
  222. val = Replace(val, Chr(10), "\n")
  223. val = Replace(val, Chr(13), "\r")
  224. val = Replace(val, Chr(9), "\t")
  225. aj_JSONEncode = Trim(val)
  226. End Function
  227.  
  228. Private Function aj_JSONDecode(ByVal val)
  229. val = Replace(val, "\""", """")
  230. val = Replace(val, "\\", "\")
  231. val = Replace(val, "\/", "/")
  232. val = Replace(val, "\b", Chr(8))
  233. val = Replace(val, "\f", Chr(12))
  234. val = Replace(val, "\n", Chr(10))
  235. val = Replace(val, "\r", Chr(13))
  236. val = Replace(val, "\t", Chr(9))
  237. aj_JSONDecode = Trim(val)
  238. End Function
  239.  
  240. Private Function aj_InlineIf(condition, returntrue, returnfalse)
  241. If condition Then aj_InlineIf = returntrue Else aj_InlineIf = returnfalse
  242. End Function
  243.  
  244. Private Function aj_Strip(ByVal val, stripper)
  245. If Left(val, 1) = stripper Then val = Mid(val, 2)
  246. If Right(val, 1) = stripper Then val = Left(val, Len(val) - 1)
  247. aj_Strip = val
  248. End Function
  249.  
  250. Private Function aj_MultilineTrim(TextData)
  251. aj_MultilineTrim = aj_RegExp.Replace(TextData, "$1")
  252. End Function
  253.  
  254. private function aj_Trim(val)
  255. aj_Trim = Trim(val)
  256. Do While Left(aj_Trim, 1) = Chr(9) : aj_Trim = Mid(aj_Trim, 2) : Loop
  257. Do While Right(aj_Trim, 1) = Chr(9) : aj_Trim = Left(aj_Trim, Len(aj_Trim) - 1) : Loop
  258. aj_Trim = Trim(aj_Trim)
  259. end function
  260. End Class
  261. %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement