@@ -231,7 +231,7 @@ Given the json
231
231
| ` $..book[?(@.author ~= '(?i)REES')] ` | All books matching regex (ignore case) |
232
232
| ` $..* ` | Give me every thing |
233
233
234
- ### The library
234
+ ## Library Usage
235
235
236
236
The library intends to provide the basic functionality for ability to find the slices of data using the syntax, saying
237
237
above. The dependency can be found as following:
@@ -251,180 +251,43 @@ To extract data there are two methods, provided on the `value`:
251
251
``` rust
252
252
let v : JsonPathValue <Value > = ...
253
253
v . to_data ();
254
- v . slice_or ( & some_dafult_value )
255
-
254
+ v . slice_or (& some_dafault_value )
256
255
```
257
256
258
- ``` rust
259
- use jsonpath_rust :: JsonPathFinder ;
260
- use serde_json :: {json, Value , JsonPathValue };
257
+ ### Find
261
258
262
- fn main () {
263
- let finder = JsonPathFinder :: from_str (r # " {"first":{"second":[{"active":1},{"passive":1}]}}" # , " $.first.second[?(@.active)]" ). unwrap ();
264
- let slice_of_data : Vec <& Value > = finder . find_slice ();
265
- let js = json! ({" active" : 1 });
266
- assert_eq! (slice_of_data , vec! [JsonPathValue :: Slice (& js ," $.first.second[0]" . to_string ())]);
267
- }
268
- ```
259
+ there are 3 different functions to find data inside a ` value ` .
260
+ All take references, to increase reusability. Especially json parsing and jsonpath parsing can take significant time, compared to a simple find.
269
261
270
- or with a separate instantiation:
262
+ The methods ` find ` , ` find_as_path ` and ` find_slice ` take the same inputs, but handle them differently depending on your usecase. They are further described in the [ docs ] ( https://docs.rs/jsonpath-rust/latest/jsonpath_rust/index.html#functions ) .
271
263
272
264
``` rust
273
- use serde_json :: {json, Value };
274
- use crate :: jsonpath_rust :: { JsonPathFinder , JsonPathQuery , JsonPathInst , JsonPathValue } ;
265
+ use jsonpath_rust :: {JsonPathInst , JsonPathValue };
266
+ use serde_json :: json ;
275
267
use std :: str :: FromStr ;
276
268
277
- fn test () {
278
- let json : Value = serde_json :: from_str (" {}" ). unwrap ();
279
- let v = json . path (" $..book[?(@.author size 10)].title" ). unwrap ();
280
- assert_eq! (v , json! ([]));
281
-
282
- let json : Value = serde_json :: from_str (" {}" ). unwrap ();
283
- let path = & json . path (" $..book[?(@.author size 10)].title" ). unwrap ();
284
-
285
- assert_eq! (path , & json! ([" Sayings of the Century" ]));
286
-
287
- let json : Box <Value > = serde_json :: from_str (" {}" ). unwrap ();
288
- let path : Box <JsonPathInst > = Box :: from (JsonPathInst :: from_str (" $..book[?(@.author size 10)].title" ). unwrap ());
289
- let finder = JsonPathFinder :: new (json , path );
290
-
291
- let v = finder . find_slice ();
292
- let js = json! (" Sayings of the Century" );
293
- assert_eq! (v , vec! [JsonPathValue :: Slice (& js ," $.book[0].title" . to_string ())]);
294
- }
295
-
296
- ```
297
- In case, if there is no match ` find_slice ` will return ` vec![NoValue] ` and ` find ` return ` json!(null) `
298
-
299
- ``` rust
300
- use jsonpath_rust :: JsonPathFinder ;
301
- use serde_json :: {json, Value , JsonPathValue };
302
-
303
- fn main () {
304
- let finder = JsonPathFinder :: from_str (r # " {"first":{"second":[{"active":1},{"passive":1}]}}" # , " $.no_field" ). unwrap ();
305
- let res_js = finder . find ();
306
- assert_eq! (res_js , json! (null ));
307
- }
308
- ```
309
-
310
- also, it will work with the instances of [[ Value]] as well.
311
-
312
- ``` rust
313
- use serde_json :: Value ;
314
- use crate :: jsonpath_rust :: {JsonPathFinder , JsonPathQuery , JsonPathInst };
315
- use crate :: path :: {json_path_instance, PathInstance };
316
-
317
- fn test (json : Box <Value >, path : & str ) {
318
- let path = JsonPathInst :: from_str (path ). unwrap ();
319
- JsonPathFinder :: new (json , path )
320
- }
321
- ```
322
-
323
- also, the trait ` JsonPathQuery ` can be used:
324
-
325
- ``` rust
326
-
327
- use serde_json :: {json, Value };
328
- use jsonpath_rust :: JsonPathQuery ;
329
-
330
- fn test () {
331
- let json : Value = serde_json :: from_str (" {}" ). unwrap ();
332
- let v = json . path (" $..book[?(@.author size 10)].title" ). unwrap ();
333
- assert_eq! (v , json! ([]));
334
-
335
- let json : Value = serde_json :: from_str (template_json ()). unwrap ();
336
- let path = & json . path (" $..book[?(@.author size 10)].title" ). unwrap ();
337
-
338
- assert_eq! (path , & json! ([" Sayings of the Century" ]));
339
- }
340
- ```
341
-
342
- also, ` JsonPathInst ` can be used to query the data without cloning.
343
- ``` rust
344
- use serde_json :: {json, Value };
345
- use crate :: jsonpath_rust :: {JsonPathInst };
346
-
347
- fn test () {
348
- let json : Value = serde_json :: from_str (" {}" ). expect (" to get json" );
349
- let query = JsonPathInst :: from_str (" $..book[?(@.author size 10)].title" ). unwrap ();
350
-
351
- // To convert to &Value, use deref()
352
- assert_eq! (query . find_slice (& json ). get (0 ). expect (" to get value" ). deref (), & json! (" Sayings of the Century" ));
353
- }
354
- ```
355
-
356
- The library can return a path describing the value instead of the value itself.
357
- To do that, the method ` find_as_path ` can be used:
358
-
359
- ``` rust
360
- use jsonpath_rust :: JsonPathFinder ;
361
- use serde_json :: {json, Value , JsonPathValue };
362
-
363
269
fn main () {
364
- let finder = JsonPathFinder :: from_str (r # " {"first":{"second":[{"active":1},{"passive":1}]}}" # , " $.first.second[?(@.active)]" ). unwrap ();
365
- let slice_of_data : Value = finder . find_as_path ();
366
- assert_eq! (slice_of_data , Value :: Array (vec! [" $.first.second[0]" . to_string ()]));
367
- }
368
- ```
270
+ let data = json! ({" first" : {" second" : [{" active" : 1 },{" passive" : 1 }]}});
271
+ let path = JsonPathInst :: from_str (" $.first.second[?(@.active)]" ). unwrap ();
272
+ let slice_of_data = jsonpath_rust :: find_slice (& path , & data );
369
273
370
- or it can be taken from the ` JsonPathValue ` instance:
371
- ``` rust
372
- use serde_json :: {json, Value };
373
- use crate :: jsonpath_rust :: {JsonPathFinder , JsonPathQuery , JsonPathInst , JsonPathValue };
374
- use std :: str :: FromStr ;
375
-
376
- fn test () {
377
- let json : Box <Value > = serde_json :: from_str (" {}" ). unwrap ();
378
- let path : Box <JsonPathInst > = Box :: from (JsonPathInst :: from_str (" $..book[?(@.author size 10)].title" ). unwrap ());
379
- let finder = JsonPathFinder :: new (json , path );
274
+ let expected_value = json! ({" active" : 1 });
275
+ let expected_path = " $.['first'].['second'][0]" . to_string ();
380
276
381
- let v = finder . find_slice ();
382
- let js = json! (" Sayings of the Century" );
383
-
384
- // Slice has a path of its value as well
385
- assert_eq! (v , vec! [JsonPathValue :: Slice (& js ," $.book[0].title" . to_string ())]);
277
+ assert_eq! (
278
+ slice_of_data ,
279
+ vec! [JsonPathValue :: Slice (& expected_value , expected_path )]
280
+ );
386
281
}
387
282
```
388
283
389
- ** If the value has been modified during the search, there is no way to find a path of a new value.
390
- It can happen if we try to find a length() of array, for in stance.**
391
-
284
+ ### The structure
392
285
286
+ The internal structure of the ` JsonPath ` can be found here:
287
+ https://docs.rs/jsonpath-rust/latest/jsonpath_rust/parser/model/enum.JsonPath.html
393
288
394
- ## The structure
395
-
396
- ``` rust
397
- pub enum JsonPath {
398
- Root ,
399
- // <- $
400
- Field (String ),
401
- // <- field of the object
402
- Chain (Vec <JsonPath >),
403
- // <- the whole jsonpath
404
- Descent (String ),
405
- // <- '..'
406
- Index (JsonPathIndex ),
407
- // <- the set of indexes represented by the next structure [[JsonPathIndex]]
408
- Current (Box <JsonPath >),
409
- // <- @
410
- Wildcard ,
411
- // <- *
412
- Empty , // the structure to avoid inconsistency
413
- }
414
-
415
- pub enum JsonPathIndex {
416
- Single (usize ),
417
- // <- [1]
418
- UnionIndex (Vec <f64 >),
419
- // <- [1,2,3]
420
- UnionKeys (Vec <String >),
421
- // <- ['key_1','key_2']
422
- Slice (i32 , i32 , usize ),
423
- // [0:10:1]
424
- Filter (Operand , FilterSign , Operand ), // <- [?(operand sign operand)]
425
- }
426
-
427
- ```
289
+ The internal structure of the ` JsonPathIndex ` can be found here:
290
+ https://docs.rs/jsonpath-rust/latest/jsonpath_rust/parser/model/enum.JsonPathIndex.html
428
291
429
292
## How to contribute
430
293
434
297
- update files
435
298
- commit them
436
299
- add tag ` git tag -a v<Version> -m "message" `
437
- - git push origin <tag_name>
300
+ - git push origin <tag_name>
0 commit comments