@@ -17,7 +17,7 @@ ObjectPath allows you to access values within objects using string path expressi
1717### Basic Usage
1818
1919``` csharp
20- using ObjectPath ;
20+ using ObjectPathLibrary ;
2121
2222// Example object
2323var obj = new
@@ -32,15 +32,15 @@ var obj = new
3232};
3333
3434// Access values using path expressions
35- var name = ObjectPath . GetValue ( obj , " Name" ); // John
36- var city = ObjectPath . GetValue ( obj , " Address.City" ); // New York
35+ var name = obj . GetValueByPath ( " Name" ); // John
36+ var city = obj . GetValueByPath ( " Address.City" ); // New York
3737```
3838
3939### JSON Example
4040
4141``` csharp
4242using System .Text .Json ;
43- using ObjectPath ;
43+ using ObjectPathLibrary ;
4444
4545// JSON string
4646var json = @"
@@ -57,16 +57,16 @@ var jsonDocument = JsonDocument.Parse(json);
5757var jsonElement = jsonDocument .RootElement ;
5858
5959// Access values using path expressions
60- var name = ObjectPath . GetValue ( jsonElement , " name" ); // John
61- var street = ObjectPath . GetValue ( jsonElement , " address.street" ); // 123 Main St
60+ var name = jsonElement . GetValueByPath ( " name" ); // John
61+ var street = jsonElement . GetValueByPath ( " address.street" ); // 123 Main St
6262```
6363
6464### Array Example
6565
6666ObjectPath supports accessing array and list elements using index notation.
6767
6868``` csharp
69- using ObjectPath ;
69+ using ObjectPathLibrary ;
7070
7171// Example object with arrays and lists
7272var obj = new
@@ -80,8 +80,8 @@ var obj = new
8080};
8181
8282// Access array elements
83- var firstNumber = ObjectPath . GetValue ( obj , " Numbers[0]" ); // 1
84- var secondPersonName = ObjectPath . GetValue ( obj , " People[1].Name" ); // Jane
83+ var firstNumber = obj . GetValueByPath ( " Numbers[0]" ); // 1
84+ var secondPersonName = obj . GetValueByPath ( " People[1].Name" ); // Jane
8585```
8686
8787### Case Sensitivity
@@ -102,10 +102,104 @@ var age = ObjectPath.GetValue(obj, "age", ignoreCase: false); // 30
102102var invalid = ObjectPath .GetValue (obj , " name" , ignoreCase : false );
103103```
104104
105+ ### Dictionary Access
106+
107+ You can also access values in dictionaries using path expressions.
108+
109+ ``` csharp
110+ using ObjectPathLibrary ;
111+ using Xunit ;
112+
113+ [Fact ]
114+ public void DictionaryAccessTest ()
115+ {
116+ var dic = new Dictionary <string , object >
117+ {
118+ [" Name" ] = " John" ,
119+ [" Age" ] = 30 ,
120+ [" Address" ] = new Dictionary <string , object >
121+ {
122+ [" City" ] = " New York" ,
123+ [" Street" ] = " 123 Main St"
124+ }
125+ };
126+
127+ // Act
128+ var name = dic .GetValueByPath (" Name" );
129+ var age = dic .GetValueByPath (" Age" );
130+ var city = dic .GetValueByPath (" Address.City" );
131+ var street = dic .GetValueByPath (" Address.Street" );
132+
133+ var address = dic .GetValueByPath (" Address" ) as IDictionary <string , object >;
134+ var addressExpando = address ! .ToExpando ();
135+
136+ // Assert
137+ Assert .Equal (" John" , name );
138+ Assert .Equal (30 , age );
139+ Assert .Equal (" New York" , city );
140+ Assert .Equal (" 123 Main St" , street );
141+
142+ Assert .Equal (" New York" , address [" City" ]);
143+ Assert .Equal (" 123 Main St" , address [" Street" ]);
144+
145+ Assert .Equal (" New York" , addressExpando ? .City );
146+ Assert .Equal (" 123 Main St" , addressExpando ? .Street );
147+ }
148+ ```
149+
150+ ### Handling Exceptions
151+
152+ Use ` GetValueByPathOrNull ` to safely get values without throwing exceptions.
153+
154+ ``` csharp
155+ using ObjectPathLibrary ;
156+
157+ var obj = new
158+ {
159+ Name = " John" ,
160+ Address = new
161+ {
162+ City = " New York"
163+ }
164+ };
165+
166+ // Access values using path expressions
167+ var name = obj .GetValueByPathOrNull (" Name" ); // John
168+ var nonExistent = obj .GetValueByPathOrNull (" NonExistentProperty" ); // null
169+ ```
170+
171+ ## ObjectPathExtensions.cs
172+
173+ ``` csharp
174+ namespace ObjectPathLibrary
175+ {
176+ public static class ObjectPathExtensions
177+ {
178+ public static object ? GetValueByPath (this object obj , string path )
179+ {
180+ return ObjectPath .GetValue (obj , path );
181+ }
182+
183+ public static object ? GetValueByPathOrNull (this object obj , string path )
184+ {
185+ try
186+ {
187+ return ObjectPath .GetValue (obj , path );
188+ }
189+ catch (Exception )
190+ {
191+ return null ;
192+ }
193+ }
194+ }
195+ }
196+ ```
197+
105198## Features
106199
107200- Access nested properties and fields.
108201- Supports JSON elements.
109202- Supports array and list index access.
110203- Case-insensitive by default, with an option for case sensitivity.
111204- Handles complex nested structures.
205+ - Provides safe access with ` GetValueByPathOrNull ` .
0 commit comments