Skip to content

Commit 3e59d5e

Browse files
authored
Update 000-NetJson.Parser.au3
1 parent 12a7156 commit 3e59d5e

1 file changed

Lines changed: 197 additions & 190 deletions

File tree

examples/000-NetJson.Parser.au3

Lines changed: 197 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -6,196 +6,203 @@
66
#include <MsgBoxConstants.au3>
77

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

200207
Func _ErrFunc($oError) ; User's COM error function. Will be called if COM error occurs
201208
; Do anything here.

0 commit comments

Comments
 (0)