-
Notifications
You must be signed in to change notification settings - Fork 0
/
VbscriptPrettifyJSON.vbs
201 lines (197 loc) · 5.09 KB
/
VbscriptPrettifyJSON.vbs
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
' ==============================================================================================
' Adaptation of JSONToXML() function for VbscriptPrettifyJSON().
' Author: Praveen Nandagiri ([email protected])
'
' JSONToXML() Credits:
' Visit: https://stackoverflow.com/a/12171836/1751166
' Author: https://stackoverflow.com/users/881441/stephen-quan
' ==============================================================================================
Const stateRoot = 0
Const stateNameQuoted = 1
Const stateNameFinished = 2
Const stateValue = 3
Const stateValueQuoted = 4
Const stateValueQuotedEscaped = 5
Const stateValueQuotedEscapedHex = 6
Const stateValueUnquoted = 7
Const stateValueUnquotedEscaped = 8
Dim iLevel
Dim bLastLevel
Function VbscriptPrettifyJSON(json)
Dim out
Dim i, ch, state, name, value, sHex
out = ""
bLastLevel = ""
state = stateRoot
For i = 1 to Len(json)
ch = Mid(json, i, 1)
Select Case state
Case stateRoot
Select Case ch
Case "["
out = Append(out, ch, "", 1)
Case "{"
out = Append(out, ch, "", 1)
Case """"
state = stateNameQuoted
name = ""
out = Append(out, ch, "/r/n", 0)
Case ","
state = stateRoot
out = Append(out, ch, "", 0)
Case "}"
out = Append(out, ch, "/r/n", -1)
Case "]"
out = Append(out, ch, "/r/n", -1)
End Select
Case stateNameQuoted
Select Case ch
Case """"
state = stateNameFinished
out = Append(out, ch, "", 0)
Case Else
name = name + ch
out = Append(out, ch, "", 0)
End Select
Case stateNameFinished
Select Case ch
Case ":"
value = ""
State = stateValue
out = Append(out, ch, " ", 0)
Case Else
State = stateRoot
out = Append(out, ch, "", 0)
End Select
Case stateValue
Select Case ch
Case """"
State = stateValueQuoted
out = Append(out, ch, " ", 0)
Case "{"
State = stateRoot
out = Append(out, ch, "/r/n", 1)
Case "["
State = stateRoot
out = Append(out, ch, "/r/n", 1)
Case " "
Case Chr(9)
Case vbCr
Case vbLF
Case Else
value = ch
State = stateValueUnquoted
End Select
Case stateValueQuoted
Select Case ch
Case """"
state = stateRoot
out = Append(out, value + ch, "", 0)
Case "\"
state = stateValueQuotedEscaped
Case Else
value = value + ch
End Select
Case stateValueQuotedEscaped
If ch = "u" Then 'Four digit hex. Ex: o = 00f8
sHex = ""
state = stateValueQuotedEscapedHex
Else
Select Case ch
Case """"
value = value + """"
Case "\"
value = value + "\"
Case "/"
value = value + "/"
Case "b" 'Backspace
value = value + chr(08)
Case "f" 'Form-Feed
value = value + chr(12)
Case "n" 'New-line (LineFeed(10))
value = value + vbLF
Case "r" 'New-line (CarriageReturn/CRLF(13))
value = value + vbCR
Case "t" 'Horizontal-Tab (09)
value = value + vbTab
Case Else
'do not accept any other escape sequence
End Select
state = stateValueQuoted
End If
Case stateValueQuotedEscapedHex
sHex = sHex + ch
If len(sHex) = 4 Then
on error resume next
value = value + Chr("&H" & sHex) 'Hex to String conversion
on error goto 0
state = stateValueQuoted
End If
Case stateValueUnquoted
Select Case ch
Case "}"
state = stateRoot
out = Append(out, ch, "/r/n", 1)
Case "]"
state = stateRoot
out = Append(out, ch, "/r/n", 1)
Case ","
state = stateRoot
out = Append(out, ch, "", 0)
Case "\"
state = stateValueUnquotedEscaped
Case Else
value = value + ch
End Select
Case stateValueUnquotedEscaped ' @@TODO: Handle escape sequences
value = value + ch
state = stateValueUnquoted
End Select
Next
VbscriptPrettifyJSON = out
End Function
Function Append(out, sChar, sPrepend, iLvl)
Select Case sPrepend
Case vbCr, "/r"
out = out + vbCr + sChar
Case vbLF, "/n"
out = out + vbLF + sChar
Case vbCrLF, "/r/n"
If iLvl = 1 Then
iLevel = iLevel + 1
ElseIf iLvl = -1 Then
iLevel = iLevel - 1
End IF
If sChar <> "{" And sChar <> "[" And sChar <> "}" And sChar <> "]" Then sChar = " " + sChar
If iLvl = -1 Then 'Fixing an issue in levelling down
out = out + vbCrLF + Joiner(iLevel + 1, vbTab) + sChar
Else
out = out + vbCrLF + Joiner(iLevel, vbTab) + sChar
End If
bLastLevel = iLvl
Case vbTab, "/t"
out = out + vbTab + sChar
Case Else
If Left(sPrepend, 2) = "/s" Then
on error resume next
iSpaces = cInt(Right(sPrepend, len(sPrepend)-2))
out = out + Space(iSpaces) + sChar
on error goto 0
Else
sPrepend = Replace(sPrepend, "/r/n", vbCrLF)
sPrepend = Replace(sPrepend, "/r", vbLF)
sPrepend = Replace(sPrepend, "/n", vbLF)
sPrepend = Replace(sPrepend, "/t", vbTab)
out = out + sPrepend + sChar
End If
End Select
Append = out
End Function
Function Joiner(iLvl, sChar)
Dim lOut
lOut = ""
For i = 1 To iLvl
lOut = lOut + sChar
Next
Joiner = lOut
End Function