|
| 1 | +;~ #AutoIt3Wrapper_UseX64=y |
| 2 | + |
| 3 | +; NetWebView2Lib.JsonParser - Tutorial Script |
| 4 | + |
| 5 | +#include <Array.au3> |
| 6 | +#include <MsgBoxConstants.au3> |
| 7 | + |
| 8 | +; Global objects handler for COM objects |
| 9 | +Global $oMyError = ObjEvent("AutoIt.Error", "_ErrFunc") |
| 10 | + |
| 11 | +; Initialize the COM Object |
| 12 | +Local $oJson = ObjCreate("NetJson.Parser") |
| 13 | +If Not IsObj($oJson) Then |
| 14 | + MsgBox(16, "Error", "Could not create NetWebView2Lib.JsonParser. Make sure the DLL is registered.") |
| 15 | + Exit |
| 16 | +EndIf |
| 17 | + |
| 18 | +ConsoleWrite(@CRLF & "=== STARTING NETJSON TUTORIAL ===" & @CRLF) |
| 19 | + |
| 20 | +; --------------------------------------------------------- |
| 21 | + ConsoleWrite(@CRLF & "+> 1. PARSING & BASICS <+" & @CRLF) |
| 22 | +; --------------------------------------------------------- |
| 23 | +Local $sRaw = '{"user": "John", "roles": ["Admin", "Tester"], "active": true}' |
| 24 | +$oJson.Parse($sRaw) |
| 25 | +ConsoleWrite("Full JSON: " & $oJson.GetJson() & @CRLF) |
| 26 | + |
| 27 | +; Check if a path exists |
| 28 | +If $oJson.Exists("user") Then |
| 29 | + ConsoleWrite("- User exists: " & $oJson.GetTokenValue("user") & @CRLF) |
| 30 | +EndIf |
| 31 | + |
| 32 | +; --------------------------------------------------------- |
| 33 | + ConsoleWrite(@CRLF & "+> 2. ARRAY OPERATIONS <+" & @CRLF) |
| 34 | +; --------------------------------------------------------- |
| 35 | +; Get length of the 'roles' array |
| 36 | +Local $iRolesCount = $oJson.GetArrayLength("roles") |
| 37 | +ConsoleWrite("- Roles count: " & $iRolesCount & @CRLF) |
| 38 | + |
| 39 | +; Get specific element from array |
| 40 | +ConsoleWrite("- First role: " & $oJson.GetTokenValue("roles[0]") & @CRLF) |
| 41 | + |
| 42 | +; --------------------------------------------------------- |
| 43 | + ConsoleWrite(@CRLF & "+> 2b. DEEP PATH NOTATION (The Power of JSON Path) <+" & @CRLF) |
| 44 | +; --------------------------------------------------------- |
| 45 | +Local $sComplex = '{"store": {"book": [{"title": "Coding 101", "price": 10}, {"title": "AutoIt Guru", "price": 25}], "location": "Athens"}}' |
| 46 | +$oJson.Parse($sComplex) |
| 47 | + |
| 48 | +; Direct access to a deep value using dots and brackets |
| 49 | +; Path: store -> book -> second element [1] -> title |
| 50 | +Local $sTitle = $oJson.GetTokenValue("store.book[1].title") |
| 51 | +Local $iPrice = $oJson.GetTokenValue("store.book[1].price") |
| 52 | + |
| 53 | +ConsoleWrite("- Deep Search (Title): " & $sTitle & @CRLF) |
| 54 | +ConsoleWrite("- Deep Search (Price): " & $iPrice & @CRLF) |
| 55 | + |
| 56 | +; Check existence of a deep path |
| 57 | +If $oJson.Exists("store.location") Then |
| 58 | + ConsoleWrite("- Store Location: " & $oJson.GetTokenValue("store.location") & @CRLF) |
| 59 | +EndIf |
| 60 | + |
| 61 | +; --------------------------------------------------------- |
| 62 | + ConsoleWrite(@CRLF & "+> 3. MODIFICATION (SetTokenValue) <+" & @CRLF) |
| 63 | +; --------------------------------------------------------- |
| 64 | +$oJson.SetTokenValue("user", "George") |
| 65 | +$oJson.SetTokenValue("active", "false") ; Note: Values are sent as strings |
| 66 | +ConsoleWrite("- Updated User: " & $oJson.GetTokenValue("user") & @CRLF) |
| 67 | + |
| 68 | +; --------------------------------------------------------- |
| 69 | + ConsoleWrite(@CRLF & "+> 4. FILE I/O <+" & @CRLF) |
| 70 | +; --------------------------------------------------------- |
| 71 | +; Save current state to a file |
| 72 | +$oJson.SaveToFile(@ScriptDir & "\settings.json") |
| 73 | +ConsoleWrite("JSON saved to file." & @CRLF) |
| 74 | + |
| 75 | +; Clear and Reload from file |
| 76 | +$oJson.Clear() |
| 77 | +ConsoleWrite("- After Clear, Json is: " & $oJson.GetJson() & @CRLF) |
| 78 | + |
| 79 | +$oJson.LoadFromFile(@ScriptDir & "\settings.json") |
| 80 | +ConsoleWrite("- Reloaded from file, User is: " & $oJson.GetTokenValue("user") & @CRLF) |
| 81 | + |
| 82 | +; --------------------------------------------------------- |
| 83 | + ConsoleWrite(@CRLF & "+> 5. FORMATTING (Pretty vs Minified) <+" & @CRLF) |
| 84 | +; --------------------------------------------------------- |
| 85 | +ConsoleWrite(@CRLF & "--- PRETTY JSON ---" & @CRLF) |
| 86 | +ConsoleWrite($oJson.GetPrettyJson() & @CRLF) |
| 87 | + |
| 88 | +ConsoleWrite(@CRLF & "--- MINIFIED JSON ---" & @CRLF) |
| 89 | +ConsoleWrite($oJson.GetMinifiedJson() & @CRLF) |
| 90 | + |
| 91 | +; --------------------------------------------------------- |
| 92 | + ConsoleWrite(@CRLF & "+> 6. ESCAPING TOOLS (Utility Methods) <+" & @CRLF) |
| 93 | +; --------------------------------------------------------- |
| 94 | +Local $sDirtyString = 'Hello "World" \ Name' |
| 95 | +Local $sEscaped = $oJson.EscapeString($sDirtyString) |
| 96 | +ConsoleWrite("- Escaped: " & $sEscaped & @CRLF) |
| 97 | +ConsoleWrite("- Unescaped: " & $oJson.UnescapeString($sEscaped) & @CRLF) |
| 98 | + |
| 99 | +; --------------------------------------------------------- |
| 100 | + ConsoleWrite(@CRLF & "+> 7. ADVANCED DATA INTELLIGENCE (v1.4.1) <+" & @CRLF) |
| 101 | +; --------------------------------------------------------- |
| 102 | + |
| 103 | +; --------------------------------------------------------- |
| 104 | + ConsoleWrite(@CRLF & "+> 7.1 MERGE EXAMPLE <+" & @CRLF) |
| 105 | +; --------------------------------------------------------- |
| 106 | +; Let's merge some new data into our current JSON |
| 107 | +Local $sUpdate = '{"user": "Admin_SmartUser", "preferences": {"theme": "Dark", "notifications": true}}' |
| 108 | +$oJson.Merge($sUpdate) |
| 109 | +ConsoleWrite("- After Merge (User Updated & Prefs Added): " & $oJson.GetTokenValue("user") & @CRLF) |
| 110 | +ConsoleWrite("- New Nested Value: " & $oJson.GetTokenValue("preferences.theme") & @CRLF) |
| 111 | + |
| 112 | +; --------------------------------------------------------- |
| 113 | + ConsoleWrite(@CRLF & "+> 7.2 TYPE CHECKING <+" & @CRLF) |
| 114 | +; --------------------------------------------------------- |
| 115 | +; Check what kind of data we have at a specific path |
| 116 | +Local $sThemeType = $oJson.GetTokenType("preferences") |
| 117 | +Local $sUserType = $oJson.GetTokenType("user") |
| 118 | +ConsoleWrite("- 'preferences' type: " & $sThemeType & @CRLF) ; Should return Object |
| 119 | +ConsoleWrite("- 'user' type: " & $sUserType & @CRLF) ; Should return String |
| 120 | + |
| 121 | +; --------------------------------------------------------- |
| 122 | + ConsoleWrite(@CRLF & "+> 7.3 SEARCH (JSONPath) <+" & @CRLF) |
| 123 | +; --------------------------------------------------------- |
| 124 | +; $..* Returns everything in a flat list (all values). |
| 125 | +; $.store.book[*].author Returns all authors in the book array. |
| 126 | +; $..book[0] Returns the first book, wherever the book array is. |
| 127 | +; $..price Returns all prices from all objects. |
| 128 | +; Let's find all titles in our store (from section 2b) |
| 129 | +Local $sTitlesFound = $oJson.Search("$..title") |
| 130 | +ConsoleWrite("- Search results (All Titles): " & $sTitlesFound & @CRLF) |
| 131 | + |
| 132 | +; Query Explanation: |
| 133 | +; $..book -> Search everywhere for the array "book" |
| 134 | +; [?(@.price > 15)] -> Filter the objects where the price property is > 15 |
| 135 | +; .title -> Of the ones found, return only the title |
| 136 | +Local $sExpensiveBooks = $oJson.Search("$..book[?(@.price > 15)].title") |
| 137 | + |
| 138 | +ConsoleWrite("- Books pricier than 15: " & $sExpensiveBooks & @CRLF) |
| 139 | +; Expected result: ["AutoIt Guru"] |
| 140 | +Local $sFullObjects = $oJson.Search("$..book[?(@.price > 15)]") |
| 141 | +ConsoleWrite("- Full Objects : " & $sFullObjects & @CRLF) |
| 142 | + |
| 143 | +; --------------------------------------------------------- |
| 144 | + ConsoleWrite(@CRLF & "+> 7.4 FLATTEN <+" & @CRLF) |
| 145 | +; --------------------------------------------------------- |
| 146 | +; Convert the complex nested structure into a flat key-value list |
| 147 | +Local $sFlatJson = $oJson.Flatten() |
| 148 | +ConsoleWrite("- FLATTENED VIEW (Key.Path = Value)" & @CRLF) |
| 149 | +ConsoleWrite($sFlatJson & @CRLF) |
| 150 | + |
| 151 | +; --------------------------------------------------------- |
| 152 | + ConsoleWrite(@CRLF & "+> 7.5 REMOVE TOKEN <+" & @CRLF) |
| 153 | +; --------------------------------------------------------- |
| 154 | +; Remove the 'preferences' object completely |
| 155 | +Local $bRemoved = $oJson.RemoveToken("preferences") |
| 156 | + |
| 157 | +If $bRemoved Then |
| 158 | + ConsoleWrite("- 'preferences' removal command sent." & @CRLF) |
| 159 | +Else |
| 160 | + ConsoleWrite("! Error: Path 'preferences' not found to remove." & @CRLF) |
| 161 | +EndIf |
| 162 | + |
| 163 | +If $oJson.Exists("preferences") Then |
| 164 | + ConsoleWrite("! Verify existence: Still there (Something went wrong)" & @CRLF) |
| 165 | + ConsoleWrite("! Current JSON: " & $oJson.GetMinifiedJson() & @CRLF) |
| 166 | +Else |
| 167 | + ConsoleWrite("- Verify existence: Gone! (Success)" & @CRLF) |
| 168 | +EndIf |
| 169 | + |
| 170 | +; --------------------------------------------------------- |
| 171 | + ConsoleWrite(@CRLF & "+> 7.6 CLONE LOGIC <+" & @CRLF) |
| 172 | +; --------------------------------------------------------- |
| 173 | +; Check if we can backup our data |
| 174 | +If $oJson.CloneTo("BackupInstance") Then |
| 175 | + ConsoleWrite("- Data integrity check for cloning: OK" & @CRLF) |
| 176 | +EndIf |
| 177 | + |
| 178 | +; --------------------------------------------------------- |
| 179 | + ConsoleWrite(@CRLF & "+> 7.7 FLATTEN TO TABLE (_ArrayFromString ready) <+" & @CRLF) |
| 180 | +; --------------------------------------------------------- |
| 181 | +Local $sTable = $oJson.FlattenToTable("|", @CRLF) |
| 182 | +Local $aFinalGrid = _ArrayFromString($sTable, "|", @CRLF, True) |
| 183 | + |
| 184 | +If Not @error Then |
| 185 | + ConsoleWrite("- Array successfully created from FlattenToTable!" & @CRLF) |
| 186 | + ;_ArrayDisplay($aFinalGrid, "v1.4.1 Final Table View") |
| 187 | + For $i = 0 To UBound($aFinalGrid) - 1 |
| 188 | + ConsoleWrite($i & ") " & $aFinalGrid[$i][0] & " = " & $aFinalGrid[$i][1] & @CRLF) |
| 189 | + Next |
| 190 | +EndIf |
| 191 | + |
| 192 | +; --------------------------------------------------------- |
| 193 | +ConsoleWrite(@CRLF & "--- TUTORIAL COMPLETED ---" & @CRLF) |
| 194 | +; --------------------------------------------------------- |
| 195 | +; Clean up object |
| 196 | +$oJson = Null |
| 197 | + |
| 198 | +; --------------------------------------------------------- |
| 199 | + |
| 200 | +Func _ErrFunc($oError) ; User's COM error function. Will be called if COM error occurs |
| 201 | + ; Do anything here. |
| 202 | + ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _ |
| 203 | + @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _ |
| 204 | + @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _ |
| 205 | + @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _ |
| 206 | + @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _ |
| 207 | + @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _ |
| 208 | + @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _ |
| 209 | + @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _ |
| 210 | + @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _ |
| 211 | + @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF) |
| 212 | +EndFunc ;==>_ErrFunc |
0 commit comments