Page 1 of 1

Updating the JsonToAHK function to AHK v2

Posted: 30 Apr 2024, 13:46
by Loop
Hello everyone and @teadrinker ,

Could someone update the teadrinker's function to AHK v2? I tried, but I failed. Thank you in advance.
;Here is my poor attempt:

Code: Select all

#Requires Autohotkey v2.0
#SingleInstance Force

JsonExam := '
(
{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}

)'

JsonObj := JSON2AHK(JsonExam)
MgBox JsonObj["id"]
JSON2AHK(JSON, Recursive := False) {
    Global	
    Static Doc := ComObject('htmlfile')
        , __  := Doc.write('<meta http-equiv="X-UA-Compatible" content="IE=9">')
        , JS  := Doc.parentWindow

   If (!Recursive)
      Obj := JSON2AHK(JS.eval('(' . JSON . ')'), True)
   Else If !IsObject(JSON)
      Obj := JSON
   Else If (JSON.toString() != '[object Object]') {
      Obj := []

      Loop JSON.Length
         Obj.Push(JSON2AHK(JSON[A_Index - 1], True) )
   }
   Else {
      Obj := {}
      Keys := JS.Object.keys(JSON)
      Loop ObjOwnPropCount(Obj) {
         K := Keys[A_Index - 1]
         Obj[K] := JSON2AHK(JSON[K], True)
      }
   }
   Return Obj
}



And here is the original in AHK v1:

Code: Select all

JsonToAHK(json, rec := false) {
   static doc := ComObjCreate("htmlfile")
         , __ := doc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
         , JS := doc.parentWindow
   if !rec
      obj := JsonToAHK(JS.eval("(" . json . ")"), true)
   else if !IsObject(json)
      obj := json
   else if json.toString() != "[object Object]" {
      obj := []
      Loop % json.length
         obj.Push( JsonToAHK(json[A_Index - 1], true) )
   }
   else {
      obj := {}
      keys := JS.Object.keys(json)
      Loop % keys.length {
         k := keys[A_Index - 1]
         obj[k] := JsonToAHK(json[k], true)
      }
   }
   Return obj
}

Re: Updating the JsonToAHK function to AHK v2  Topic is solved

Posted: 30 Apr 2024, 19:47
by teadrinker
Hello

Code: Select all

#Requires Autohotkey v2.0

JsonExam := '
(
{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}

)'

obj := JsonToAhk(JsonExam)
MsgBox obj['menu']['id']
obj := JsonToAhk(JsonExam, false)
MsgBox obj.menu.id

JsonToAhk(json, objIsMap := true, _rec?) {
    static fn := %A_ThisFunc%, document := '', JS
    if !document {
        document := ComObject('HTMLFILE')
        document.write('<meta http-equiv="X-UA-Compatible" content="IE=9">')
        JS := document.parentWindow
        (document.documentMode < 9 && JS.execScript())
    }
    switch {
        case !IsSet(_rec)    : obj := fn(JS.JSON.parse(json), objIsMap, true)
        case !IsObject(json) : obj := json
        case JS.Object.prototype.toString.call(json) == '[object Array]':
            obj := []
            Loop json.length {
                obj.Push(fn(json.%A_Index - 1%, objIsMap, true))
            }
        default:
            obj := objIsMap ? Map() : {}
            keys := JS.Object.keys(json)
            Loop keys.length {
                k := keys.%A_Index - 1%
                objIsMap ? obj[k]  := fn(json.%k%, true , true)
                         : obj.%k% := fn(json.%k%, false, true)
            }
    }
    return obj
}

Re: Updating the JsonToAHK function to AHK v2

Posted: 01 May 2024, 11:24
by Loop
Thank you very much! Brilliant, it works perfectly.