-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.vbs
187 lines (182 loc) · 5.5 KB
/
index.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
' ==============================================================================================
' Adaptation of JSONToXML() function for enhancements and bugfixes.
' Author: Praveen Nandagiri ([email protected])
' Enhancement#1: Arrays are now rendered as Text Nodes
' Enhancement#2: Handled Escape characters (incl. Hex). Refer: http://www.json.org/
'
' Credits:
' Visit: https://stackoverflow.com/a/12171836/1751166
' Author: https://stackoverflow.com/users/881441/stephen-quan
' ==============================================================================================
Class JSONToXML
Private stateRoot
Private stateNameQuoted
Private stateNameFinished
Private stateValue
Private stateValueQuoted
Private stateValueQuotedEscaped
Private stateValueQuotedEscapedHex
Private stateValueUnquoted
Private stateValueUnquotedEscaped
Private Sub Class_Initialize
stateRoot = 0
stateNameQuoted = 1
stateNameFinished = 2
stateValue = 3
stateValueQuoted = 4
stateValueQuotedEscaped = 5
stateValueQuotedEscapedHex = 6
stateValueUnquoted = 7
stateValueUnquotedEscaped = 8
End Sub
Public Function toXml(json)
Dim dom, xmlElem, i, ch, state, name, value, sHex
Set dom = CreateObject("Microsoft.XMLDOM")
state = stateRoot
For i = 1 to Len(json)
ch = Mid(json, i, 1)
Select Case state
Case stateRoot
Select Case ch
Case "["
If dom.documentElement is Nothing Then
Set xmlElem = dom.CreateElement("ARRAY")
Set dom.documentElement = xmlElem
Else
Set xmlElem = XMLCreateChild(xmlElem, "ARRAY")
End If
Case "{"
If dom.documentElement is Nothing Then
Set xmlElem = dom.CreateElement("ROOT")
Set dom.documentElement = xmlElem
Else
Set xmlElem = XMLCreateChild(xmlElem, "OBJECT")
End If
Case """"
state = stateNameQuoted
name = ""
Case "}"
Set xmlElem = xmlElem.parentNode
Case "]"
Set xmlElem = xmlElem.parentNode
End Select
Case stateNameQuoted
Select Case ch
Case """"
state = stateNameFinished
Case Else
name = name + ch
End Select
Case stateNameFinished
Select Case ch
Case ":"
value = ""
State = stateValue
Case Else '@@Enhancement#1: Handling Array values
Set xmlitem = dom.createTextNode(name)
xmlElem.appendChild(xmlitem)
State = stateRoot
End Select
Case stateValue
Select Case ch
Case """"
State = stateValueQuoted
Case "{"
Set xmlElem = XMLCreateChild(xmlElem, name)
State = stateRoot
Case "["
Set xmlElem = XMLCreateChild(xmlElem, name)
State = stateRoot
Case " "
Case Chr(9)
Case vbCr
Case vbLF
Case Else
value = ch
State = stateValueUnquoted
End Select
Case stateValueQuoted
Select Case ch
Case """"
xmlElem.setAttribute name, value
state = stateRoot
Case "\"
state = stateValueQuotedEscaped
Case Else
value = value + ch
End Select
Case stateValueQuotedEscaped ' @@Enhancement#2: Handle escape sequences
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 "}"
xmlElem.setAttribute name, value
Set xmlElem = xmlElem.parentNode
state = stateRoot
Case "]"
xmlElem.setAttribute name, value
Set xmlElem = xmlElem.parentNode
state = stateRoot
Case ","
xmlElem.setAttribute name, value
state = stateRoot
Case "\"
state = stateValueUnquotedEscaped
Case Else
value = value + ch
End Select
Case stateValueUnquotedEscaped ' @@TODO: Handle escape sequences
value = value + ch
state = stateValueUnquoted
End Select
Next
Set toXml = dom
End Function
Private Function XMLCreateChild(xmlParent, tagName)
Dim xmlChild
If xmlParent is Nothing Then
Set XMLCreateChild = Nothing
Exit Function
End If
If xmlParent.ownerDocument is Nothing Then
Set XMLCreateChild = Nothing
Exit Function
End If
Set xmlChild = xmlParent.ownerDocument.createElement(tagName)
xmlParent.appendChild xmlChild
Set XMLCreateChild = xmlChild
End Function
End Class