forked from G33kDude/AutoHotkey-JSON
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexport.ahk
344 lines (301 loc) · 10.1 KB
/
export.ahk
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
class JSON
{
class parse extends JSON.functor
{
call(self, ByRef param_string, param_reviver:="")
{
this.rev := isObject(param_reviver) ? param_reviver : false
; Object keys(and array indices) are temporarily stored in arrays so that
; we can enumerate them in the order they appear in the string instead
; of alphabetically. Skip if no reviver function is specified.
this.keys := this.rev ? {} : false
static quot := chr(34), bashq := "\" quot
, json_value := quot "{[01234567890-tfn"
, json_value_or_array_closing := quot "{[]01234567890-tfn"
, object_key_or_object_closing := quot "}"
key := ""
is_key := false
root := {}
stack := [root]
next := json_value
pos := 0
while ((ch := subStr(param_string, ++pos, 1)) != "") {
if inStr(" `t`r`n", ch) {
continue
}
if !inStr(next, ch, 1) {
this.parseError(next, param_string, pos)
}
holder := stack[1]
is_array := holder.IsArray
if inStr(",:", ch) {
next := (is_key := !is_array && ch == ",") ? quot : json_value
} else if (inStr("}]", ch)) {
objRemoveAt(stack, 1)
next := stack[1]==root ? "" : stack[1].IsArray ? ",]" : ",}"
} else {
if (inStr("{[", ch)) {
; Check if Array() is overridden and if its return value has
; the 'IsArray' property. If so, Array() will be called normally,
; otherwise, use a custom base object for arrays
static json_array := func("Array").isBuiltIn || ![].IsArray ? {IsArray: true} : 0
; sacrifice readability for minor(actually negligible) performance gain
(ch == "{")
? ( is_key := true
, value := {}
, next := object_key_or_object_closing )
; ch == "["
: ( value := json_array ? new json_array : []
, next := json_value_or_array_closing )
ObjInsertAt(stack, 1, value)
if (this.keys) {
this.keys[value] := []
}
} else {
if (ch == quot) {
i := pos
while (i := inStr(param_string, quot,, i+1)) {
value := strReplace(subStr(param_string, pos+1, i-pos-1), "\\", "\u005c")
static tail := A_AhkVersion<"2" ? 0 : -1
if (subStr(value, tail) != "\") {
break
}
}
if (!i) {
this.parseError("'", param_string, pos)
}
value := strReplace(value, "\/", "/")
, value := strReplace(value, bashq, quot)
, value := strReplace(value, "\b", "`b")
, value := strReplace(value, "\f", "`f")
, value := strReplace(value, "\n", "`n")
, value := strReplace(value, "\r", "`r")
, value := strReplace(value, "\t", "`t")
pos := i ; update pos
i := 0
while (i := inStr(value, "\",, i+1)) {
if (!(subStr(value, i+1, 1) == "u")) {
this.parseError("\", param_string, pos - strLen(subStr(value, i+1)))
}
uffff := Abs("0x" subStr(value, i+2, 4))
if (A_IsUnicode || uffff < 0x100) {
value := subStr(value, 1, i-1) chr(uffff) subStr(value, i+6)
}
}
if (is_key) {
key := value, next := ":"
continue
}
} else {
value := subStr(param_string, pos, i := regExMatch(param_string, "[\]\},\s]|$",, pos)-pos)
if value is number
{
if value is integer
{
value += 0
}
}
else if (value == "true" || value == "false") {
value := %value% + 0
} else if (value == "null") {
value := ""
} else {
; we can do more here to pinpoint the actual culprit
; but that's just too much extra work.
this.parseError(next, text, pos, i)
}
pos += i - 1
}
next := holder == root ? "" : is_array ? ",]" : ",}"
} ; If inStr("{[", ch) { ... } else
is_array? key := objPush(holder, value) : holder[key] := value
if (this.keys && this.keys.hasKey(holder)) {
this.keys[holder].Push(key)
}
}
} ; while ( ... )
return this.rev ? this.walk(root, "") : root[""]
}
parseError(param_expect, ByRef param_string, pos, param_length:=1)
{
static quot := chr(34), qurly := quot "}"
line := strSplit(subStr(param_string, 1, pos), "`n", "`r").length()
col := pos - inStr(param_string, "`n",, -(strLen(param_string)-pos+1))
msg := format("{1}`n`nLine:`t{2}`nCol:`t{3}`nChar:`t{4}"
, (param_expect == "") ? "Extra data"
: (param_expect == "'") ? "Unterminated string starting at"
: (param_expect == "\") ? "Invalid \escape"
: (param_expect == ":") ? "Expecting ':' delimiter"
: (param_expect == quot) ? "Expecting object key enclosed in double quotes"
: (param_expect == qurly) ? "Expecting object key enclosed in double quotes or object closing '}'"
: (param_expect == ",}") ? "Expecting ',' delimiter or object closing '}'"
: (param_expect == ",]") ? "Expecting ',' delimiter or array closing ']'"
: inStr(param_expect, "]") ? "Expecting JSON value or array closing ']'"
: "Expecting JSON value(string, number, true, false, null, object or array)"
, line, col, pos)
static offset := A_AhkVersion < "2" ? -3 : -4
throw Exception(msg, offset, subStr(param_string, pos, param_length))
}
walk(param_holder, param_key)
{
value := param_holder[param_key]
if (isObject(value)) {
for i, k in this.keys[value] {
; check if objhasKey(value, k) ??
v := this.walk(value, k)
if (v != JSON.Undefined) {
value[k] := v
} else {
objDelete(value, k)
}
}
}
return this.rev.call(param_holder, param_key, value)
}
}
class stringify extends JSON.functor
{
call(self, param_value, param_replacer:="", space:="")
{
this.rep := isObject(param_replacer) ? param_replacer : ""
this.gap := ""
if (space) {
if space is integer
{
loop, % ((n := Abs(space))>10 ? 10 : n) {
this.gap .= " "
}
} else {
this.gap := subStr(space, 1, 10)
}
this.indent := "`n"
}
return this.str({"": param_value}, "")
}
str(param_holder, param_key)
{
param_value := param_holder[param_key]
if (this.rep) {
param_value := this.rep.call(param_holder, param_key, objhasKey(param_holder, param_key) ? param_value : JSON.Undefined)
}
if isObject(param_value) {
; Check object type, skip serialization for other object types such as
; ComObject, Func, BoundFunc, FileObject, RegExMatchObject, Property, etc.
static type := A_AhkVersion<"2" ? "" : func("Type")
if (type ? type.call(param_value) == "Object" : objGetCapacity(param_value) != "") {
if (this.gap) {
stepback := this.indent
this.indent .= this.gap
}
is_array := param_value.IsArray
; Array() is not overridden, rollback to old method of
; identifying array-like objects. Due to the use of a for-loop
; sparse arrays such as '[1,,3]' are detected as objects({}).
if (!is_array) {
for i in param_value {
is_array := i == A_Index
}
until (!is_array)
}
str := ""
if (is_array) {
loop, % param_value.length() {
if (this.gap) {
str .= this.indent
}
v := this.str(param_value, A_Index)
str .= (v != "") ? v "," : "null,"
}
} else {
colon := this.gap ? ": " : ":"
for k in param_value {
v := this.str(param_value, k)
if (v != "") {
if (this.gap) {
str .= this.indent
}
str .= this.quote(k) colon v ","
}
}
}
if (str != "") {
str := rTrim(str, ",")
if (this.gap) {
str .= stepback
}
}
if (this.gap) {
this.indent := stepback
}
return is_array ? "[" str "]" : "{" str "}"
}
} else {
; is_number ? param_value : "param_value"
return objGetCapacity([param_value], 1) == "" ? param_value : this.quote(param_value)
}
}
quote(param_string)
{
static quot := chr(34), bashq := "\" quot
if (param_string != "") {
param_string := strReplace(param_string, "\", "\\")
; , param_string := strReplace(param_string, "/", "\/") ; optional in ECMAScript
, param_string := strReplace(param_string, quot, bashq)
, param_string := strReplace(param_string, "`b", "\b")
, param_string := strReplace(param_string, "`f", "\f")
, param_string := strReplace(param_string, "`n", "\n")
, param_string := strReplace(param_string, "`r", "\r")
, param_string := strReplace(param_string, "`t", "\t")
static rx_escapable := A_AhkVersion<"2" ? "O)[^\x20-\x7e]" : "[^\x20-\x7e]"
while regExMatch(param_string, rx_escapable, m) {
param_string := strReplace(param_string, m.Value, format("\u{1:04x}", ord(m.Value)))
}
}
return quot param_string quot
}
}
class test extends JSON.functor
{
call(self, param_string:="")
{
if (isObject(param_string) || param_string == "") {
return false
}
try {
JSON.parse(param_string)
} catch error {
return false
}
return true
}
}
; For use with reviver and replacer functions since AutoHotkey does not
; have an 'undefined' type. Returning blank("") or 0 won't work since these
; can't be distnguished from actual JSON values. This leaves us with objects.
; Replacer() - the caller may return a non-serializable AHK objects such as
; ComObject, Func, BoundFunc, FileObject, RegExMatchObject, and Property to
; mimic the behavior of returning 'undefined' in JavaScript but for the sake
; of code readability and convenience, it's better to do 'return JSON.Undefined'.
; Internally, the property returns a ComObject with the variant type of VT_EMPTY.
Undefined[]
{
get {
static empty := {}, vt_empty := ComObject(0, &empty, 1)
return vt_empty
}
}
class functor
{
__call(param_method, ByRef param_args, param_extargs*)
{
; When casting to call(), use a new instance of the "function object"
; so as to avoid directly storing the properties(used across sub-methods)
; into the "function object" itself.
if isObject(param_method) {
return (new this).call(param_method, param_args, param_extargs*)
} else if (param_method == "") {
return (new this).call(param_args, param_extargs*)
}
}
}
}