[v2] so() - 字符对象互转

许多实用脚本和封装函数, 可以让您编写脚本更加便捷高效

Moderators: arcticir, tmplinshi

arcticir
Posts: 639
Joined: 17 Nov 2013, 11:32

[v2] so() - 字符对象互转

17 Oct 2014, 00:10

兼容ahk和json语法。

ahk的对象语法,可以引用变量:

Code: Select all

obj:=so("{(A_ScriptDir): A_ScriptDir,A_ScriptName:A_ScriptName,A_TickCount:A_TickCount,  Test  :  [A_AhkPath,`"Test`",A_AhkVersion]}")
对象转字符串二者没区别,处理 json 需要指定第二个参数为0:

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%)
}
Last edited by arcticir on 16 Jul 2016, 03:35, edited 14 times in total.
User avatar
RobertL
Posts: 540
Joined: 18 Jan 2014, 01:14
Location: China

Re: [lib] 字符串创建对象 - so() 预览

17 Oct 2014, 02:09

还是预览版,看着有点庞大,慢慢精简吧..感觉字符串正则操作用得很好了。
我为人人,人人为己?
cixiplc
Posts: 8
Joined: 18 Oct 2014, 15:52

Re: [lib] 字符串创建对象 - so()

03 Apr 2015, 05:35

最近对json有关注 正需要 谢谢

Return to “脚本函数”

Who is online

Users browsing this forum: No registered users and 2 guests