|
| 1 | +VERSION 1.0 CLASS |
| 2 | +BEGIN |
| 3 | + MultiUse = -1 'True |
| 4 | +END |
| 5 | +Attribute VB_Name = "GitHubTreeJsonConverter" |
| 6 | +Attribute VB_GlobalNameSpace = False |
| 7 | +Attribute VB_Creatable = False |
| 8 | +Attribute VB_PredeclaredId = False |
| 9 | +Attribute VB_Exposed = False |
| 10 | +Option Compare Database |
| 11 | +Option Explicit |
| 12 | + |
| 13 | +Const SubPropIndentLen As Long = 2 |
| 14 | + |
| 15 | +Public Function Json2TreeNode(ByVal JsonString As String) As JsonProperties |
| 16 | + |
| 17 | + JsonString = OptimizeJsonString(JsonString) |
| 18 | + |
| 19 | + If Left(JsonString, 1) <> "{" Then |
| 20 | + Err.Raise vbObjectError, "JsonConverter.Parse", "Missing JSON structure" |
| 21 | + End If |
| 22 | + |
| 23 | + If Right(JsonString, 1) <> "}" Then |
| 24 | + Err.Raise vbObjectError, "JsonConverter.Parse", "Missing JSON structure" |
| 25 | + End If |
| 26 | + |
| 27 | + Set Json2TreeNode = ParseProperties(JsonString) |
| 28 | + |
| 29 | +End Function |
| 30 | + |
| 31 | +'############################################# |
| 32 | +' Parse JSON |
| 33 | + |
| 34 | +Private Function OptimizeJsonString(ByVal JsonString As String) As String |
| 35 | + |
| 36 | + JsonString = Replace(JsonString, vbNewLine, vbNullString) |
| 37 | + JsonString = Replace(JsonString, vbTab, vbNullString) |
| 38 | + JsonString = Replace(JsonString, vbCr, vbNullString) |
| 39 | + JsonString = Replace(JsonString, vbLf, vbNullString) |
| 40 | + |
| 41 | + JsonString = Trim(JsonString) |
| 42 | + |
| 43 | + OptimizeJsonString = JsonString |
| 44 | + |
| 45 | +End Function |
| 46 | + |
| 47 | +Private Function ParseProperties(ByVal JsonString As String) As JsonProperties |
| 48 | + |
| 49 | + Dim Props As JsonProperties |
| 50 | + Set Props = New JsonProperties |
| 51 | + |
| 52 | + Dim PropStringArray() As String |
| 53 | + Dim i As Long |
| 54 | + Dim PropString As String |
| 55 | + |
| 56 | + JsonString = Trim(JsonString) |
| 57 | + If Left(JsonString, 1) = "{" And Right(JsonString, 1) = "}" Then ' if |
| 58 | + JsonString = Mid(JsonString, 2, Len(JsonString) - 2) |
| 59 | + Else |
| 60 | + Err.Raise vbObjectError, "JsonConverter.ParseProperties", "Missing JSON structure" |
| 61 | + End If |
| 62 | + |
| 63 | + |
| 64 | + PropStringArray = Split(SetSplitMarker(JsonString), ",|") |
| 65 | + |
| 66 | + For i = LBound(PropStringArray) To UBound(PropStringArray) |
| 67 | + PropString = Trim(PropStringArray(i)) |
| 68 | + If Len(PropString) > 0 Then |
| 69 | + Props.Append ConvertString2Property(PropString) |
| 70 | + End If |
| 71 | + Next |
| 72 | + |
| 73 | + Set ParseProperties = Props |
| 74 | + |
| 75 | +End Function |
| 76 | + |
| 77 | +Private Function ConvertString2Property(ByVal JsonString As String) As JsonProperty |
| 78 | +' "Name" : "abc" => Value |
| 79 | +' "Name" : 123 => Value |
| 80 | +' "Name" : { ... => Properties |
| 81 | + |
| 82 | + Dim Prop As JsonProperty |
| 83 | + |
| 84 | + Dim PropName As String |
| 85 | + Dim PropValueString As Variant |
| 86 | + |
| 87 | + Dim CutPos As Long |
| 88 | + |
| 89 | + Set Prop = New JsonProperty |
| 90 | + |
| 91 | + CutPos = InStr(1, JsonString, ":") |
| 92 | + |
| 93 | + PropName = Trim(Left(JsonString, CutPos - 1)) |
| 94 | + If Left(PropName, 1) = """" And Right(PropName, 1) = """" Then |
| 95 | + PropName = Mid(PropName, 2, Len(PropName) - 2) |
| 96 | + End If |
| 97 | + Prop.Name = PropName |
| 98 | + |
| 99 | + PropValueString = Trim(Mid(JsonString, CutPos + 1)) |
| 100 | + |
| 101 | + If Left(PropValueString, 1) = "{" Then ' new properties |
| 102 | + Set Prop.Properties = ParseProperties(PropValueString) |
| 103 | + ElseIf Left(PropValueString, 1) = "[" Then ' new properties |
| 104 | + Set Prop.ObjectValue = ConvertString2JsonItem(PropValueString) |
| 105 | + Else |
| 106 | + Prop.Value = ConvertString2JsonItem(PropValueString) |
| 107 | + End If |
| 108 | + |
| 109 | + Set ConvertString2Property = Prop |
| 110 | + |
| 111 | +End Function |
| 112 | + |
| 113 | +Private Function ConvertString2JsonItem(ByVal PropValueString As String) As Variant |
| 114 | + |
| 115 | + If Left(PropValueString, 1) = "{" Then ' new properties |
| 116 | + Set ConvertString2JsonItem = ParseProperties(PropValueString) |
| 117 | + ElseIf Left(PropValueString, 1) = "[" Then ' new nodes |
| 118 | + Set ConvertString2JsonItem = ParseJsonArray(PropValueString) |
| 119 | + ElseIf PropValueString = "null" Then |
| 120 | + ConvertString2JsonItem = Null |
| 121 | + ElseIf PropValueString = "true" Then |
| 122 | + ConvertString2JsonItem = True |
| 123 | + ElseIf PropValueString = "false" Then |
| 124 | + ConvertString2JsonItem = False |
| 125 | + ElseIf Left(PropValueString, 1) = """" Then 'string |
| 126 | + ConvertString2JsonItem = Mid(PropValueString, 2, Len(PropValueString) - 2) |
| 127 | + Else ' Variant |
| 128 | + ConvertString2JsonItem = CVar(PropValueString) |
| 129 | + End If |
| 130 | + |
| 131 | +End Function |
| 132 | + |
| 133 | +Private Function ParseJsonArray(ByVal JsonString As String) As Collection |
| 134 | + |
| 135 | + Dim Items As Collection |
| 136 | + Dim PropStringArray() As String |
| 137 | + Dim i As Long |
| 138 | + Dim Props As Variant |
| 139 | + |
| 140 | + If Left(JsonString, 1) = "[" And Right(JsonString, 1) = "]" Then |
| 141 | + JsonString = Trim(Mid(JsonString, 2, Len(JsonString) - 2)) |
| 142 | + End If |
| 143 | + |
| 144 | + PropStringArray = Split(SetSplitMarker(JsonString), ",|") |
| 145 | + |
| 146 | + Set Items = New Collection |
| 147 | + For i = LBound(PropStringArray) To UBound(PropStringArray) |
| 148 | + Items.Add ConvertString2JsonItem(Trim(PropStringArray(i))) |
| 149 | + Next |
| 150 | + |
| 151 | + Set ParseJsonArray = Items |
| 152 | + |
| 153 | +End Function |
| 154 | + |
| 155 | +Private Function ParseValueFromString(ByVal JsonString As String) As Variant |
| 156 | + |
| 157 | + JsonString = Trim(JsonString) |
| 158 | + |
| 159 | + If JsonString = "null" Then |
| 160 | + ParseValueFromString = Null |
| 161 | + ElseIf JsonString = "true" Then |
| 162 | + ParseValueFromString = True |
| 163 | + ElseIf JsonString = "false" Then |
| 164 | + ParseValueFromString = False |
| 165 | + ElseIf Left(JsonString, 1) = """" And Right(JsonString, 1) = """" Then 'string |
| 166 | + ParseValueFromString = Mid(JsonString, 2, Len(JsonString) - 2) |
| 167 | + Else |
| 168 | + ParseValueFromString = Val(JsonString) |
| 169 | + End If |
| 170 | + |
| 171 | +End Function |
| 172 | + |
| 173 | +Private Function SetSplitMarker(ByVal JsonString As String) As String |
| 174 | + |
| 175 | + Dim Pos As Long |
| 176 | + Dim LeftPart As String |
| 177 | + Dim CheckString As String |
| 178 | + |
| 179 | + JsonString = Replace(JsonString, ",", ", ") ' place to insert marker |
| 180 | + |
| 181 | + Pos = InStr(1, JsonString, ",") |
| 182 | + |
| 183 | + Do While Pos > 0 |
| 184 | + CheckString = Left(JsonString, Pos - 1) |
| 185 | + If Not PosIsInSubObject(CheckString) Then |
| 186 | + If Not PosIsInArray(CheckString) Then |
| 187 | + Mid(JsonString, Pos + 1, 1) = "|" |
| 188 | + End If |
| 189 | + End If |
| 190 | + Pos = InStr(Pos + 1, JsonString, ",") |
| 191 | + Loop |
| 192 | + |
| 193 | + SetSplitMarker = Replace(JsonString, ", ", ",") |
| 194 | + |
| 195 | +End Function |
| 196 | + |
| 197 | +Private Function PosIsInSubObject(ByVal StringToCheck As String) As Boolean |
| 198 | + |
| 199 | + PosIsInSubObject = PosIsInOpenStruct(StringToCheck, "{", "}") |
| 200 | + |
| 201 | +End Function |
| 202 | + |
| 203 | +Private Function PosIsInArray(ByVal StringToCheck As String) As Boolean |
| 204 | + |
| 205 | + PosIsInArray = PosIsInOpenStruct(StringToCheck, "[", "]") |
| 206 | + |
| 207 | +End Function |
| 208 | + |
| 209 | +Private Function PosIsInOpenStruct(ByVal StringToCheck As String, ByVal OpeningChar As String, ByVal ClosingChar As String) As Boolean |
| 210 | + |
| 211 | + Dim CntOpeningBrackets As Long |
| 212 | + Dim CntClosingBrackets As Long |
| 213 | + |
| 214 | + CntOpeningBrackets = Len(StringToCheck) - Len(Replace(StringToCheck, OpeningChar, vbNullString)) |
| 215 | + CntClosingBrackets = Len(StringToCheck) - Len(Replace(StringToCheck, ClosingChar, vbNullString)) |
| 216 | + |
| 217 | + If CntOpeningBrackets > CntClosingBrackets Then |
| 218 | + PosIsInOpenStruct = True |
| 219 | + Else |
| 220 | + PosIsInOpenStruct = False |
| 221 | + End If |
| 222 | + |
| 223 | +End Function |
0 commit comments