-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDictToJSON.bas
72 lines (50 loc) · 1.9 KB
/
DictToJSON.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
Sub saveJSON(filename As String, Entity As Variant)
Dim fs As New FileSystemObject
Dim ts As TextStream
Set ts = fs.OpenTextFile(ActiveWorkbook.Path & "\" & filename, ForWriting, True)
ts.Write toJSON(Entity)
ts.Close
End Sub
Function toJSON(entity As Variant) As String
Dim index As Long
Dim s As String
If IsArray(entity) Then
s = s & "["
For index = LBound(entity) To UBound(entity)
s = s & toJSON(entity(index))
If index < UBound(entity) Then s = s & ","
Next
s = s & "]"
Else
Select Case TypeName(entity)
Case "Empty"
s = s & "null"
Case "Integer", "Long", "Single", "Double"
s = s & entity
Case "Boolean"
s = s & """" & entity & """"
Case "String"
s = s & """" & entity & """"
Case "Dictionary"
s = s & "{"
Dim keylist As Variant
keylist = entity.Keys
For index = LBound(keylist) To UBound(keylist)
s = s & """" & keylist(index) & """:"
s = s & toJSON(entity(keylist(index)))
If index < UBound(keylist) Then s = s & ","
Next
s = s & "}"
Case "Collection"
s = s & "["
For index = 1 To entity.count
s = s & toJSON(entity(index))
If index < entity.count Then s = s & ","
Next
s = s & "]"
Case Else
s = s & """" & TypeName(entity) & """"
End Select
End If
toJSON = s
End Function