Advertisement
Guest User

Untitled

a guest
Feb 8th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /****************************************************************************************
  2.     Function: BuildJson(obj)
  3.         Builds a JSON string from an AutoHotkey object
  4.  
  5.     Parameters:
  6.         obj - An AutoHotkey array or object, which can include nested objects.
  7.  
  8.     Remarks:
  9.         Originally Obj2Str() by Coco,
  10.         http://www.autohotkey.com/board/topic/93300-what-format-to-store-settings-in/page-2#entry588373
  11.        
  12.         Modified to use double quotes instead of single quotes and to leave numeric values
  13.         unquoted.
  14.  
  15.     Returns:
  16.         The JSON string
  17. */
  18. BuildJson(obj)
  19. {
  20.     str := "" , array := true
  21.     for k in obj {
  22.         if (k == A_Index)
  23.             continue
  24.         array := false
  25.         break
  26.     }
  27.     for a, b in obj
  28.         str .= (array ? "" : """" a """:") . (IsObject(b) ? BuildJson(b) : IsNumber(b) ? b : """" b """") . ","
  29.     str := RTrim(str, " ,")
  30.     return (array ? "[" str "]" : "{" str "}")
  31. }
  32.  
  33. /****************************************************************************************
  34.     Function: ParseJson(jsonStr)
  35.         Converts a JSON string into an AutoHotkey object
  36.  
  37.     Parameters:
  38.         jsonstr - the JSON string to convert
  39.  
  40.     Remarks:
  41.         Originally by Getfree,
  42.         http://www.autohotkey.com/board/topic/93300-what-format-to-store-settings-in/#entry588268
  43.  
  44.     Returns:
  45.         The AutoHotkey object.
  46. */
  47. ParseJson(jsonStr)
  48. {
  49.     SC := ComObjCreate("ScriptControl")
  50.     SC.Language := "JScript"
  51.     ComObjError(false)
  52.     jsCode =
  53.     (
  54.     function arrangeForAhkTraversing(obj){
  55.         if(obj instanceof Array){
  56.             for(var i=0 ; i<obj.length ; ++i)
  57.                 obj[i] = arrangeForAhkTraversing(obj[i]) ;
  58.             return ['array',obj] ;
  59.         }else if(obj instanceof Object){
  60.             var keys = [], values = [] ;
  61.             for(var key in obj){
  62.                 keys.push(key) ;
  63.                 values.push(arrangeForAhkTraversing(obj[key])) ;
  64.             }
  65.             return ['object',[keys,values]] ;
  66.         }else
  67.             return [typeof obj,obj] ;
  68.     }
  69.     )
  70.     SC.ExecuteStatement(jsCode "; obj=" jsonStr)
  71.     return convertJScriptObjToAhks( SC.Eval("arrangeForAhkTraversing(obj)") )
  72. }
  73.  
  74. /*!
  75.     Function: convertJScriptObjToAhks(jsObj)
  76.         Used by ParseJson()
  77. */
  78. convertJScriptObjToAhks(jsObj)
  79. {
  80.     if(jsObj[0]="object"){
  81.         obj := {}, keys := jsObj[1][0], values := jsObj[1][1]
  82.         loop % keys.length
  83.             obj[keys[A_INDEX-1]] := convertJScriptObjToAhks( values[A_INDEX-1] )
  84.         return obj
  85.     }else if(jsObj[0]="array"){
  86.         array := []
  87.         loop % jsObj[1].length
  88.             array.insert(convertJScriptObjToAhks( jsObj[1][A_INDEX-1] ))
  89.         return array
  90.     }else
  91.         return jsObj[1]
  92. }
  93.  
  94. /*!
  95.     Function: IsNumber(Num)
  96.         Checks if Num is a number.
  97.  
  98.     Returns:
  99.         True if Num is a number, false if not
  100. */
  101. IsNumber(Num)
  102. {
  103.     if Num is number
  104.         return true
  105.     else
  106.         return false
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement