Rev 3959 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
'June 2013 - Version 1.1 by Gerrit van KuipersClass aspJSONPublic dataPrivate p_JSONstringPrivate p_datatypeprivate 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_newlabelPrivate Sub Class_Initialize()Set data = Collection()p_datatype = "{}"End SubPrivate Sub Class_Terminate()Set data = NothingEnd SubPublic Function loadJSON(strInput)if len(trim(strInput)) = 0 then Err.Raise 1, "loadJSON Error", "No data to load."p_JSONstring = CleanUpJSONstring(Trim(strInput))aj_lines = Split(p_JSONstring, Chr(13) & Chr(10))Dim level(99)aj_currentlevel = 1Set level(aj_currentlevel) = dataFor Each aj_line In aj_linesaj_currentkey = ""aj_currentvalue = ""If Instr(aj_line, ":") > 0 Thenaj_in_string = Falseaj_in_escape = FalseFor aj_i_tmp = 1 To Len(aj_line)If aj_in_escape Thenaj_in_escape = FalseElseSelect Case Mid(aj_line, aj_i_tmp, 1)Case """"aj_in_string = Not aj_in_stringCase ":"If Not aj_in_escape Thenaj_currentkey = Left(aj_line, aj_i_tmp - 1)aj_currentvalue = Mid(aj_line, aj_i_tmp + 1)Exit ForEnd IfCase "\"aj_in_escape = TrueEnd SelectEnd IfNextaj_currentkey = aj_Strip(aj_JSONDecode(aj_currentkey), """")If Not level(aj_currentlevel).exists(aj_currentkey) Then level(aj_currentlevel).Add aj_currentkey, ""End IfIf right(aj_line,1) = "{" Or right(aj_line,1) = "[" ThenIf Len(aj_currentkey) = 0 Then aj_currentkey = level(aj_currentlevel).CountSet level(aj_currentlevel).Item(aj_currentkey) = Collection()Set level(aj_currentlevel + 1) = level(aj_currentlevel).Item(aj_currentkey)aj_currentlevel = aj_currentlevel + 1aj_currentkey = ""ElseIf right(aj_line,1) = "}" Or right(aj_line,1) = "]" or right(aj_line,2) = "}," Or right(aj_line,2) = "]," Thenaj_currentlevel = aj_currentlevel - 1ElseIf Len(Trim(aj_line)) > 0 Thenif Len(aj_currentvalue) = 0 Then aj_currentvalue = getJSONValue(aj_line)aj_currentvalue = getJSONValue(aj_currentvalue)If Len(aj_currentkey) = 0 Then aj_currentkey = level(aj_currentlevel).Countlevel(aj_currentlevel).Item(aj_currentkey) = aj_currentvalueEnd IfNextEnd FunctionPublic Function Collection()set Collection = CreateObject("Scripting.Dictionary")End FunctionPublic Function AddToCollection(dictobj)if TypeName(dictobj) <> "Dictionary" then Err.Raise 1, "AddToCollection Error", "Not a collection."aj_newlabel = dictobj.Countdictobj.Add aj_newlabel, Collection()set AddToCollection = dictobj.item(aj_newlabel)end functionPrivate Function CleanUpJSONstring(aj_originalstring)aj_originalstring = Replace(aj_originalstring, Chr(13) & Chr(10), "")p_datatype = Left(aj_originalstring, 1) & Right(aj_originalstring, 1)aj_originalstring = Mid(aj_originalstring, 2, Len(aj_originalstring) - 2)aj_in_string = False : aj_in_escape = False : aj_s_tmp = ""For aj_i_tmp = 1 To Len(aj_originalstring)aj_char_tmp = Mid(aj_originalstring, aj_i_tmp, 1)If aj_in_escape Thenaj_in_escape = Falseaj_s_tmp = aj_s_tmp & aj_char_tmpElseSelect Case aj_char_tmpCase "\" : aj_in_escape = TrueCase """" : aj_s_tmp = aj_s_tmp & aj_char_tmp : aj_in_string = Not aj_in_stringCase "{", "["aj_s_tmp = aj_s_tmp & aj_char_tmp & aj_InlineIf(aj_in_string, "", Chr(13) & Chr(10))Case "}", "]"aj_s_tmp = aj_s_tmp & aj_InlineIf(aj_in_string, "", Chr(13) & Chr(10)) & aj_char_tmpCase "," : aj_s_tmp = aj_s_tmp & aj_char_tmp & aj_InlineIf(aj_in_string, "", Chr(13) & Chr(10))Case Else : aj_s_tmp = aj_s_tmp & aj_char_tmpEnd SelectEnd IfNextCleanUpJSONstring = ""aj_s_tmp = split(aj_s_tmp, Chr(13) & Chr(10))For Each aj_line_tmp In aj_s_tmpaj_line_tmp = replace(replace(aj_line_tmp, chr(10), ""), chr(13), "")CleanUpJSONstring = CleanUpJSONstring & Trim(aj_line_tmp) & Chr(13) & Chr(10)NextEnd FunctionPrivate Function getJSONValue(ByVal val)val = Trim(val)If Left(val,1) = ":" Then val = Mid(val, 2)If Right(val,1) = "," Then val = Left(val, Len(val) - 1)val = Trim(val)Select Case valCase "true" : getJSONValue = TrueCase "false" : getJSONValue = FalseCase "null" : getJSONValue = NullCase ElseIf (Instr(val, """") = 0) ThenIf IsNumeric(val) ThengetJSONValue = CDbl(val)ElsegetJSONValue = valEnd IfElseIf Left(val,1) = """" Then val = Mid(val, 2)If Right(val,1) = """" Then val = Left(val, Len(val) - 1)getJSONValue = aj_JSONDecode(Trim(val))End IfEnd SelectEnd FunctionPrivate JSONoutput_levelPublic Function JSONoutput()JSONoutput_level = 1JSONoutput = Left(p_datatype, 1) & Chr(13) & Chr(10) & GetDict(data) & Right(p_datatype, 1)End FunctionPrivate Function GetDict(objDict)dim aj_item, aj_keyvals, aj_label, aj_dicttypeFor Each aj_item In objDictSelect Case TypeName(objDict.Item(aj_item))Case "Dictionary"GetDict = GetDict & Space(JSONoutput_level * 4)aj_dicttype = "[]"For Each aj_label In objDict.Item(aj_item).KeysIf Not IsInt(aj_label) Then aj_dicttype = "{}"NextIf IsInt(aj_item) ThenGetDict = GetDict & Left(aj_dicttype,1) & Chr(13) & Chr(10)ElseGetDict = GetDict & """" & aj_JSONEncode(aj_item) & """" & ": " & Left(aj_dicttype,1) & Chr(13) & Chr(10)End IfJSONoutput_level = JSONoutput_level + 1aj_keyvals = objDict.KeysGetDict = 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)Case Elseaj_keyvals = objDict.KeysGetDict = 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)End SelectNextEnd FunctionPrivate Function IsInt(val)IsInt = (TypeName(val) = "Integer" Or TypeName(val) = "Long")End FunctionPrivate Function GetSubDict(objSubDict)GetSubDict = GetDict(objSubDict)JSONoutput_level= JSONoutput_level -1End FunctionPrivate Function WriteValue(ByVal val)Select Case TypeName(val)Case "Double", "Integer", "Long": WriteValue = valCase "Null" : WriteValue = "null"Case "Boolean" : WriteValue = aj_InlineIf(val, "true", "false")Case Else : WriteValue = """" & aj_JSONEncode(val) & """"End SelectEnd FunctionPrivate Function aj_JSONEncode(ByVal val)val = Replace(val, "\", "\\")val = Replace(val, """", "\""")'val = Replace(val, "/", "\/")val = Replace(val, Chr(8), "\b")val = Replace(val, Chr(12), "\f")val = Replace(val, Chr(10), "\n")val = Replace(val, Chr(13), "\r")val = Replace(val, Chr(9), "\t")aj_JSONEncode = Trim(val)End FunctionPrivate Function aj_JSONDecode(ByVal val)val = Replace(val, "\""", """")val = Replace(val, "\\", "\")val = Replace(val, "\/", "/")val = Replace(val, "\b", Chr(8))val = Replace(val, "\f", Chr(12))val = Replace(val, "\n", Chr(10))val = Replace(val, "\r", Chr(13))val = Replace(val, "\t", Chr(9))aj_JSONDecode = Trim(val)End FunctionPrivate Function aj_InlineIf(condition, returntrue, returnfalse)If condition Then aj_InlineIf = returntrue Else aj_InlineIf = returnfalseEnd FunctionPrivate Function aj_Strip(ByVal val, stripper)If Left(val, 1) = stripper Then val = Mid(val, 2)If Right(val, 1) = stripper Then val = Left(val, Len(val) - 1)aj_Strip = valEnd FunctionEnd Class