/ahk/json.ahk
http://soulbound360.googlecode.com/ · AutoHotKey · 53 lines · 38 code · 0 blank · 15 comment · 14 complexity · a1e4cabb418107e537eed98e750ff26f MD5 · raw file
- /*
- 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/~Titan/#json>
- - Simplified BSD License <http://www.autohotkey.net/~Titan/license.txt>
- */
- 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
- }