@@ -178,9 +178,10 @@ End Function
178178'
179179' @method ConvertToJson
180180' @param {Variant} json_DictionaryCollectionOrArray (Dictionary, Collection, or Array)
181+ ' @param {Integer|String} json_Whitespace "Pretty" print json with given number of spaces per indentation (Integer) or given string
181182' @return {String}
182183''
183- Public Function ConvertToJson (ByVal json_DictionaryCollectionOrArray As Variant ) As String
184+ Public Function ConvertToJson (ByVal json_DictionaryCollectionOrArray As Variant , Optional ByVal json_Whitespace As Variant , Optional ByVal json_CurrentIndentation As Long = 0 ) As String
184185 Dim json_buffer As String
185186 Dim json_BufferPosition As Long
186187 Dim json_BufferLength As Long
@@ -197,13 +198,17 @@ Public Function ConvertToJson(ByVal json_DictionaryCollectionOrArray As Variant)
197198 Dim json_DateStr As String
198199 Dim json_Converted As String
199200 Dim json_SkipItem As Boolean
201+ Dim json_PrettyPrint As Boolean
202+ Dim json_Indentation As String
203+ Dim json_InnerIndentation As String
200204
201205 json_LBound = -1
202206 json_UBound = -1
203207 json_IsFirstItem = True
204208 json_LBound2D = -1
205209 json_UBound2D = -1
206210 json_IsFirstItem2D = True
211+ json_PrettyPrint = Not IsMissing(json_Whitespace)
207212
208213 Select Case VBA.VarType(json_DictionaryCollectionOrArray)
209214 Case VBA.vbNull
@@ -227,6 +232,16 @@ Public Function ConvertToJson(ByVal json_DictionaryCollectionOrArray As Variant)
227232 ConvertToJson = "false"
228233 End If
229234 Case VBA.vbArray To VBA.vbArray + VBA.vbByte
235+ If json_PrettyPrint Then
236+ If VBA.VarType(json_Whitespace) = VBA.vbString Then
237+ json_Indentation = VBA.String $(json_CurrentIndentation + 1 , json_Whitespace)
238+ json_InnerIndentation = VBA.String $(json_CurrentIndentation + 2 , json_Whitespace)
239+ Else
240+ json_Indentation = VBA.Space$((json_CurrentIndentation + 1 ) * json_Whitespace)
241+ json_InnerIndentation = VBA.Space$((json_CurrentIndentation + 2 ) * json_Whitespace)
242+ End If
243+ End If
244+
230245 ' Array
231246 json_BufferAppend json_buffer, "[" , json_BufferPosition, json_BufferLength
232247
@@ -242,11 +257,16 @@ Public Function ConvertToJson(ByVal json_DictionaryCollectionOrArray As Variant)
242257 If json_IsFirstItem Then
243258 json_IsFirstItem = False
244259 Else
260+ ' Append comma to previous line
245261 json_BufferAppend json_buffer, "," , json_BufferPosition, json_BufferLength
246262 End If
247263
248264 If json_LBound2D >= 0 And json_UBound2D >= 0 Then
249- json_BufferAppend json_buffer, "[" , json_BufferPosition, json_BufferLength
265+ ' 2D Array
266+ If json_PrettyPrint Then
267+ json_BufferAppend json_buffer, vbNewLine, json_BufferPosition, json_BufferLength
268+ End If
269+ json_BufferAppend json_buffer, json_Indentation & "[" , json_BufferPosition, json_BufferLength
250270
251271 For json_Index2D = json_LBound2D To json_UBound2D
252272 If json_IsFirstItem2D Then
@@ -255,7 +275,7 @@ Public Function ConvertToJson(ByVal json_DictionaryCollectionOrArray As Variant)
255275 json_BufferAppend json_buffer, "," , json_BufferPosition, json_BufferLength
256276 End If
257277
258- json_Converted = ConvertToJson(json_DictionaryCollectionOrArray(json_Index, json_Index2D))
278+ json_Converted = ConvertToJson(json_DictionaryCollectionOrArray(json_Index, json_Index2D), json_Whitespace, json_CurrentIndentation + 2 )
259279
260280 ' For Arrays/Collections, undefined (Empty/Nothing) is treated as null
261281 If json_Converted = "" Then
@@ -265,13 +285,22 @@ Public Function ConvertToJson(ByVal json_DictionaryCollectionOrArray As Variant)
265285 End If
266286 End If
267287
288+ If json_PrettyPrint Then
289+ json_Converted = vbNewLine & json_InnerIndentation & json_Converted
290+ End If
291+
268292 json_BufferAppend json_buffer, json_Converted, json_BufferPosition, json_BufferLength
269293 Next json_Index2D
270294
271- json_BufferAppend json_buffer, "]" , json_BufferPosition, json_BufferLength
295+ If json_PrettyPrint Then
296+ json_BufferAppend json_buffer, vbNewLine, json_BufferPosition, json_BufferLength
297+ End If
298+
299+ json_BufferAppend json_buffer, json_Indentation & "]" , json_BufferPosition, json_BufferLength
272300 json_IsFirstItem2D = True
273301 Else
274- json_Converted = ConvertToJson(json_DictionaryCollectionOrArray(json_Index))
302+ ' 1D Array
303+ json_Converted = ConvertToJson(json_DictionaryCollectionOrArray(json_Index), json_Whitespace, json_CurrentIndentation + 1 )
275304
276305 ' For Arrays/Collections, undefined (Empty/Nothing) is treated as null
277306 If json_Converted = "" Then
@@ -280,6 +309,10 @@ Public Function ConvertToJson(ByVal json_DictionaryCollectionOrArray As Variant)
280309 json_Converted = "null"
281310 End If
282311 End If
312+
313+ If json_PrettyPrint Then
314+ json_Converted = vbNewLine & json_Indentation & json_Converted
315+ End If
283316
284317 json_BufferAppend json_buffer, json_Converted, json_BufferPosition, json_BufferLength
285318 End If
@@ -288,18 +321,36 @@ Public Function ConvertToJson(ByVal json_DictionaryCollectionOrArray As Variant)
288321
289322 On Error GoTo 0
290323
291- json_BufferAppend json_buffer, "]" , json_BufferPosition, json_BufferLength
324+ If json_PrettyPrint Then
325+ json_BufferAppend json_buffer, vbNewLine, json_BufferPosition, json_BufferLength
326+
327+ If VBA.VarType(json_Whitespace) = VBA.vbString Then
328+ json_Indentation = VBA.String $(json_CurrentIndentation, json_Whitespace)
329+ Else
330+ json_Indentation = VBA.Space$(json_CurrentIndentation * json_Whitespace)
331+ End If
332+ End If
333+
334+ json_BufferAppend json_buffer, json_Indentation & "]" , json_BufferPosition, json_BufferLength
292335
293336 ConvertToJson = json_BufferToString(json_buffer, json_BufferPosition, json_BufferLength)
294337
295338 ' Dictionary or Collection
296339 Case VBA.vbObject
340+ If json_PrettyPrint Then
341+ If VBA.VarType(json_Whitespace) = VBA.vbString Then
342+ json_Indentation = VBA.String $(json_CurrentIndentation + 1 , json_Whitespace)
343+ Else
344+ json_Indentation = VBA.Space$((json_CurrentIndentation + 1 ) * json_Whitespace)
345+ End If
346+ End If
347+
297348 ' Dictionary
298349 If VBA.TypeName(json_DictionaryCollectionOrArray) = "Dictionary" Then
299350 json_BufferAppend json_buffer, "{" , json_BufferPosition, json_BufferLength
300351 For Each json_Key In json_DictionaryCollectionOrArray.Keys
301352 ' For Objects, undefined (Empty/Nothing) is not added to object
302- json_Converted = ConvertToJson(json_DictionaryCollectionOrArray(json_Key))
353+ json_Converted = ConvertToJson(json_DictionaryCollectionOrArray(json_Key), json_Whitespace, json_CurrentIndentation + 1 )
303354 If json_Converted = "" Then
304355 json_SkipItem = json_IsUndefined(json_DictionaryCollectionOrArray(json_Key))
305356 Else
@@ -313,10 +364,27 @@ Public Function ConvertToJson(ByVal json_DictionaryCollectionOrArray As Variant)
313364 json_BufferAppend json_buffer, "," , json_BufferPosition, json_BufferLength
314365 End If
315366
316- json_BufferAppend json_buffer, """" & json_Key & """:" & json_Converted, json_BufferPosition, json_BufferLength
367+ If json_PrettyPrint Then
368+ json_Converted = vbNewLine & json_Indentation & """" & json_Key & """: " & json_Converted
369+ Else
370+ json_Converted = """" & json_Key & """:" & json_Converted
371+ End If
372+
373+ json_BufferAppend json_buffer, json_Converted, json_BufferPosition, json_BufferLength
317374 End If
318375 Next json_Key
319- json_BufferAppend json_buffer, "}" , json_BufferPosition, json_BufferLength
376+
377+ If json_PrettyPrint Then
378+ json_BufferAppend json_buffer, vbNewLine, json_BufferPosition, json_BufferLength
379+
380+ If VBA.VarType(json_Whitespace) = VBA.vbString Then
381+ json_Indentation = VBA.String $(json_CurrentIndentation, json_Whitespace)
382+ Else
383+ json_Indentation = VBA.Space$(json_CurrentIndentation * json_Whitespace)
384+ End If
385+ End If
386+
387+ json_BufferAppend json_buffer, json_Indentation & "}" , json_BufferPosition, json_BufferLength
320388
321389 ' Collection
322390 ElseIf VBA.TypeName(json_DictionaryCollectionOrArray) = "Collection" Then
@@ -328,7 +396,7 @@ Public Function ConvertToJson(ByVal json_DictionaryCollectionOrArray As Variant)
328396 json_BufferAppend json_buffer, "," , json_BufferPosition, json_BufferLength
329397 End If
330398
331- json_Converted = ConvertToJson(json_Value)
399+ json_Converted = ConvertToJson(json_Value, json_Whitespace, json_CurrentIndentation + 1 )
332400
333401 ' For Arrays/Collections, undefined (Empty/Nothing) is treated as null
334402 If json_Converted = "" Then
@@ -338,9 +406,24 @@ Public Function ConvertToJson(ByVal json_DictionaryCollectionOrArray As Variant)
338406 End If
339407 End If
340408
409+ If json_PrettyPrint Then
410+ json_Converted = vbNewLine & json_Indentation & json_Converted
411+ End If
412+
341413 json_BufferAppend json_buffer, json_Converted, json_BufferPosition, json_BufferLength
342414 Next json_Value
343- json_BufferAppend json_buffer, "]" , json_BufferPosition, json_BufferLength
415+
416+ If json_PrettyPrint Then
417+ json_BufferAppend json_buffer, vbNewLine, json_BufferPosition, json_BufferLength
418+
419+ If VBA.VarType(json_Whitespace) = VBA.vbString Then
420+ json_Indentation = VBA.String $(json_CurrentIndentation, json_Whitespace)
421+ Else
422+ json_Indentation = VBA.Space$(json_CurrentIndentation * json_Whitespace)
423+ End If
424+ End If
425+
426+ json_BufferAppend json_buffer, json_Indentation & "]" , json_BufferPosition, json_BufferLength
344427 End If
345428
346429 ConvertToJson = json_BufferToString(json_buffer, json_BufferPosition, json_BufferLength)
0 commit comments