forked from Winand/mikrotik-json-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JParseFunctions
386 lines (365 loc) · 11.9 KB
/
JParseFunctions
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# -------------------------------- JParseFunctions ---------------------------------------------------
# ------------------------------- fJParsePrint ----------------------------------------------------------------
:global fJParsePrint
:if (!any $fJParsePrint) do={ :global fJParsePrint do={
:global JParseOut
:local TempPath
:global fJParsePrint
:if ([:len $1] = 0) do={
:set $1 "\$JParseOut"
:set $2 $JParseOut
}
:foreach k,v in=$2 do={
:if ([:typeof $k] = "str") do={
:set k "\"$k\""
}
:set TempPath ($1. "->" . $k)
:if ([:typeof $v] = "array") do={
:if ([:len $v] > 0) do={
$fJParsePrint $TempPath $v
} else={
:put "$TempPath = [] ($[:typeof $v])"
}
} else={
:put "$TempPath = $v ($[:typeof $v])"
}
}
}}
# ------------------------------- fJParsePrintVar ----------------------------------------------------------------
:global fJParsePrintVar
:if (!any $fJParsePrintVar) do={ :global fJParsePrintVar do={
:global JParseOut
:local TempPath
:global fJParsePrintVar
:local fJParsePrintRet ""
:if ([:len $1] = 0) do={
:set $1 "\$JParseOut"
:set $2 $JParseOut
}
:foreach k,v in=$2 do={
:if ([:typeof $k] = "str") do={
:set k "\"$k\""
}
:set TempPath ($1. "->" . $k)
:if ($fJParsePrintRet != "") do={
:set fJParsePrintRet ($fJParsePrintRet . "\r\n")
}
:if ([:typeof $v] = "array") do={
:if ([:len $v] > 0) do={
:set fJParsePrintRet ($fJParsePrintRet . [$fJParsePrintVar $TempPath $v])
} else={
:set fJParsePrintRet ($fJParsePrintRet . "$TempPath = [] ($[:typeof $v])")
}
} else={
:set fJParsePrintRet ($fJParsePrintRet . "$TempPath = $v ($[:typeof $v])")
}
}
:return $fJParsePrintRet
}}
# ------------------------------- fJSkipWhitespace ----------------------------------------------------------------
:global fJSkipWhitespace
:if (!any $fJSkipWhitespace) do={ :global fJSkipWhitespace do={
:global Jpos
:global JSONIn
:global Jdebug
:while ($Jpos < [:len $JSONIn] and ([:pick $JSONIn $Jpos] ~ "[ \r\n\t]")) do={
:set Jpos ($Jpos + 1)
}
:if ($Jdebug) do={:put "fJSkipWhitespace: Jpos=$Jpos Char=$[:pick $JSONIn $Jpos]"}
}}
# -------------------------------- fJParse ---------------------------------------------------------------
:global fJParse
:if (!any $fJParse) do={ :global fJParse do={
:global Jpos
:global JSONIn
:global Jdebug
:global fJSkipWhitespace
:local Char
:if (!$1) do={
:set Jpos 0
}
$fJSkipWhitespace
:set Char [:pick $JSONIn $Jpos]
:if ($Jdebug) do={:put "fJParse: Jpos=$Jpos Char=$Char"}
:if ($Char="{") do={
:set Jpos ($Jpos + 1)
:global fJParseObject
:return [$fJParseObject]
} else={
:if ($Char="[") do={
:set Jpos ($Jpos + 1)
:global fJParseArray
:return [$fJParseArray]
} else={
:if ($Char="\"") do={
:set Jpos ($Jpos + 1)
:global fJParseString
:return [$fJParseString]
} else={
# :if ([:pick $JSONIn $Jpos ($Jpos+2)]~"^-\?[0-9]") do={
:if ($Char~"[eE0-9.+-]") do={
:global fJParseNumber
:return [$fJParseNumber]
} else={
:if ($Char="n" and [:pick $JSONIn $Jpos ($Jpos+4)]="null") do={
:set Jpos ($Jpos + 4)
:return []
} else={
:if ($Char="t" and [:pick $JSONIn $Jpos ($Jpos+4)]="true") do={
:set Jpos ($Jpos + 4)
:return true
} else={
:if ($Char="f" and [:pick $JSONIn $Jpos ($Jpos+5)]="false") do={
:set Jpos ($Jpos + 5)
:return false
} else={
:put "Err.Raise 8732. No JSON object could be fJParseed"
:set Jpos ($Jpos + 1)
:return []
}
}
}
}
}
}
}
}}
#-------------------------------- fJParseString ---------------------------------------------------------------
:global fJParseString
:if (!any $fJParseString) do={ :global fJParseString do={
:global Jpos
:global JSONIn
:global Jdebug
:global fUnicodeToUTF8
:local Char
:local StartIdx
:local Char2
:local TempString ""
:local UTFCode
:local Unicode
:set StartIdx $Jpos
:set Char [:pick $JSONIn $Jpos]
:if ($Jdebug) do={:put "fJParseString: Jpos=$Jpos Char=$Char"}
:while ($Jpos < [:len $JSONIn] and $Char != "\"") do={
:if ($Char="\\") do={
:set Char2 [:pick $JSONIn ($Jpos + 1)]
:if ($Char2 = "u") do={
:set UTFCode [:tonum "0x$[:pick $JSONIn ($Jpos+2) ($Jpos+6)]"]
:if ($UTFCode>=0xD800 and $UTFCode<=0xDFFF) do={
# Surrogate pair
:set Unicode (($UTFCode & 0x3FF) << 10)
:set UTFCode [:tonum "0x$[:pick $JSONIn ($Jpos+8) ($Jpos+12)]"]
:set Unicode ($Unicode | ($UTFCode & 0x3FF) | 0x10000)
:set TempString ($TempString . [:pick $JSONIn $StartIdx $Jpos] . [$fUnicodeToUTF8 $Unicode])
:set Jpos ($Jpos + 12)
} else= {
# Basic Multilingual Plane (BMP)
:set Unicode $UTFCode
:set TempString ($TempString . [:pick $JSONIn $StartIdx $Jpos] . [$fUnicodeToUTF8 $Unicode])
:set Jpos ($Jpos + 6)
}
:set StartIdx $Jpos
:if ($Jdebug) do={:put "fJParseString Unicode: $Unicode"}
} else={
:if ($Char2 ~ "[\\bfnrt\"]") do={
:if ($Jdebug) do={:put "fJParseString escape: Char+Char2 $Char$Char2"}
:set TempString ($TempString . [:pick $JSONIn $StartIdx $Jpos] . [[:parse "(\"\\$Char2\")"]])
:set Jpos ($Jpos + 2)
:set StartIdx $Jpos
} else={
:if ($Char2 = "/") do={
:if ($Jdebug) do={:put "fJParseString /: Char+Char2 $Char$Char2"}
:set TempString ($TempString . [:pick $JSONIn $StartIdx $Jpos] . "/")
:set Jpos ($Jpos + 2)
:set StartIdx $Jpos
} else={
:put "Err.Raise 8732. Invalid escape"
:set Jpos ($Jpos + 2)
}
}
}
} else={
:set Jpos ($Jpos + 1)
}
:set Char [:pick $JSONIn $Jpos]
}
:set TempString ($TempString . [:pick $JSONIn $StartIdx $Jpos])
:set Jpos ($Jpos + 1)
:if ($Jdebug) do={:put "fJParseString: $TempString"}
:return $TempString
}}
#-------------------------------- fJParseNumber ---------------------------------------------------------------
:global fJParseNumber
:if (!any $fJParseNumber) do={ :global fJParseNumber do={
:global Jpos
:local StartIdx
:global JSONIn
:global Jdebug
:local NumberString
:local Number
:set StartIdx $Jpos
:set Jpos ($Jpos + 1)
:while ($Jpos < [:len $JSONIn] and [:pick $JSONIn $Jpos]~"[eE0-9.+-]") do={
:set Jpos ($Jpos + 1)
}
:set NumberString [:pick $JSONIn $StartIdx $Jpos]
:set Number [:tonum $NumberString]
:if ([:typeof $Number] = "num") do={
:if ($Jdebug) do={:put "fJParseNumber: StartIdx=$StartIdx Jpos=$Jpos $Number ($[:typeof $Number])"}
:return $Number
} else={
:if ($Jdebug) do={:put "fJParseNumber: StartIdx=$StartIdx Jpos=$Jpos $NumberString ($[:typeof $NumberString])"}
:return $NumberString
}
}}
#-------------------------------- fJParseArray ---------------------------------------------------------------
:global fJParseArray
:if (!any $fJParseArray) do={ :global fJParseArray do={
:global Jpos
:global JSONIn
:global Jdebug
:global fJParse
:global fJSkipWhitespace
:local Value
:local ParseArrayRet [:toarray ""]
$fJSkipWhitespace
:while ($Jpos < [:len $JSONIn] and [:pick $JSONIn $Jpos]!= "]") do={
:set Value [$fJParse true]
:set ($ParseArrayRet->([:len $ParseArrayRet])) $Value
:if ($Jdebug) do={:put "fJParseArray: Value="; :put $Value}
$fJSkipWhitespace
:if ([:pick $JSONIn $Jpos] = ",") do={
:set Jpos ($Jpos + 1)
$fJSkipWhitespace
}
}
:set Jpos ($Jpos + 1)
# :if ($Jdebug) do={:put "ParseArrayRet: "; :put $ParseArrayRet}
:return $ParseArrayRet
}}
# -------------------------------- fJParseObject ---------------------------------------------------------------
:global fJParseObject
:if (!any $fJParseObject) do={ :global fJParseObject do={
:global Jpos
:global JSONIn
:global Jdebug
:global fJSkipWhitespace
:global fJParseString
:global fJParse
# Syntax :local ParseObjectRet ({}) don't work in recursive call, use [:toarray ""] for empty array!!!
:local ParseObjectRet [:toarray ""]
:local Key
:local Value
:local ExitDo false
$fJSkipWhitespace
:while ($Jpos < [:len $JSONIn] and [:pick $JSONIn $Jpos]!="}" and !$ExitDo) do={
:if ([:pick $JSONIn $Jpos]!="\"") do={
:put "Err.Raise 8732. Expecting property name"
:set ExitDo true
} else={
:set Jpos ($Jpos + 1)
:set Key [$fJParseString]
$fJSkipWhitespace
:if ([:pick $JSONIn $Jpos] != ":") do={
:put "Err.Raise 8732. Expecting : delimiter"
:set ExitDo true
} else={
:set Jpos ($Jpos + 1)
:set Value [$fJParse true]
:set ($ParseObjectRet->$Key) $Value
:if ($Jdebug) do={:put "fJParseObject: Key=$Key Value="; :put $Value}
$fJSkipWhitespace
:if ([:pick $JSONIn $Jpos]=",") do={
:set Jpos ($Jpos + 1)
$fJSkipWhitespace
}
}
}
}
:set Jpos ($Jpos + 1)
# :if ($Jdebug) do={:put "ParseObjectRet: "; :put $ParseObjectRet}
:return $ParseObjectRet
}}
# ------------------- fByteToEscapeChar ----------------------
:global fByteToEscapeChar
:if (!any $fByteToEscapeChar) do={ :global fByteToEscapeChar do={
# :set $1 [:tonum $1]
:return [[:parse "(\"\\$[:pick "0123456789ABCDEF" (($1 >> 4) & 0xF)]$[:pick "0123456789ABCDEF" ($1 & 0xF)]\")"]]
}}
# ------------------- fUnicodeToUTF8----------------------
:global fUnicodeToUTF8
:if (!any $fUnicodeToUTF8) do={ :global fUnicodeToUTF8 do={
:global fByteToEscapeChar
# :local Ubytes [:tonum $1]
:local Nbyte
:local EscapeStr ""
:if ($1 < 0x80) do={
:set EscapeStr [$fByteToEscapeChar $1]
} else={
:if ($1 < 0x800) do={
:set Nbyte 2
} else={
:if ($1 < 0x10000) do={
:set Nbyte 3
} else={
:if ($1 < 0x20000) do={
:set Nbyte 4
} else={
:if ($1 < 0x4000000) do={
:set Nbyte 5
} else={
:if ($1 < 0x80000000) do={
:set Nbyte 6
}
}
}
}
}
:for i from=2 to=$Nbyte do={
:set EscapeStr ([$fByteToEscapeChar ($1 & 0x3F | 0x80)] . $EscapeStr)
:set $1 ($1 >> 6)
}
:set EscapeStr ([$fByteToEscapeChar (((0xFF00 >> $Nbyte) & 0xFF) | $1)] . $EscapeStr)
}
:return $EscapeStr
}}
# ------------------- Load JSON from arg --------------------------------
global JSONLoads
if (!any $JSONLoads) do={ global JSONLoads do={
global JSONIn $1
global fJParse
local ret [$fJParse]
set JSONIn
global Jpos; set Jpos
global Jdebug; if (!$Jdebug) do={set Jdebug}
return $ret
}}
# ------------------- Load JSON from file --------------------------------
global JSONLoad
if (!any $JSONLoad) do={ global JSONLoad do={
if ([len [/file find name=$1]] > 0) do={
global JSONLoads
return [$JSONLoads [/file get $1 contents]]
}
}}
# ------------------- Unload JSON parser library ----------------------
global JSONUnload
if (!any $JSONUnload) do={ global JSONUnload do={
global JSONIn; set JSONIn
global Jpos; set Jpos
global Jdebug; set Jdebug
global fByteToEscapeChar; set fByteToEscapeChar
global fJParse; set fJParse
global fJParseArray; set fJParseArray
global fJParseNumber; set fJParseNumber
global fJParseObject; set fJParseObject
global fJParsePrint; set fJParsePrint
global fJParsePrintVar; set fJParsePrintVar
global fJParseString; set fJParseString
global fJSkipWhitespace; set fJSkipWhitespace
global fUnicodeToUTF8; set fUnicodeToUTF8
global JSONLoads; set JSONLoads
global JSONLoad; set JSONLoad
global JSONUnload; set JSONUnload
}}
# ------------------- End JParseFunctions----------------------