PageRenderTime 14ms CodeModel.GetById 8ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 1ms

/ahk/json.ahk

http://soulbound360.googlecode.com/
AutoHotKey | 53 lines | 38 code | 0 blank | 15 comment | 14 complexity | a1e4cabb418107e537eed98e750ff26f MD5 | raw file
 1/*
 2	Function: JSON
 3
 4	Parameters:
 5		js - source
 6		s - path to element
 7		v - (optional) value to overwrite
 8
 9	Returns:
10		Value of element (prior to change).
11
12	License:
13		- Version 2.0 <http://www.autohotkey.net/~Titan/#json>
14		- Simplified BSD License <http://www.autohotkey.net/~Titan/license.txt>
15*/
16json(ByRef js, s, v = "") {
17	j = %js%
18	Loop, Parse, s, .
19	{
20		p = 2
21		RegExMatch(A_LoopField, "([+\-]?)([^[]+)((?:\[\d+\])*)", q)
22		Loop {
23			If (!p := RegExMatch(j, "(?<!\\)(""|')([^\1]+?)(?<!\\)(?-1)\s*:\s*((\{(?:[^{}]++|(?-1))*\})|(\[(?:[^[\]]++|(?-1))*\])|"
24				. "(?<!\\)(""|')[^\7]*?(?<!\\)(?-1)|[+\-]?\d+(?:\.\d*)?|true|false|null?)\s*(?:,|$|\})", x, p))
25				Return
26			Else If (x2 == q2 or q2 == "*") {
27				j = %x3%
28				z += p + StrLen(x2) - 2
29				If (q3 != "" and InStr(j, "[") == 1) {
30					StringTrimRight, q3, q3, 1
31					Loop, Parse, q3, ], [
32					{
33						z += 1 + RegExMatch(SubStr(j, 2, -1), "^(?:\s*((\[(?:[^[\]]++|(?-1))*\])|(\{(?:[^{\}]++|(?-1))*\})|[^,]*?)\s*(?:,|$)){" . SubStr(A_LoopField, 1) + 1 . "}", x)
34						j = %x1%
35					}
36				}
37				Break
38			}
39			Else p += StrLen(x)
40		}
41	}
42	If v !=
43	{
44		vs = "
45		If (RegExMatch(v, "^\s*(?:""|')*\s*([+\-]?\d+(?:\.\d*)?|true|false|null?)\s*(?:""|')*\s*$", vx)
46			and (vx1 + 0 or vx1 == 0 or vx1 == "true" or vx1 == "false" or vx1 == "null" or vx1 == "nul"))
47			vs := "", v := vx1
48		StringReplace, v, v, ", \", All
49		js := SubStr(js, 1, z := RegExMatch(js, ":\s*", zx, z) + StrLen(zx) - 1) . vs . v . vs . SubStr(js, z + StrLen(x3) + 1)
50	}
51	Return, j == "false" ? 0 : j == "true" ? 1 : j == "null" or j == "nul"
52		? "" : SubStr(j, 1, 1) == """" ? SubStr(j, 2, -1) : j
53}