-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmodJSON.bas
More file actions
537 lines (433 loc) · 13.9 KB
/
modJSON.bas
File metadata and controls
537 lines (433 loc) · 13.9 KB
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
Attribute VB_Name = "modJSON"
'Author: David Zimmer <dzzie@yahoo.com>
'AI: Claude.ai
'Site: http://sandsprite.com
'License: MIT
Option Explicit
Public Function IsJSONMethod(methodName As String) As Boolean
IsJSONMethod = (methodName = "parse" Or methodName = "stringify")
End Function
Public Function CallJSONMethod(methodName As String, args As Collection) As CValue
Dim result As New CValue
Select Case methodName
Case "parse"
' JSON.parse(text)
If args.count = 0 Then
result.vType = vtUndefined
Else
Dim jsonText As String
Dim arg As CValue
Set arg = args(1)
jsonText = arg.ToString()
' Parse JSON string to CValue
Set result = ParseJSON(jsonText)
End If
Case "stringify"
' JSON.stringify(value)
If args.count = 0 Then
result.vType = vtUndefined
Else
Set arg = args(1)
' Convert CValue to JSON string
result.vType = vtString
result.strVal = StringifyJSON(arg)
End If
Case Else
result.vType = vtUndefined
End Select
Set CallJSONMethod = result
End Function
' Parse JSON string into CValue
Public Function ParseJSON(jsonText As String) As CValue
Dim result As New CValue
Dim pos As Long
pos = 1
' Skip whitespace
pos = SkipWhitespace(jsonText, pos)
' Parse value
Set result = ParseJSONValue(jsonText, pos)
Set ParseJSON = result
End Function
Public Function SkipWhitespace(text As String, pos As Long) As Long
Do While pos <= Len(text)
Dim ch As String
ch = Mid$(text, pos, 1)
If ch = " " Or ch = vbTab Or ch = vbCr Or ch = vbLf Then
pos = pos + 1
Else
Exit Do
End If
Loop
SkipWhitespace = pos
End Function
Public Function ParseJSONValue(text As String, ByRef pos As Long) As CValue
Dim result As New CValue
pos = SkipWhitespace(text, pos)
If pos > Len(text) Then
result.vType = vtUndefined
Set ParseJSONValue = result
Exit Function
End If
Dim ch As String
ch = Mid$(text, pos, 1)
Select Case ch
Case "{"
Set result = ParseJSONObject(text, pos)
Case "["
Set result = ParseJSONArray(text, pos)
Case """"
Set result = ParseJSONString(text, pos)
Case "t", "f"
Set result = ParseJSONBoolean(text, pos)
Case "n"
Set result = ParseJSONNull(text, pos)
Case "-", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
Set result = ParseJSONNumber(text, pos)
Case Else
result.vType = vtUndefined
End Select
Set ParseJSONValue = result
End Function
' Update ParseJSONObject in CInterpreter.cls
Private Function ParseJSONObject(text As String, ByRef pos As Long) As CValue
Dim result As New CValue
result.vType = vtObject
Set result.objectProps = New Collection
Set result.objectKeys = New Collection
pos = pos + 1 ' Skip {
pos = SkipWhitespace(text, pos)
' Empty object
If Mid$(text, pos, 1) = "}" Then
pos = pos + 1
Set ParseJSONObject = result
Exit Function
End If
Do While pos <= Len(text)
' Parse key
pos = SkipWhitespace(text, pos)
If Mid$(text, pos, 1) <> """" Then Exit Do
Dim key As CValue
Set key = ParseJSONString(text, pos)
' Expect colon
pos = SkipWhitespace(text, pos)
If Mid$(text, pos, 1) <> ":" Then Exit Do
pos = pos + 1
' Parse value
Dim val As CValue
Set val = ParseJSONValue(text, pos)
' Add to object
On Error Resume Next
result.objectProps.Remove key.strVal
result.objectKeys.Remove key.strVal
On Error GoTo 0
result.objectProps.add val, key.strVal
result.objectKeys.add key.strVal, key.strVal ' NEW: Track the key
' Check for comma or end
pos = SkipWhitespace(text, pos)
If Mid$(text, pos, 1) = "}" Then
pos = pos + 1
Exit Do
ElseIf Mid$(text, pos, 1) = "," Then
pos = pos + 1
Else
Exit Do
End If
Loop
Set ParseJSONObject = result
End Function
Public Function ParseJSONArray(text As String, ByRef pos As Long) As CValue
Dim result As New CValue
result.vType = vtArray
Set result.arrayVal = New Collection
pos = pos + 1 ' Skip [
pos = SkipWhitespace(text, pos)
' Empty array
If Mid$(text, pos, 1) = "]" Then
pos = pos + 1
Set ParseJSONArray = result
Exit Function
End If
Do While pos <= Len(text)
' Parse value
Dim val As CValue
Set val = ParseJSONValue(text, pos)
result.arrayVal.add val
' Check for comma or end
pos = SkipWhitespace(text, pos)
If Mid$(text, pos, 1) = "]" Then
pos = pos + 1
Exit Do
ElseIf Mid$(text, pos, 1) = "," Then
pos = pos + 1
Else
Exit Do
End If
Loop
Set ParseJSONArray = result
End Function
Public Function ParseJSONString(text As String, ByRef pos As Long) As CValue
Dim result As New CValue
result.vType = vtString
pos = pos + 1 ' Skip opening "
Dim str As String
str = ""
Do While pos <= Len(text)
Dim ch As String
ch = Mid$(text, pos, 1)
If ch = """" Then
pos = pos + 1
Exit Do
ElseIf ch = "\" Then
pos = pos + 1
If pos <= Len(text) Then
ch = Mid$(text, pos, 1)
Select Case ch
Case "n": str = str & vbLf
Case "r": str = str & vbCr
Case "t": str = str & vbTab
Case "\": str = str & "\"
Case "/": str = str & "/"
Case """": str = str & """"
Case "b", "f": ' Ignore
Case "u"
' Unicode escape (simplified)
If pos + 4 <= Len(text) Then
Dim hex As String
hex = Mid$(text, pos + 1, 4)
On Error Resume Next
str = str & ChrW$("&H" & hex)
On Error GoTo 0
pos = pos + 4
End If
Case Else
str = str & ch
End Select
pos = pos + 1
End If
Else
str = str & ch
pos = pos + 1
End If
Loop
result.strVal = str
' SNEAKY AUTO-CONVERT: strict validation only - 0x hex strings or numbers as quoted strings we convert to int/bigint unsigned.
If Len(str) > 0 And InStr(str, " ") = 0 Then
' Try hex (0x prefix)
If Len(str) > 2 And Left$(str, 2) = "0x" Then
Dim hexPart As String
hexPart = Mid$(str, 3)
If IsStrictHex(hexPart) Then
If result.LoadNumFromStr(str) <> vtUndefined Then
' Converted
End If
End If
' Try decimal (strict - only digits, optional leading -)
ElseIf IsStrictNumeric(str) Then
If result.LoadNumFromStr(str) <> vtUndefined Then
' Converted
End If
End If
End If
Set ParseJSONString = result
End Function
Private Function IsStrictHex(s As String) As Boolean
If Len(s) = 0 Then Exit Function
Dim i As Long, ch As String
For i = 1 To Len(s)
ch = Mid$(s, i, 1)
If Not ((ch >= "0" And ch <= "9") Or _
(ch >= "a" And ch <= "f") Or _
(ch >= "A" And ch <= "F")) Then
Exit Function
End If
Next
IsStrictHex = True
End Function
Private Function IsStrictNumeric(s As String) As Boolean
If Len(s) = 0 Then Exit Function
Dim i As Long
Dim ch As String
Dim start As Long
start = 1
' Allow leading minus
If Left$(s, 1) = "-" Then
If Len(s) = 1 Then Exit Function ' Just "-" is invalid
start = 2
End If
' Rest must be digits only
For i = start To Len(s)
ch = Mid$(s, i, 1)
If Not (ch >= "0" And ch <= "9") Then
Exit Function
End If
Next
IsStrictNumeric = True
End Function
Public Function ParseJSONNumber(text As String, ByRef pos As Long) As CValue
Dim result As New CValue
result.vType = vtNumber
Dim numStr As String
numStr = ""
' Optional minus
If Mid$(text, pos, 1) = "-" Then
numStr = "-"
pos = pos + 1
End If
' Digits
Do While pos <= Len(text)
Dim ch As String
ch = Mid$(text, pos, 1)
If ch >= "0" And ch <= "9" Then
numStr = numStr & ch
pos = pos + 1
Else
Exit Do
End If
Loop
' Decimal point
If pos <= Len(text) And Mid$(text, pos, 1) = "." Then
numStr = numStr & "."
pos = pos + 1
Do While pos <= Len(text)
ch = Mid$(text, pos, 1)
If ch >= "0" And ch <= "9" Then
numStr = numStr & ch
pos = pos + 1
Else
Exit Do
End If
Loop
End If
' Exponent
If pos <= Len(text) Then
ch = Mid$(text, pos, 1)
If ch = "e" Or ch = "E" Then
numStr = numStr & "E"
pos = pos + 1
If pos <= Len(text) Then
ch = Mid$(text, pos, 1)
If ch = "+" Or ch = "-" Then
numStr = numStr & ch
pos = pos + 1
End If
End If
Do While pos <= Len(text)
ch = Mid$(text, pos, 1)
If ch >= "0" And ch <= "9" Then
numStr = numStr & ch
pos = pos + 1
Else
Exit Do
End If
Loop
End If
End If
result.numVal = CDbl(numStr)
Set ParseJSONNumber = result
End Function
Public Function ParseJSONBoolean(text As String, ByRef pos As Long) As CValue
Dim result As New CValue
result.vType = vtBoolean
If Mid$(text, pos, 4) = "true" Then
result.boolVal = True
pos = pos + 4
ElseIf Mid$(text, pos, 5) = "false" Then
result.boolVal = False
pos = pos + 5
End If
Set ParseJSONBoolean = result
End Function
Public Function ParseJSONNull(text As String, ByRef pos As Long) As CValue
Dim result As New CValue
result.vType = vtNull
If Mid$(text, pos, 4) = "null" Then
pos = pos + 4
End If
Set ParseJSONNull = result
End Function
' Convert CValue to JSON string
Public Function StringifyJSONArray(arr As Collection) As String
Dim result As String
Dim i As Long
result = "["
For i = 1 To arr.count
If i > 1 Then result = result & ","
Dim val As CValue
Set val = arr(i)
result = result & StringifyJSON(val)
Next
result = result & "]"
StringifyJSONArray = result
End Function
Private Function StringifyJSONObject(props As Collection, Keys As Collection) As String
Dim result As String
Dim i As Long
Dim keyStr As String
Dim val As CValue
result = "{"
For i = 1 To Keys.count
If i > 1 Then result = result & ","
' Get key
keyStr = Keys(i)
' Get value
Set val = props(keyStr)
' Add to result
result = result & """" & EscapeJSONString(keyStr) & """:" & StringifyJSON(val)
Next
result = result & "}"
StringifyJSONObject = result
End Function
' Update StringifyJSON to pass keys:
Function StringifyJSON(val As CValue) As String
Select Case val.vType
Case vtUndefined
StringifyJSON = "undefined"
Case vtNull
StringifyJSON = "null"
Case vtBoolean
StringifyJSON = IIf(val.boolVal, "true", "false")
Case vtNumber
StringifyJSON = CStr(val.numVal)
Case vtString
StringifyJSON = """" & EscapeJSONString(val.strVal) & """"
Case vtArray
StringifyJSON = StringifyJSONArray(val.arrayVal)
Case vtObject
StringifyJSON = StringifyJSONObject(val.objectProps, val.objectKeys) ' CHANGED
Case vtfunction
StringifyJSON = "undefined"
Case vtCOMObject
StringifyJSON = "null"
Case vtInt64
Dim U64 As New ULong64
U64.mode = mSigned
U64.rawValue = val.int64Val
StringifyJSON = U64.ToString(mUnsigned)
Case Else
StringifyJSON = "null"
End Select
End Function
Public Function EscapeJSONString(str As String) As String
Dim result As String
Dim i As Long
Dim ch As String
result = ""
For i = 1 To Len(str)
ch = Mid$(str, i, 1)
Select Case ch
Case """"
result = result & "\"""
Case "\"
result = result & "\\"
Case vbCr
result = result & "\r"
Case vbLf
result = result & "\n"
Case vbTab
result = result & "\t"
Case Else
result = result & ch
End Select
Next
EscapeJSONString = result
End Function