ahk的对象语法,可以引用变量:
Code: Select all
obj:=so("{(A_ScriptDir): A_ScriptDir,A_ScriptName:A_ScriptName,A_TickCount:A_TickCount, Test : [A_AhkPath,`"Test`",A_AhkVersion]}")
Code: Select all
obj:=so(str)
obj:=so(str,0)
Code: Select all
so(s,n:=""){
static json,jx,d,y:="`"",j:="`" `t",m:={"\b":Chr(08),"\\":"\","\t":"`t","\n":"`n","\f":Chr(12),"\r":"`r"},gu:="āЁξ"
if Isobject(s)
{
if !InStr(Type(s),"Object")
Return
if !Count:=s.Count()
Return "[]"
if w:= (Count = s.Length())
for k in s
if (k != A_Index)
{
w:=""
Break
}
if w
{
for i,n in s
str.=(Type(n)="Object"?so(n,1): n+0=""?(gu n gu):n) ","
}
else
{
for i,n in s
str.=(Type(i)="Object"?so(i,1):i+0=""?(gu i gu):i) ":" (Type(n)="Object"?so(n,1): n+0=""?(gu n gu):n) ","
}
if !n
{
StrReplace, str, %str%,/,\/
for c,z in m
IF InStr(str,z)
StrReplace, str, %str%,% z,% c
StrReplace, str, %str%,% y,% "\" y
StrReplace, str, %str%,% gu,% y
}
Return str:=RTrim(str,","),w?"[" str "]":"{" str "}"
}
f:=[],i:=1
if n
b:=StrLen(s)
else
{
if !(s:=Trim(s," `n`r`t")) or !regexmatch(s, "[\[\{]")
Return s
if json:=(n=0) ; so(s,0) json
{
for c,z in m
if InStr(s,c)
StrReplace,s,% s,% c,% z
StrReplace, s, %s%,\/,/
if e:=InStr(s,"\u")
for e,n in StrSplit(SubStr(s, e+2), "\u")
IF n and !f[b:=SubStr(n, 1, 4)]
{
IF d := Abs("0x" b)
StrReplace,s,% s,% "\u" b,% Chr(d)
f[b]:=1
}
}
f:=[],b:=StrLen(s),n:=SubStr(s,i,1),d:=0,jx:=json?"\":"``"
}
if (n="{")
loop
{
if ((r?r[0]:"") = "}") or !i or !( i:=RegExMatch(s,"\S",n,i+1) ) or ((n:=n[0]) = "}")
Return d:=i,f
if InStr("[{",n)
(k:=so(SubStr(s,i),n),i+=d,i:=RegExMatch(s,"\S",t,InStr(s,":",,i)+1),(InStr("[{",t:=i?t[0]:"")
? (f[so_json(K,json) ""]:=so(SubStr(s,i),t),i:=RegExMatch(s,",|\}",r,i+d))
: ( ((t=y) ? (p:=InStr(s,y,,i+1),p:=RegExMatch(s,",|\}",r,p),z:=Trim(SubStr(s,i+1,p-i-2)))
: (p:=RegExMatch(s,",|\}",r,i),z:=Trim(SubStr(s,i,p-i)),z:=z+0=""?so_Try(z):z+0))
,f[so_json(K,json) ""]:=so_json(K,json),i:=p)) )
else
(x:=InStr(s,":",,(n=y)?InStr(s,y,,i,2):i))
? (k:= ((n=y)?SubStr(s,i+1,x-i-2):SubStr(s,i,x-i))
,k:=(n=y ? Trim(k) : n="(" ? so_Try(Trim(k,"() `t")):Trim(k)),i:=RegExMatch(s,"\S",t,x+1)
,InStr("[{",t:=i?t[0]:"")
? (f[so_json(K,json) ""]:=so(SubStr(s,i),t),i:=RegExMatch(s,",|\}",r,i+d))
: ( ((t=y) ? (p:=RegExMatch(s,",|\}",r,so_InStr(s,i,jx)),z:=Trim(SubStr(s,i+1,InStr(s,y,,p-b-1)-i-1)))
: (p:=RegExMatch(s,",|\}",r,i),z:=Trim(SubStr(s,i,p-i)),z:=z+0=""?so_Try(z):z+0))
,f[so_json(K,json) ""]:=so_json(Z,json),i:=p))
: i:=0
}
if (n = "[")
loop
{
if ((r?r[0]:"") = "]") or !i or !( i:=RegExMatch(s,"\S",n,i+1) ) or ((n:=n[0]) = "]")
Return d:=i,f
(InStr("[{",n)
? (f.Push(so(SubStr(s,i),n)),i:=RegExMatch(s,",|\]",r,i+d))
: ( (n=y) ? (p:=RegExMatch(s,",|\]",r,so_InStr(s,i,jx)),z:=Trim(SubStr(s,i+1,InStr(s,y,,p-b-1)-i-1)))
: (p:=RegExMatch(s,",|\]",r,i),z:=Trim(SubStr(s,i,p-i)),z:=z+0=""?so_Try(z):z+0),i:=p
,f.Push(so_json(Z,json))))
}
}
so_json(s,json){
static J:="\`"",P:="`""
Return json AND InStr(s,J)?StrReplace(S,J,P):S
}
so_InStr(s,i,jx){
while (p:=InStr(s,"`"",,i+1)) and (SubStr(s,p-1,1)=jx)
i:=p
Return p
}
so_Try(f){
global
Try
Return (%f%)
}