forked from Paris/AutoHotkey-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
json.ahk
53 lines (50 loc) · 1.61 KB
/
json.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
/*
Function: JSON
Parameters:
js - source
s - path to element
v - (optional) value to overwrite
Returns:
Value of element (prior to change).
License:
- Version 2.0 <http://www.autohotkey.net/~polyethene/#json>
- Dedicated to the public domain (CC0 1.0) <http://creativecommons.org/publicdomain/zero/1.0/>
*/
json(ByRef js, s, v = "") {
j = %js%
Loop, Parse, s, .
{
p = 2
RegExMatch(A_LoopField, "([+\-]?)([^[]+)((?:\[\d+\])*)", q)
Loop {
If (!p := RegExMatch(j, "(?<!\\)(""|')([^\1]+?)(?<!\\)(?-1)\s*:\s*((\{(?:[^{}]++|(?-1))*\})|(\[(?:[^[\]]++|(?-1))*\])|"
. "(?<!\\)(""|')[^\7]*?(?<!\\)(?-1)|[+\-]?\d+(?:\.\d*)?|true|false|null?)\s*(?:,|$|\})", x, p))
Return
Else If (x2 == q2 or q2 == "*") {
j = %x3%
z += p + StrLen(x2) - 2
If (q3 != "" and InStr(j, "[") == 1) {
StringTrimRight, q3, q3, 1
Loop, Parse, q3, ], [
{
z += 1 + RegExMatch(SubStr(j, 2, -1), "^(?:\s*((\[(?:[^[\]]++|(?-1))*\])|(\{(?:[^{\}]++|(?-1))*\})|[^,]*?)\s*(?:,|$)){" . SubStr(A_LoopField, 1) + 1 . "}", x)
j = %x1%
}
}
Break
}
Else p += StrLen(x)
}
}
If v !=
{
vs = "
If (RegExMatch(v, "^\s*(?:""|')*\s*([+\-]?\d+(?:\.\d*)?|true|false|null?)\s*(?:""|')*\s*$", vx)
and (vx1 + 0 or vx1 == 0 or vx1 == "true" or vx1 == "false" or vx1 == "null" or vx1 == "nul"))
vs := "", v := vx1
StringReplace, v, v, ", \", All
js := SubStr(js, 1, z := RegExMatch(js, ":\s*", zx, z) + StrLen(zx) - 1) . vs . v . vs . SubStr(js, z + StrLen(x3) + 1)
}
Return, j == "false" ? 0 : j == "true" ? 1 : j == "null" or j == "nul"
? "" : SubStr(j, 1, 1) == """" ? SubStr(j, 2, -1) : j
}