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