Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 04/12/2022 in all areas

  1. Introduction JSON (Javascript Object Notation) is a popular data-interchange format and supported by a lot of script languages. On AutoIt, there is already a >JSON UDF written by Gabriel Boehme. It is good but too slow, and not supports unicode and control characters very well. So I write a new one (and of course, fast one as usual). I use a machine code version of JSON parser called "jsmn". jsmn not only supports standard JSON, but also accepts some non-strict JSON string. See below for example. Important Update!! I rename the library from jsmn.au3 to json.au3. All function names are changed, too. Decoding Function Json_Decode($Json) $Json can be a standard or non-standard JSON string. For example, it accepts: { server: example.com port: 80 message: "this looks like a config file" } The most JSON data type will be decoded into corresponding AutoIt variable, including 1D array, string, number, true, false, and null. JSON object will be decoded into "Windows Scripting Dictionary Object" retuned from ObjCreate("Scripting.Dictionary"). AutoIt build-in functions like IsArray, IsBool, etc. can be used to check the returned data type. But for Object and Null, Json_IsObject() and Json_IsNull() should be used. If the input JSON string is invalid, @Error will be set to $JSMN_ERROR_INVAL. And if the input JSON string is not finish (maybe read from stream?), @Error will be set to $JSMN_ERROR_PART. Encoding Function Json_Encode($Data, $Option = 0, $Indent = "\t", $ArraySep = ",\r\n", $ObjectSep = ",\r\n", $ColonSep = ": ") $Data can be a string, number, bool, keyword(default or null), 1D arrry, or "Scripting.Dictionary" COM object. Ptr will be converted to number, Binary will be converted to string in UTF8 encoding. Other unsupported types like 2D array, dllstruct or object will be encoded into null. $Option is bitmask consisting following constant: $JSON_UNESCAPED_ASCII ; Don't escape ascii charcters between chr(1) ~ chr(0x1f) $JSON_UNESCAPED_UNICODE ; Encode multibyte Unicode characters literally $JSON_UNESCAPED_SLASHES ; Don't escape / $JSON_HEX_TAG ; All < and > are converted to \u003C and \u003E $JSON_HEX_AMP ; All &amp;amp;amp;amp;s are converted to \u0026 $JSON_HEX_APOS ; All ' are converted to \u0027 $JSON_HEX_QUOT ; All " are converted to \u0022 $JSON_PRETTY_PRINT ; Use whitespace in returned data to format it $JSON_STRICT_PRINT ; Make sure returned JSON string is RFC4627 compliant $JSON_UNQUOTED_STRING ; Output unquoted string if possible (conflicting with $JSMN_STRICT_PRINT) Most encoding option have the same means like PHP's json_enocde() function. When $JSON_PRETTY_PRINT is set, output format can be change by other 4 parameters ($Indent, $ArraySep, $ObjectSep, and $ColonSep). Because these 4 output format parameters will be checked inside Jsmn_Encode() function, returned string will be always accepted by Jsmn_Decode(). $JSON_UNQUOTED_STRING can be used to output unquoted string that also accetped by Jsmn_Decode(). $JSON_STRICT_PRINT is used to check output format setting and avoid non-standard JSON output. So this option is conflicting with $JSON_UNQUOTED_STRING. Get and Put Functions Json_Put(ByRef $Var, $Notation, $Data, $CheckExists = False) Json_Get(ByRef $Var, $Notation) These functions helps user to access object or array more easily. Both dot notation and square bracket notation can be supported. Json_Put() by default will create non-exists objects and arrays. For example: Local $Obj Json_Put($Obj, ".foo", "foo") Json_Put($Obj, ".bar[0]", "bar") Json_Put($Obj, ".test[1].foo.bar[2].foo.bar", "Test") Local $Test = Json_Get($Obj, '["test"][1]["foo"]["bar"][2]["foo"]["bar"]') ; "Test" Object Help Functions Json_ObjCreate() Json_ObjPut(ByRef $Object, $Key, $Value) Json_ObjGet(ByRef $Object, $Key) Json_ObjDelete(ByRef $Object, $Key) Json_ObjExists(ByRef $Object, $Key) Json_ObjGetCount(ByRef $Object) Json_ObjGetKeys(ByRef $Object) Json_ObjClear(ByRef $Object) These functions are just warps of "Scripting.Dictionary" COM object. You can use these functions if you are not already familiar with it. == Update 2013/05/19 == * Add Jsmn_Encode() option "$JSMN_UNESCAPED_ASCII". Now the default output of Json_Encode() is exactly the same as PHP's json_encode() function (for example, chr(1) will be encoded into u0001). $JSON_UNESCAPED_ASCII ; Don't escape ascii charcters between chr(1) ~ chr(0x1f) == Update 2015/01/08 == * Rename the library from jsmn.au3 to json.au3. All function names are changed, too. * Add Json_Put() and Json_Get() * Add Null support * Using BinaryCall.au3 to loading the machine code. == Update 2018/01/13== (Jos) * Add JsonDump() to list all Json Keys and their values to easily figure out what they are. == Update 2018/10/01== (Jos) * Fixed JsonDump() some fields and values were not showing as discussed here - tnx @TheXman . == Update 2018/10/01b== (Jos) * Added Json_ObjGetItems, Tidied source and fixed au3check warnings - tnx @TheXman . == Update 2018/10/28== (Jos) * Added declaration for $value to avoid au3check warning - tnx @DerPensionist == Update 2018/12/16== (Jos) * Added another declaration for $value to avoid au3check warning and updated the version at the top - tnx @maniootek == Update 2018/12/29== (Jos) * Changed Json_ObjGet() and Json_ObjExists() to allow for multilevel object in string. == Update 2019/01/17== (Jos) * Added support for DOT notation in JSON functions. == Update 2019/07/15== (Jos) * Added support for reading keys with a dot inside when using a dot as separator (updated) == Update 2021/11/18== (TheXman) * Update details in below post: == Update 2021/11/20== (TheXman) * Minor RegEx update, no change to the functionality or result._Json(2021.11.20).zip
    1 point
  2. Deye

    Catchy Replace example

    Replace single characters on the left with anything specified between matching vertical bars on the right $txt = "Jack's birthday is 02/02/02 & Jill's is the same" MsgBox(0, '', _PatternStringReplace($txt, "'/ &", "|-|_|And")) MsgBox(0, '', _PatternStringReplace($txt, "/&20", "2|could it be that")) MsgBox(0, '', _PatternStringReplace($txt, "j02/&'", "M")) Func _PatternStringReplace($sTxt, $sPatt, $sReplace)     Local $a1 = StringSplit($sPatt, "")     Local $a2 = StringSplit($sReplace, "|")     ReDim $a2[UBound($a1)]     Local $sda = ObjCreate("Scripting.Dictionary")     For $i = 1 To UBound($a1) - 1         $sda.item($a1[$i]) = $a2[$i]         If StringIsAlpha($a1[$i]) Then $sda.item(StringIsLower($a1[$i]) ? StringUpper($a1[$i]) : StringLower($a1[$i])) = $a2[$i]     Next     Return Execute('"' & StringRegExpReplace($sTxt, "(?i)([" & $sPatt & "])", '" & $sda.Item("$1") & "') & '"') EndFunc
    1 point
  3. abberration

    AutoIt Snippets

    _StringCapitalize() - version 1.0 I like _StringProper, but it has some unwanted effects, like capitalizing the letter s after an apostrophe (ex: cat's = Cat'S) or letters after other symbols. Also, _StringProper changes all letters after the first to lowercase. With this code, it only capitalizes the first letter and leaves the rest alone. If you use the exceptions option ($text4 example), it makes email addresses all lowercase and deals with names that begin with mc (like McDonald). Lastly, you have the ability to override the default symbols that the script will capitalize afterwards (space, period, comma, brace, bracket, etc.). When you run the example, you will see the difference between _StringProper and _StringCapitalize (standard and with exceptions). It's not perfect (yet), but it's closer to what I want than _StringProper. Usage: _StringCapitalize ( string, [, exceptions= 0 [, symbols="[.| |,|/(|/[|-]" ] ] ) Parameters: string string passed to function exceptions [optional] Default = 0 0 = do not use exceptions 1 = use exceptions symbols [optional] define symbols for function to use #include <String.au3> #include <Array.au3> $text1 = "mcdonald's AshtonK Ke$ha #trending @realcelebrity (etc) TEST@MSN.NET" $text2 = _StringProper($text1) $text3 = _StringCapitalize($text1) $text4 = _StringCapitalize($text1, 1) ; 1 = use email and "mc" exceptions $text5 = _StringCapitalize($text1, 1, "[.| |,|/[|-|#|@|*]") ; user defined symbols - script will capitalize next letter after these symbols. MsgBox(0, "", "Original Text:" & @CRLF & $text1 & @CRLF & @CRLF & "_StringProper:" & @CRLF & $text2 & @CRLF & @CRLF & "_StringCapitalize:" & @CRLF & $text3 & @CRLF & @CRLF & "_StringCapitalize with exceptions:" & @CRLF & $text4 & @CRLF & @CRLF & "_StringCapitalize with exceptions and user specified symbols:" & @CRLF & $text5) Func _StringCapitalize($__sText, $__iExceptions = 0, $__sSymbols = "[.| |,|/(|/[|-]") ; version 1.0 $__aText = StringSplit($__sText, "") For $i = 1 To $__aText[0] If $i = 1 OR StringRegExp($__aText[$i-1], $__sSymbols) = 1 Then $__aText[$i] = StringUpper($__aText[$i]) EndIf Next $__textOut = _ArrayToString($__aText, "", 1) If $__iExceptions = 1 Then ; Lowercase all email addresses $__aExpression = StringRegExp($__textOut, "[\w]+@[\w]+\.+[\w]+", 3) For $i = 1 To UBound($__aExpression) $__textOut = StringReplace($__textOut, $__aExpression[$i - 1], StringLower($__aExpression[$i - 1])) Next $__aExpression = "" ; reset variable ; Format words beginning with mc. Ex: mcdonald = McDonald $__aExpression = StringRegExp($__textOut, "([Mm][Cc][A-Za-z])", 3) For $i = 1 To UBound($__aExpression) $__textOut = StringReplace($__textOut, $__aExpression[$i - 1], "Mc" & StringUpper(StringRight($__aExpression[$i - 1], 1))) Next EndIf Return $__textOut EndFunc
    1 point
  4. Would that be helpful to you ? ConsoleWrite("Frames=" & _WD_GetFrameCount($sSession) & @CRLF) Local $aTag = _WD_FindElement($sSession, $_WD_LOCATOR_ByTagName, "iFrame", Default, True) _ArrayColInsert($aTag, 1) _ArrayColInsert($aTag, 1) For $i = 0 To UBound($aTag)-1 $aTag[$i][1] = _WD_ElementAction($sSession, $aTag[$i][0], "attribute", "name") $aTag[$i][2] = _WD_ElementAction($sSession, $aTag[$i][0], "attribute", "id") Next _ArrayDisplay($aTag)
    1 point
×
×
  • Create New...