jsonpath_rust/
lib.rs

1//! # Json path
2//! The library provides the basic functionality
3//! to find the slice of data according to the query.
4//! The idea comes from xpath for xml structures.
5//! The details can be found over [`there`]
6//! Therefore JSONPath is a query language for JSON,
7//! similar to XPath for XML. The jsonpath query is a set of assertions to specify the JSON fields that need to be verified.
8//!
9//! # Simple example
10//! Let's suppose we have a following json:
11//! ```json
12//!  {
13//!   "shop": {
14//!    "orders": [
15//!       {"id": 1, "active": true},
16//!       {"id": 2 },
17//!       {"id": 3 },
18//!       {"id": 4, "active": true}
19//!     ]
20//!   }
21//! }
22//! ```
23//! And we pursue to find all orders id having the field 'active'
24//! we can construct the jsonpath instance like that
25//! ```$.shop.orders[?(@.active)].id``` and get the result ``` [1,4] ```
26//!
27//! # Another examples
28//! ```json
29//! { "store": {
30//!     "book": [
31//!       { "category": "reference",
32//!         "author": "Nigel Rees",
33//!         "title": "Sayings of the Century",
34//!         "price": 8.95
35//!       },
36//!       { "category": "fiction",
37//!         "author": "Evelyn Waugh",
38//!         "title": "Sword of Honour",
39//!         "price": 12.99
40//!       },
41//!       { "category": "fiction",
42//!         "author": "Herman Melville",
43//!         "title": "Moby Dick",
44//!         "isbn": "0-553-21311-3",
45//!         "price": 8.99
46//!       },
47//!       { "category": "fiction",
48//!         "author": "J. R. R. Tolkien",
49//!         "title": "The Lord of the Rings",
50//!         "isbn": "0-395-19395-8",
51//!         "price": 22.99
52//!       }
53//!     ],
54//!     "bicycle": {
55//!       "color": "red",
56//!       "price": 19.95
57//!     }
58//!   }
59//! }
60//! ```
61//! and examples
62//! - ``` $.store.book[*].author ``` : the authors of all books in the store
63//! - ``` $..book[?(@.isbn)]``` : filter all books with isbn number
64//! - ``` $..book[?(@.price<10)]``` : filter all books cheapier than 10
65//! - ``` $..*``` : all Elements in XML document. All members of JSON structure
66//! - ``` $..book[0,1]``` : The first two books
67//! - ``` $..book[:2]``` : The first two books
68//!
69//! # Operators
70//!
71//! - `$` : Pointer to the root of the json. It is gently advising to start every jsonpath from the root. Also, inside the filters to point out that the path is starting from the root.
72//! - `@`Pointer to the current element inside the filter operations.It is used inside the filter operations to iterate the collection.
73//! - `*` or `[*]`Wildcard. It brings to the list all objects and elements regardless their names.It is analogue a flatmap operation.
74//! - `<..>`| Descent operation. It brings to the list all objects, children of that objects and etc It is analogue a flatmap operation.
75//! - `.<name>` or `.['<name>']`the key pointing to the field of the objectIt is used to obtain the specific field.
76//! - `['<name>' (, '<name>')]`the list of keysthe same usage as for a single key but for list
77//! - `[<number>]`the filter getting the element by its index.
78//! - `[<number> (, <number>)]`the list if elements of array according to their indexes representing these numbers. |
79//! - `[<start>:<end>:<step>]`slice operator to get a list of element operating with their indexes. By default step = 1, start = 0, end = array len. The elements can be omitted ```[:]```
80//! - `[?(<expression>)]`the logical expression to filter elements in the list.It is used with arrays preliminary.
81//!
82//!
83//! [`there`]: https://goessner.net/articles/JsonPath/
84#![allow(warnings)]
85
86pub mod query;
87
88#[allow(clippy::module_inception)]
89pub mod parser;
90
91#[macro_use]
92extern crate pest_derive;
93extern crate core;
94extern crate pest;
95
96use crate::query::queryable::Queryable;
97use crate::query::{Queried, QueryPath, QueryRef};
98use serde_json::Value;
99
100/// A trait for types that can be queried with JSONPath.
101pub trait JsonPath: Queryable {
102    /// Queries the value with a JSONPath expression and returns a vector of `QueryResult`.
103    fn query_with_path(&self, path: &str) -> Queried<Vec<QueryRef<Self>>> {
104        query::js_path(path, self)
105    }
106
107    /// Queries the value with a JSONPath expression and returns a vector of values.
108    fn query_only_path(&self, path: &str) -> Queried<Vec<QueryPath>> {
109        query::js_path_path(path, self)
110    }
111
112    /// Queries the value with a JSONPath expression and returns a vector of values, omitting the path.
113    fn query(&self, path: &str) -> Queried<Vec<&Self>> {
114        query::js_path_vals(path, self)
115    }
116}
117
118impl JsonPath for Value {}