55
66#include < Array.au3>
77#include < MsgBoxConstants.au3>
8+ #include " ..\NetWebView2Lib.au3"
89
910; Global objects handler for COM objects
1011Global $oMyError = ObjEvent (" AutoIt.Error" , _ErrFunc)
@@ -14,18 +15,19 @@ _Example()
1415Exit
1516
1617Func _Example()
17- ; Initialize the COM Object
18- Local $oJson = ObjCreate (" NetJson.Parser" )
19- If Not IsObj ($oJson ) Then
18+ ConsoleWrite (@CRLF & " === STARTING NETJSON TUTORIAL ===" & @CRLF )
19+
20+ #Region ; 0. Initialize the COM Object
21+ Local $oJson = _NetJson_CreateParser()
22+ If @error Then
2023 MsgBox (16 , " Error" , " Could not create NetWebView2Lib.JsonParser. Make sure the DLL is registered." )
2124 Return
2225 EndIf
26+ #EndRegion ; 0. Initialize the COM Object
2327
24- ConsoleWrite (@CRLF & " === STARTING NETJSON TUTORIAL ===" & @CRLF )
2528
26- ; ---------------------------------------------------------
29+ #Region ; 1. PARSING & BASICS
2730 ConsoleWrite (@CRLF & " +> 1. PARSING & BASICS <+" & @CRLF )
28- ; ---------------------------------------------------------
2931 Local $sRaw = ' {"user": "John", "roles": ["Admin", "Tester"], "active": true}'
3032 $oJson .Parse($sRaw )
3133 ConsoleWrite (" Full JSON: " & $oJson .GetJson() & @CRLF )
@@ -34,20 +36,22 @@ Func _Example()
3436 If $oJson .Exists(" user" ) Then
3537 ConsoleWrite (" - User exists: " & $oJson .GetTokenValue(" user" ) & @CRLF )
3638 EndIf
39+ #EndRegion ; 1. PARSING & BASICS
40+
3741
38- ; ---------------------------------------------------------
42+ #Region ; 2. ARRAY OPERATIONS
3943 ConsoleWrite (@CRLF & " +> 2. ARRAY OPERATIONS <+" & @CRLF )
40- ; ---------------------------------------------------------
4144 ; Get length of the 'roles' array
4245 Local $iRolesCount = $oJson .GetArrayLength(" roles" )
4346 ConsoleWrite (" - Roles count: " & $iRolesCount & @CRLF )
4447
4548 ; Get specific element from array
4649 ConsoleWrite (" - First role: " & $oJson .GetTokenValue(" roles[0]" ) & @CRLF )
50+ #EndRegion ; 2. ARRAY OPERATIONS
4751
48- ; ---------------------------------------------------------
52+
53+ #Region ; 2b. DEEP PATH NOTATION (The Power of JSON Path)
4954 ConsoleWrite (@CRLF & " +> 2b. DEEP PATH NOTATION (The Power of JSON Path) <+" & @CRLF )
50- ; ---------------------------------------------------------
5155 Local $sComplex = ' {"store": {"book": [{"title": "Coding 101", "price": 10}, {"title": "AutoIt Guru", "price": 25}], "location": "Athens"}}'
5256 $oJson .Parse($sComplex )
5357
@@ -63,17 +67,19 @@ Func _Example()
6367 If $oJson .Exists(" store.location" ) Then
6468 ConsoleWrite (" - Store Location: " & $oJson .GetTokenValue(" store.location" ) & @CRLF )
6569 EndIf
70+ #EndRegion ; 2b. DEEP PATH NOTATION (The Power of JSON Path)
71+
6672
67- ; ---------------------------------------------------------
73+ #Region ; 3. MODIFICATION (SetTokenValue)
6874 ConsoleWrite (@CRLF & " +> 3. MODIFICATION (SetTokenValue) <+" & @CRLF )
69- ; ---------------------------------------------------------
7075 $oJson .SetTokenValue(" user" , " George" )
7176 $oJson .SetTokenValue(" active" , " false" ) ; Note: Values are sent as strings
7277 ConsoleWrite (" - Updated User: " & $oJson .GetTokenValue(" user" ) & @CRLF )
78+ #EndRegion ; 3. MODIFICATION (SetTokenValue)
79+
7380
74- ; ---------------------------------------------------------
81+ #Region ; 4. FILE I/O
7582 ConsoleWrite (@CRLF & " +> 4. FILE I/O <+" & @CRLF )
76- ; ---------------------------------------------------------
7783 ; Save current state to a file
7884 $oJson .SaveToFile(@ScriptDir & " \settings.json" )
7985 ConsoleWrite (" JSON saved to file." & @CRLF )
@@ -84,49 +90,51 @@ Func _Example()
8490
8591 $oJson .LoadFromFile(@ScriptDir & " \settings.json" )
8692 ConsoleWrite (" - Reloaded from file, User is: " & $oJson .GetTokenValue(" user" ) & @CRLF )
93+ #EndRegion ; 4. FILE I/O
8794
88- ; ---------------------------------------------------------
95+
96+ #Region ; 5. FORMATTING (Pretty vs Minified)
8997 ConsoleWrite (@CRLF & " +> 5. FORMATTING (Pretty vs Minified) <+" & @CRLF )
90- ; ---------------------------------------------------------
9198 ConsoleWrite (@CRLF & " --- PRETTY JSON ---" & @CRLF )
9299 ConsoleWrite ($oJson .GetPrettyJson() & @CRLF )
93100
94101 ConsoleWrite (@CRLF & " --- MINIFIED JSON ---" & @CRLF )
95102 ConsoleWrite ($oJson .GetMinifiedJson() & @CRLF )
103+ #EndRegion ; 5. FORMATTING (Pretty vs Minified)
104+
96105
97- ; ---------------------------------------------------------
106+ #Region ; 6. ESCAPING TOOLS (Utility Methods)
98107 ConsoleWrite (@CRLF & " +> 6. ESCAPING TOOLS (Utility Methods) <+" & @CRLF )
99- ; ---------------------------------------------------------
100108 Local $sDirtyString = ' Hello "World" \ Name'
101109 Local $sEscaped = $oJson .EscapeString($sDirtyString )
102110 ConsoleWrite (" - Escaped: " & $sEscaped & @CRLF )
103111 ConsoleWrite (" - Unescaped: " & $oJson .UnescapeString($sEscaped ) & @CRLF )
112+ #EndRegion ; 6. ESCAPING TOOLS (Utility Methods)
104113
105- ; ---------------------------------------------------------
114+
115+ #Region ; 7. ADVANCED DATA INTELLIGENCE (v1.4.1)
106116 ConsoleWrite (@CRLF & " +> 7. ADVANCED DATA INTELLIGENCE (v1.4.1) <+" & @CRLF )
107- ; ---------------------------------------------------------
108117
109- ; ---------------------------------------------------------
118+ #Region ; 7.1 MERGE EXAMPLE
110119 ConsoleWrite (@CRLF & " +> 7.1 MERGE EXAMPLE <+" & @CRLF )
111- ; ---------------------------------------------------------
112120 ; Let's merge some new data into our current JSON
113121 Local $sUpdate = ' {"user": "Admin_SmartUser", "preferences": {"theme": "Dark", "notifications": true}}'
114122 $oJson .Merge($sUpdate )
115123 ConsoleWrite (" - After Merge (User Updated & Prefs Added): " & $oJson .GetTokenValue(" user" ) & @CRLF )
116124 ConsoleWrite (" - New Nested Value: " & $oJson .GetTokenValue(" preferences.theme" ) & @CRLF )
125+ #EndRegion ; 7.1 MERGE EXAMPLE
117126
118- ; ---------------------------------------------------------
127+ #Region ; 7.2 TYPE CHECKING
119128 ConsoleWrite (@CRLF & " +> 7.2 TYPE CHECKING <+" & @CRLF )
120- ; ---------------------------------------------------------
121129 ; Check what kind of data we have at a specific path
122130 Local $sThemeType = $oJson .GetTokenType(" preferences" )
123131 Local $sUserType = $oJson .GetTokenType(" user" )
124132 ConsoleWrite (" - 'preferences' type: " & $sThemeType & @CRLF ) ; Should return Object
125133 ConsoleWrite (" - 'user' type: " & $sUserType & @CRLF ) ; Should return String
134+ #EndRegion ; 7.2 TYPE CHECKING
126135
127- ; ---------------------------------------------------------
136+ #Region ; 7.3 SEARCH (JSONPath)
128137 ConsoleWrite (@CRLF & " +> 7.3 SEARCH (JSONPath) <+" & @CRLF )
129- ; ---------------------------------------------------------
130138 ; $..* Returns everything in a flat list (all values).
131139 ; $.store.book[*].author Returns all authors in the book array.
132140 ; $..book[0] Returns the first book, wherever the book array is.
@@ -145,18 +153,18 @@ Func _Example()
145153 ; Expected result: ["AutoIt Guru"]
146154 Local $sFullObjects = $oJson .Search(" $..book[?(@.price > 15)]" )
147155 ConsoleWrite (" - Full Objects : " & $sFullObjects & @CRLF )
156+ #EndRegion ; 7.3 SEARCH (JSONPath)
148157
149- ; ---------------------------------------------------------
158+ #Region ; 7.4 FLATTEN
150159 ConsoleWrite (@CRLF & " +> 7.4 FLATTEN <+" & @CRLF )
151- ; ---------------------------------------------------------
152160 ; Convert the complex nested structure into a flat key-value list
153161 Local $sFlatJson = $oJson .Flatten()
154162 ConsoleWrite (" - FLATTENED VIEW (Key.Path = Value)" & @CRLF )
155163 ConsoleWrite ($sFlatJson & @CRLF )
164+ #EndRegion ; 7.4 FLATTEN
156165
157- ; ---------------------------------------------------------
166+ #Region ; 7.5 REMOVE TOKEN
158167 ConsoleWrite (@CRLF & " +> 7.5 REMOVE TOKEN <+" & @CRLF )
159- ; ---------------------------------------------------------
160168 ; Remove the 'preferences' object completely
161169 Local $bRemoved = $oJson .RemoveToken(" preferences" )
162170
@@ -172,18 +180,18 @@ Func _Example()
172180 Else
173181 ConsoleWrite (" - Verify existence: Gone! (Success)" & @CRLF )
174182 EndIf
183+ #EndRegion ; 7.5 REMOVE TOKEN
175184
176- ; ---------------------------------------------------------
185+ #Region ; 7.6 CLONE LOGIC
177186 ConsoleWrite (@CRLF & " +> 7.6 CLONE LOGIC <+" & @CRLF )
178- ; ---------------------------------------------------------
179187 ; Check if we can backup our data
180188 If $oJson .CloneTo(" BackupInstance" ) Then
181189 ConsoleWrite (" - Data integrity check for cloning: OK" & @CRLF )
182190 EndIf
191+ #EndRegion ; 7.6 CLONE LOGIC
183192
184- ; ---------------------------------------------------------
193+ #Region ; 7.7 FLATTEN TO TABLE (_ArrayFromString ready)
185194 ConsoleWrite (@CRLF & " +> 7.7 FLATTEN TO TABLE (_ArrayFromString ready) <+" & @CRLF )
186- ; ---------------------------------------------------------
187195 Local $sTable = $oJson .FlattenToTable(" |" , @CRLF )
188196 Local $aFinalGrid = _ArrayFromString($sTable , " |" , @CRLF , True )
189197
@@ -194,14 +202,16 @@ Func _Example()
194202 ConsoleWrite ($i & " ) " & $aFinalGrid [$i ][0 ] & " = " & $aFinalGrid [$i ][1 ] & @CRLF )
195203 Next
196204 EndIf
205+ #EndRegion ; 7.7 FLATTEN TO TABLE (_ArrayFromString ready)
197206
198- ; ---------------------------------------------------------
199- ConsoleWrite ( @CRLF & " --- TUTORIAL COMPLETED --- " & @CRLF )
200- ; ---------------------------------------------------------
201- ; Clean up object
207+ #EndRegion ; 7. ADVANCED DATA INTELLIGENCE (v1.4.1)
208+
209+
210+ #Region ; 8. Clean up object
202211 $oJson = Null
212+ #EndRegion ; 8. Clean up object
203213
204- ; ---------------------------------------------------------
214+ ConsoleWrite ( @CRLF & " --- TUTORIAL COMPLETED ---" & @CRLF )
205215
206216EndFunc ; ==>_Example
207217
0 commit comments