-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSON.au3
430 lines (367 loc) · 18.2 KB
/
JSON.au3
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
; ============================================================================================================================
; File : Json.au3 (2015.01.08)
; Purpose : A Non-Strict JavaScript Object Notation (JSON) Parser UDF
; Author : Ward
; Dependency: BinaryCall.au3
; Website : http://www.json.org/index.html
;
; Source : jsmn.c
; Author : zserge
; Website : http://zserge.com/jsmn.html
;
; Source : json_string_encode.c, json_string_decode.c
; Author : Ward
; ============================================================================================================================
; ============================================================================================================================
; Public Functions:
; Json_StringEncode($String, $Option = 0)
; Json_StringDecode($String)
; Json_IsObject(ByRef $Object)
; Json_IsNull(ByRef $Null)
; Json_Encode($Data, $Option = 0, $Indent = Default, $ArraySep = Default, $ObjectSep = Default, $ColonSep = Default)
; Json_Decode($Json, $InitTokenCount = 1000)
; Json_ObjCreate()
; Json_ObjPut(ByRef $Object, $Key, $Value)
; Json_ObjGet(ByRef $Object, $Key)
; Json_ObjDelete(ByRef $Object, $Key)
; Json_ObjExists(ByRef $Object, $Key)
; Json_ObjGetCount(ByRef $Object)
; Json_ObjGetKeys(ByRef $Object)
; Json_ObjClear(ByRef $Object)
; Json_Put(ByRef $Var, $Notation, $Data, $CheckExists = False)
; Json_Get(ByRef $Var, $Notation)
; ============================================================================================================================
#Include-once
#Include "BinaryCall.au3"
; The following constants can be combined to form options for Json_Encode()
Global Const $JSON_UNESCAPED_UNICODE = 1 ; Encode multibyte Unicode characters literally
Global Const $JSON_UNESCAPED_SLASHES = 2 ; Don't escape /
Global Const $JSON_HEX_TAG = 4 ; All < and > are converted to \u003C and \u003E
Global Const $JSON_HEX_AMP = 8 ; All &s are converted to \u0026
Global Const $JSON_HEX_APOS = 16 ; All ' are converted to \u0027
Global Const $JSON_HEX_QUOT = 32 ; All " are converted to \u0022
Global Const $JSON_UNESCAPED_ASCII = 64 ; Don't escape ascii charcters between chr(1) ~ chr(0x1f)
Global Const $JSON_PRETTY_PRINT = 128 ; Use whitespace in returned data to format it
Global Const $JSON_STRICT_PRINT = 256 ; Make sure returned JSON string is RFC4627 compliant
Global Const $JSON_UNQUOTED_STRING = 512 ; Output unquoted string if possible (conflicting with $Json_STRICT_PRINT)
; Error value returnd by Json_Decode()
Global Const $JSMN_ERROR_NOMEM = -1 ; Not enough tokens were provided
Global Const $JSMN_ERROR_INVAL = -2 ; Invalid character inside JSON string
Global Const $JSMN_ERROR_PART = -3 ; The string is not a full JSON packet, more bytes expected
Func __Jsmn_RuntimeLoader($ProcName = "")
Static $SymbolList
If Not IsDllStruct($SymbolList) Then
Local $Code
If @AutoItX64 Then
$Code = 'AwAAAAQfCAAAAAAAAAA1HbEvgTNrvX54gCiWSTVmt5v7RCdoFJ/zhkKmwcm8yVqZPjJBoVhNHHAIzrHWKbZh1J0QAUaHB5zyQTilTmWa9O0OKeLrk/Jg+o7CmMzjEk74uPongdHv37nwYXvg97fiHvjP2bBzI9gxSkKq9Cqh/GxSHIlZPYyW76pXUt//25Aqs2Icfpyay/NFd50rW7eMliH5ynkrp16HM1afithVrO+LpSaz/IojowApmXnBHUncHliDqbkx6/AODUkyDm1hj+AiEZ9Me1Jy+hBQ1/wC/YnuuYSJvNAKp6XDnyc8Nwr54Uqx5SbUW2CezwQQ7aXX/HFiHSKpQcFW/gi8oSx5nsoxUXVjxeNI/L7z6GF2mfu3Tnpt7hliWEdA2r2VB+TIM7Pgwl9X3Ge0T3KJQUaRtLJZcPvVtOuKXr2Q9wy7hl80hVRrt9zYrbjBHXLrRx/HeIMkZwxhmKo/dD/vvaNgE+BdU8eeJqFBJK2alrK2rh2WkRynftyepm1WrdKrz/5KhQPp/4PqH+9IADDjoGBbfvJQXdT+yiO8DtfrVnd+JOEKsKEsdgeM3UXx5r6tEHO9rYWbzbnyEiX7WozZemry+vBZMMtHn1aA63+RcDQED73xOsnj00/9E5Z6hszM5Hi8vi6Hw3iOgf3cHwcXG44aau0JpuA2DlrUvnJOYkNnY+bECeSdAR1UQkFNyqRoH2xm4Y7gYMCPsFtPBlwwleEKI27SsUq1ZHVQvFCoef7DXgf/GwPCAvwDMIQfb3hJtIVubOkASRQZVNIJ/y4KPrn/gcASV7fvMjE34loltTVlyqprUWxpI51tN6vhTOLAp+CHseKxWaf9g1wdbVs0e/5xAiqgJbmKNi9OYbhV/blpp3SL63XKxGiHdxhK1aR+4rUY4e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nIcBsQ/3UyVnZk4kZP1KzyPCBLLIQNpCVgOLJzQuyaQ6k2QCBy0eJ0ppUyfp54LjwVg0X7bwncYbAomG4ZcFwTQnC2AX3oYG5n6Bz4SLLjxrFsY+v/SVa+GqH8uePBh1TPkHVNmzjXXymEf5jROlnd+EjfQdRyitkjPrg2HiQxxDcVhCh5J2L5+6CY9eIaYgrbd8zJnzAD8KnowHwh2bi4JLgmt7ktJ1XGizox7cWf3/Dod56KAcaIrSVw9XzYybdJCf0YRA6yrwPWXbwnzc/4+UDkmegi+AoCEMoue+cC7vnYVdmlbq/YLE/DWJX383oz2Ryq8anFrZ8jYvdoh8WI+dIugYL2SwRjmBoSwn56XIaot/QpMo3pYJIa4o8aZIZrjvB7BXO5aCDeMuZdUMT6AXGAGF1AeAWxFd2XIo1coR+OplMNDuYia8YAtnSTJ9JwGYWi2dJz3xrxsTQpBONf3yn8LVf8eH+o5eXc7lzCtHlDB+YyI8V9PyMsUPOeyvpB3rr9fDfNy263Zx33zTi5jldgP2OetUqGfbwl+0+zNYnrg64bluyIN/Awt1doDCQkCKpKXxuPaem/SyCHrKjg'
EndIf
Local $Symbol[] = ["jsmn_parse","jsmn_init","json_string_decode","json_string_encode"]
Local $CodeBase = _BinaryCall_Create($Code)
If @Error Then Exit MsgBox(16, "Json", "Startup Failure!")
$SymbolList = _BinaryCall_SymbolList($CodeBase, $Symbol)
If @Error Then Exit MsgBox(16, "Json", "Startup Failure!")
EndIf
If $ProcName Then Return DllStructGetData($SymbolList, $ProcName)
EndFunc
Func Json_StringEncode($String, $Option = 0)
Static $Json_StringEncode = __Jsmn_RuntimeLoader("json_string_encode")
Local $Length = StringLen($String) * 6 + 1
Local $Buffer = DllStructCreate("wchar[" & $Length & "]")
Local $Ret = DllCallAddress("int:cdecl", $Json_StringEncode, "wstr", $String, "ptr", DllStructGetPtr($Buffer), "uint", $Length, "int", $Option)
Return SetError($Ret[0], 0, DllStructGetData($Buffer, 1))
EndFunc
Func Json_StringDecode($String)
Static $Json_StringDecode = __Jsmn_RuntimeLoader("json_string_decode")
Local $Length = StringLen($String) + 1
Local $Buffer = DllStructCreate("wchar[" & $Length & "]")
Local $Ret = DllCallAddress("int:cdecl", $Json_StringDecode, "wstr", $String, "ptr", DllStructGetPtr($Buffer), "uint", $Length)
Return SetError($Ret[0], 0, DllStructGetData($Buffer, 1))
EndFunc
Func Json_Decode($Json, $InitTokenCount = 1000)
Static $Jsmn_Init = __Jsmn_RuntimeLoader("jsmn_init"), $Jsmn_Parse = __Jsmn_RuntimeLoader("jsmn_parse")
If $Json = "" Then $Json = '""'
Local $TokenList, $Ret
Local $Parser = DllStructCreate("uint pos;int toknext;int toksuper")
Do
DllCallAddress("none:cdecl", $Jsmn_Init, "ptr", DllStructGetPtr($Parser))
$TokenList = DllStructCreate("byte[" & ($InitTokenCount * 20) & "]")
$Ret = DllCallAddress("int:cdecl", $Jsmn_Parse, "ptr", DllStructGetPtr($Parser), "wstr", $Json, "ptr", DllStructGetPtr($TokenList), "uint", $InitTokenCount)
$InitTokenCount *= 2
Until $Ret[0] <> $JSMN_ERROR_NOMEM
Local $Next = 0
Return SetError($Ret[0], 0, _Json_Token($Json, DllStructGetPtr($TokenList), $Next))
EndFunc
Func _Json_Token(ByRef $Json, $Ptr, ByRef $Next)
If $Next = -1 Then Return Null
Local $Token = DllStructCreate("int;int;int;int", $Ptr + ($Next * 20))
Local $Type = DllStructGetData($Token, 1)
Local $Start = DllStructGetData($Token, 2)
Local $End = DllStructGetData($Token, 3)
Local $Size = DllStructGetData($Token, 4)
$Next += 1
If $Type = 0 And $Start = 0 And $End = 0 And $Size = 0 Then ; Null Item
$Next = -1
Return Null
EndIf
Switch $Type
Case 0 ; Json_PRIMITIVE
Local $Primitive = StringMid($Json, $Start + 1, $End - $Start)
Switch $Primitive
Case "true"
Return True
Case "false"
Return False
Case "null"
Return Null
Case Else
If StringRegExp($Primitive, "^[+\-0-9]") Then
Return Number($Primitive)
Else
Return Json_StringDecode($Primitive)
EndIf
EndSwitch
Case 1 ; Json_OBJECT
Local $Object = Json_ObjCreate()
For $i = 0 To $Size - 1 Step 2
Local $Key = _Json_Token($Json, $Ptr, $Next)
Local $Value = _Json_Token($Json, $Ptr, $Next)
If Not IsString($Key) Then $Key = Json_Encode($Key)
If $Object.Exists($Key) Then $Object.Remove($Key)
$Object.Add($Key, $Value)
Next
Return $Object
Case 2 ; Json_ARRAY
Local $Array[$Size]
For $i = 0 To $Size - 1
$Array[$i] = _Json_Token($Json, $Ptr, $Next)
Next
Return $Array
Case 3 ; Json_STRING
Return Json_StringDecode(StringMid($Json, $Start + 1, $End - $Start))
EndSwitch
EndFunc
Func Json_IsObject(ByRef $Object)
Return (IsObj($Object) And ObjName($Object) = "Dictionary")
EndFunc
Func Json_IsNull(ByRef $Null)
Return IsKeyword($Null) Or (Not IsObj($Null) And VarGetType($Null) = "Object")
EndFunc
Func Json_Encode_Compact($Data, $Option = 0)
Select
Case IsString($Data)
Return '"' & Json_StringEncode($Data, $Option) & '"'
Case IsNumber($Data)
Return $Data
Case IsArray($Data) And UBound($Data, 0) = 1
Local $Json = "["
For $i = 0 To UBound($Data) - 1
$Json &= Json_Encode_Compact($Data[$i], $Option) & ","
Next
If StringRight($Json, 1) = "," Then $Json = StringTrimRight($Json, 1)
Return $Json & "]"
Case Json_IsObject($Data)
Local $Json = "{"
Local $Keys = $Data.Keys()
For $i = 0 To UBound($Keys) - 1
$Json &= '"' & Json_StringEncode($Keys[$i], $Option) & '":' & Json_Encode_Compact($Data.Item($Keys[$i]), $Option) & ","
Next
If StringRight($Json, 1) = "," Then $Json = StringTrimRight($Json, 1)
Return $Json & "}"
Case IsBool($Data)
Return StringLower($Data)
Case IsPtr($Data)
Return Number($Data)
Case IsBinary($Data)
Return '"' & Json_StringEncode(BinaryToString($Data, 4), $Option) & '"'
Case Else ; Keyword, DllStruct, Object
Return "null"
EndSelect
EndFunc
Func Json_Encode_Pretty($Data, $Option, $Indent, $ArraySep, $ObjectSep, $ColonSep, $ArrayCRLF = Default, $ObjectCRLF = Default, $NextIdent = "")
Local $ThisIdent = $NextIdent, $Json = ""
Select
Case IsString($Data)
Local $String = Json_StringEncode($Data, $Option)
If BitAND($Option, $Json_UNQUOTED_STRING) And Not BitAND($Option, $Json_STRICT_PRINT) And Not StringRegExp($String, "[\s,:]") And Not StringRegExp($String, "^[+\-0-9]") Then
Return $String
Else
Return '"' & $String & '"'
EndIf
Case IsArray($Data) And UBound($Data, 0) = 1
If UBound($Data) = 0 Then Return "[]"
If IsKeyword($ArrayCRLF) Then
$ArrayCRLF = ""
Local $Match = StringRegExp($ArraySep, "[\r\n]+$", 3)
If IsArray($Match) Then $ArrayCRLF = $Match[0]
EndIf
If $ArrayCRLF Then $NextIdent &= $Indent
Local $Length = UBound($Data) - 1
For $i = 0 To $Length
If $ArrayCRLF Then $Json &= $NextIdent
$Json &= Json_Encode_Pretty($Data[$i], $Option, $Indent, $ArraySep, $ObjectSep, $ColonSep, $ArrayCRLF, $ObjectCRLF, $NextIdent)
If $i < $Length Then $Json &= $ArraySep
Next
If $ArrayCRLF Then Return "[" & $ArrayCRLF & $Json & $ArrayCRLF & $ThisIdent & "]"
Return "[" & $Json & "]"
Case Json_IsObject($Data)
If $Data.Count = 0 Then Return "{}"
If IsKeyword($ObjectCRLF) Then
$ObjectCRLF = ""
Local $Match = StringRegExp($ObjectSep, "[\r\n]+$", 3)
If IsArray($Match) Then $ObjectCRLF = $Match[0]
EndIf
If $ObjectCRLF Then $NextIdent &= $Indent
Local $Keys = $Data.Keys()
Local $Length = UBound($Keys) - 1
For $i = 0 To $Length
If $ObjectCRLF Then $Json &= $NextIdent
$Json &= Json_Encode_Pretty(String($Keys[$i]), $Option, $Indent, $ArraySep, $ObjectSep, $ColonSep) & $ColonSep _
& Json_Encode_Pretty($Data.Item($Keys[$i]), $Option, $Indent, $ArraySep, $ObjectSep, $ColonSep, $ArrayCRLF, $ObjectCRLF, $NextIdent)
If $i < $Length Then $Json &= $ObjectSep
Next
If $ObjectCRLF Then Return "{" & $ObjectCRLF & $Json & $ObjectCRLF & $ThisIdent & "}"
Return "{" & $Json & "}"
Case Else
Return Json_Encode_Compact($Data, $Option)
EndSelect
EndFunc
Func Json_Encode($Data, $Option = 0, $Indent = Default, $ArraySep = Default, $ObjectSep = Default, $ColonSep = Default)
If BitAND($Option, $Json_PRETTY_PRINT) Then
Local $Strict = BitAND($Option, $Json_STRICT_PRINT)
If IsKeyword($Indent) Then
$Indent = @Tab
Else
$Indent = Json_StringDecode($Indent)
If StringRegExp($Indent, "[^\t ]") Then $Indent = @Tab
EndIf
If IsKeyword($ArraySep) Then
$ArraySep = "," & @CRLF
Else
$ArraySep = Json_StringDecode($ArraySep)
If $ArraySep = "" Or StringRegExp($ArraySep, "[^\s,]|,.*,") Or ($Strict And Not StringRegExp($ArraySep, ",")) Then $ArraySep = "," & @CRLF
EndIf
If IsKeyword($ObjectSep) Then
$ObjectSep = "," & @CRLF
Else
$ObjectSep = Json_StringDecode($ObjectSep)
If $ObjectSep = "" Or StringRegExp($ObjectSep, "[^\s,]|,.*,") Or ($Strict And Not StringRegExp($ObjectSep, ",")) Then $ObjectSep = "," & @CRLF
EndIf
If IsKeyword($ColonSep) Then
$ColonSep = ": "
Else
$ColonSep = Json_StringDecode($ColonSep)
If $ColonSep = "" Or StringRegExp($ColonSep, "[^\s,:]|[,:].*[,:]") Or ($Strict And (StringRegExp($ColonSep, ",") Or Not StringRegExp($ColonSep, ":"))) Then $ColonSep = ": "
EndIf
Return Json_Encode_Pretty($Data, $Option, $Indent, $ArraySep, $ObjectSep, $ColonSep)
ElseIf BitAND($Option, $Json_UNQUOTED_STRING) Then
Return Json_Encode_Pretty($Data, $Option, "", ",", ",", ":")
Else
Return Json_Encode_Compact($Data, $Option)
EndIf
EndFunc
Func Json_ObjCreate()
Local $Object = ObjCreate('Scripting.Dictionary')
$Object.CompareMode = 0
Return $Object
EndFunc
Func Json_ObjPut(ByRef $Object, $Key, $Value)
$Key = String($Key)
If $Object.Exists($Key) Then $Object.Remove($Key)
$Object.Add($Key, $Value)
EndFunc
Func Json_ObjGet(ByRef $Object, $Key)
$Key = String($Key)
If $Object.Exists($Key) Then Return $Object.Item($Key)
Return SetError(1, 0, '')
EndFunc
Func Json_ObjDelete(ByRef $Object, $Key)
$Key = String($Key)
If $Object.Exists($Key) Then $Object.Remove($Key)
EndFunc
Func Json_ObjExists(ByRef $Object, $Key)
Return $Object.Exists(String($Key))
EndFunc
Func Json_ObjGetCount(ByRef $Object)
Return $Object.Count
EndFunc
Func Json_ObjGetKeys(ByRef $Object)
Return $Object.Keys()
EndFunc
Func Json_ObjClear(ByRef $Object)
Return $Object.RemoveAll()
EndFunc
; Both dot notation and square bracket notation can be supported
Func Json_Put(ByRef $Var, $Notation, $Data, $CheckExists = False)
Local $Match = StringRegExp($Notation, "(^\[([^\]]+)\])|(^\.([^\.\[]+))", 3)
If IsArray($Match) Then
Local $Index
If UBound($Match) = 4 Then
$Index = String(Json_Decode($Match[3])) ; only string using dot notation
$Notation = StringTrimLeft($Notation, StringLen($Match[2]))
Else
$Index = Json_Decode($Match[1])
$Notation = StringTrimLeft($Notation, StringLen($Match[0]))
EndIf
If IsString($Index) Then
If $CheckExists And (Not Json_IsObject($Var) Or Not Json_ObjExists($Var, $Index)) Then
Return SetError(1, 0, False) ; no specific object
EndIf
If Not Json_IsObject($Var) Then $Var = Json_ObjCreate()
If $Notation Then
Local $Item = Json_ObjGet($Var, $Index)
Local $Ret = Json_Put($Item, $Notation, $Data, $CheckExists)
Local $Error = @Error
If Not $Error Then Json_ObjPut($Var, $Index, $Item)
Return SetError($Error, 0, $Ret)
Else
Json_ObjPut($Var, $Index, $Data)
Return True
EndIf
ElseIf IsInt($Index) Then
If $Index < 0 Or ($CheckExists And (Not IsArray($Var) Or UBound($Var, 0) <> 1 Or $Index >= UBound($Var))) Then
Return SetError(1, 0, False) ; no specific object
EndIf
If Not IsArray($Var) Or UBound($Var, 0) <> 1 Then
Dim $Var[$Index + 1]
ElseIf $Index >= UBound($Var) Then
ReDim $Var[$Index + 1]
EndIf
If $Notation Then
Local $Ret = Json_Put($Var[$Index], $Notation, $Data, $CheckExists)
Return SetError(@Error, 0, $Ret)
Else
$Var[$Index] = $Data
Return True
EndIf
EndIf
EndIf
Return SetError(2, 0, False) ; invalid notation
EndFunc
; Both dot notation and square bracket notation can be supported
Func Json_Get(ByRef $Var, $Notation)
Local $Match = StringRegExp($Notation, "(^\[([^\]]+)\])|(^\.([^\.\[]+))", 3)
If IsArray($Match) Then
Local $Index
If UBound($Match) = 4 Then
$Index = String(Json_Decode($Match[3])) ; only string using dot notation
$Notation = StringTrimLeft($Notation, StringLen($Match[2]))
Else
$Index = Json_Decode($Match[1])
$Notation = StringTrimLeft($Notation, StringLen($Match[0]))
EndIf
Local $Item
If IsString($Index) And Json_IsObject($Var) And Json_ObjExists($Var, $Index) Then
$Item = Json_ObjGet($Var, $Index)
ElseIf IsInt($Index) And IsArray($Var) And UBound($Var, 0) = 1 And $Index >= 0 And $Index < UBound($Var) Then
$Item = $Var[$Index]
Else
Return SetError(1, 0, "") ; no specific object
EndIf
If Not $Notation Then Return $Item
Local $Ret = Json_Get($Item, $Notation)
Return SetError(@Error, 0, $Ret)
EndIf
Return SetError(2, 0, "") ; invalid notation
EndFunc