-
Notifications
You must be signed in to change notification settings - Fork 28
/
lib.rs
400 lines (367 loc) · 12.3 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
//! # Json path
//! The library provides the basic functionality
//! to find the slice of data according to the query.
//! The idea comes from xpath for xml structures.
//! The details can be found over [`there`]
//! Therefore JSONPath is a query language for JSON,
//! similar to XPath for XML. The jsonpath query is a set of assertions to specify the JSON fields that need to be verified.
//!
//! # Simple example
//! Let's suppose we have a following json:
//! ```json
//! {
//! "shop": {
//! "orders": [
//! {"id": 1, "active": true},
//! {"id": 2 },
//! {"id": 3 },
//! {"id": 4, "active": true}
//! ]
//! }
//! }
//! ```
//! And we pursue to find all orders id having the field 'active'
//! we can construct the jsonpath instance like that
//! ```$.shop.orders[?(@.active)].id``` and get the result ``` [1,4] ```
//!
//! # Another examples
//! ```json
//! { "store": {
//! "book": [
//! { "category": "reference",
//! "author": "Nigel Rees",
//! "title": "Sayings of the Century",
//! "price": 8.95
//! },
//! { "category": "fiction",
//! "author": "Evelyn Waugh",
//! "title": "Sword of Honour",
//! "price": 12.99
//! },
//! { "category": "fiction",
//! "author": "Herman Melville",
//! "title": "Moby Dick",
//! "isbn": "0-553-21311-3",
//! "price": 8.99
//! },
//! { "category": "fiction",
//! "author": "J. R. R. Tolkien",
//! "title": "The Lord of the Rings",
//! "isbn": "0-395-19395-8",
//! "price": 22.99
//! }
//! ],
//! "bicycle": {
//! "color": "red",
//! "price": 19.95
//! }
//! }
//! }
//! ```
//! and examples
//! - ``` $.store.book[*].author ``` : the authors of all books in the store
//! - ``` $..book[?(@.isbn)]``` : filter all books with isbn number
//! - ``` $..book[?(@.price<10)]``` : filter all books cheapier than 10
//! - ``` $..*``` : all Elements in XML document. All members of JSON structure
//! - ``` $..book[0,1]``` : The first two books
//! - ``` $..book[:2]``` : The first two books
//!
//! # Operators
//!
//! - `$` : 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.
//! - `@`Pointer to the current element inside the filter operations.It is used inside the filter operations to iterate the collection.
//! - `*` or `[*]`Wildcard. It brings to the list all objects and elements regardless their names.It is analogue a flatmap operation.
//! - `<..>`| Descent operation. It brings to the list all objects, children of that objects and etc It is analogue a flatmap operation.
//! - `.<name>` or `.['<name>']`the key pointing to the field of the objectIt is used to obtain the specific field.
//! - `['<name>' (, '<name>')]`the list of keysthe same usage as for a single key but for list
//! - `[<number>]`the filter getting the element by its index.
//! - `[<number> (, <number>)]`the list if elements of array according to their indexes representing these numbers. |
//! - `[<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 ```[:]```
//! - `[?(<expression>)]`the logical expression to filter elements in the list.It is used with arrays preliminary.
//!
//! # Examples
//!```rust
//! use std::str::FromStr;
//! use serde_json::{json, Value};
//! use jsonpath_rust::{jp_v, JsonPathValue, JsonPath};
//!
//! fn test() -> Result<(), Box<dyn std::error::Error>> {
//! let json = serde_json::from_str(r#"{"first":{"second":[{"active":1},{"passive":1}]}}"#)?;
//! let path = JsonPath::try_from("$.first.second[?(@.active)]")?;
//! let slice_of_data:Vec<JsonPathValue<Value>> = path.find_slice(&json);
//! let js = json!({"active":1});
//! assert_eq!(slice_of_data, jp_v![&js;"$.first.second[0]",]);
//! # Ok(())
//! }
//! ```
//!
//!
//! [`there`]: https://goessner.net/articles/JsonPath/
pub use parser::model::JsonPath;
pub use parser::JsonPathParserError;
use serde_json::Value;
use std::fmt::Debug;
use std::ops::Deref;
use JsonPathValue::{NewValue, NoValue, Slice};
mod jsonpath;
pub mod parser;
pub mod path;
#[macro_use]
extern crate pest_derive;
extern crate core;
extern crate pest;
/// the trait allows to query a path on any value by just passing the &str of as JsonPath.
///
/// It is equal to
/// ```rust
/// # use serde_json::json;
/// # use std::str::FromStr;
/// use jsonpath_rust::JsonPath;
///
/// let query = "$.hello";
/// let json_path = JsonPath::from_str(query).unwrap();
/// json_path.find(&json!({"hello": "world"}));
/// ```
///
/// It is default implemented for [Value].
///
/// #Note:
/// the result is going to be cloned and therefore it can be significant for the huge queries.
/// if the same &str is used multiple times, it's more efficient to reuse a single JsonPath.
///
/// # Examples:
/// ```
/// use std::str::FromStr;
/// use serde_json::{json, Value};
/// use jsonpath_rust::jp_v;
/// use jsonpath_rust::{JsonPathQuery, JsonPath, JsonPathValue};
///
/// fn test() -> Result<(), Box<dyn std::error::Error>> {
/// let json: Value = serde_json::from_str("{}")?;
/// let v = json.path("$..book[?(@.author size 10)].title")?;
/// assert_eq!(v, json!([]));
///
/// let json: Value = serde_json::from_str("{}")?;
/// let path = json.path("$..book[?(@.author size 10)].title")?;
///
/// assert_eq!(path, json!(["Sayings of the Century"]));
///
/// let json: Value = serde_json::from_str("{}")?;
/// let path = JsonPath::try_from("$..book[?(@.author size 10)].title")?;
///
/// let v = path.find_slice(&json);
/// let js = json!("Sayings of the Century");
/// assert_eq!(v, jp_v![&js;"",]);
/// # Ok(())
/// }
///
/// ```
pub trait JsonPathQuery {
fn path(self, query: &str) -> Result<Value, JsonPathParserError>;
}
/// Json paths may return either pointers to the original json or new data. This custom pointer type allows us to handle both cases.
/// Unlike JsonPathValue, this type does not represent NoValue to allow the implementation of Deref.
pub enum JsonPtr<'a, Data> {
/// The slice of the initial json data
Slice(&'a Data),
/// The new data that was generated from the input data (like length operator)
NewValue(Data),
}
/// Allow deref from json pointer to value.
impl<'a> Deref for JsonPtr<'a, Value> {
type Target = Value;
fn deref(&self) -> &Self::Target {
match self {
JsonPtr::Slice(v) => v,
JsonPtr::NewValue(v) => v,
}
}
}
impl JsonPathQuery for Value {
fn path(self, query: &str) -> Result<Value, JsonPathParserError> {
let p = JsonPath::try_from(query)?;
Ok(p.find(&self))
}
}
/*
impl<T> JsonPathQuery for T
where T: Deref<Target=Value> {
fn path(self, query: &str) -> Result<Value, String> {
let p = JsonPath::from_str(query)?;
Ok(p.find(self.deref()))
}
}
*/
/// just to create a json path value of data
/// Example:
/// - `jp_v(&json) = JsonPathValue::Slice(&json)`
/// - `jp_v(&json;"foo") = JsonPathValue::Slice(&json, "foo".to_string())`
/// - `jp_v(&json,) = vec![JsonPathValue::Slice(&json)]`
/// - `jp_v[&json1,&json1] = vec![JsonPathValue::Slice(&json1),JsonPathValue::Slice(&json2)]`
/// - `jp_v(json) = JsonPathValue::NewValue(json)`
/// ```
/// use std::str::FromStr;
/// use serde_json::{json, Value};
/// use jsonpath_rust::{jp_v, JsonPathQuery, JsonPath, JsonPathValue};
///
/// fn test() -> Result<(), Box<dyn std::error::Error>> {
/// let json: Value = serde_json::from_str("{}")?;
/// let path = JsonPath::try_from("$..book[?(@.author size 10)].title")?;
/// let v = path.find_slice(&json);
///
/// let js = json!("Sayings of the Century");
/// assert_eq!(v, jp_v![&js;"",]);
/// # Ok(())
/// }
/// ```
#[macro_export]
macro_rules! jp_v {
(&$v:expr) =>{
JsonPathValue::Slice(&$v, String::new())
};
(&$v:expr ; $s:expr) =>{
JsonPathValue::Slice(&$v, $s.to_string())
};
($(&$v:expr;$s:expr),+ $(,)?) =>{
{
vec![
$(
jp_v!(&$v ; $s),
)+
]
}
};
($(&$v:expr),+ $(,)?) => {
{
vec![
$(
jp_v!(&$v),
)+
]
}
};
($v:expr) =>{
JsonPathValue::NewValue($v)
};
}
/// Represents the path of the found json data
type JsPathStr = String;
pub fn jsp_idx(prefix: &str, idx: usize) -> String {
format!("{}[{}]", prefix, idx)
}
pub fn jsp_obj(prefix: &str, key: &str) -> String {
format!("{}.['{}']", prefix, key)
}
/// A result of json path
/// Can be either a slice of initial data or a new generated value(like length of array)
#[derive(Debug, PartialEq, Clone)]
pub enum JsonPathValue<'a, Data> {
/// The slice of the initial json data
Slice(&'a Data, JsPathStr),
/// The new data that was generated from the input data (like length operator)
NewValue(Data),
/// The absent value that indicates the input data is not matched to the given json path (like the absent fields)
NoValue,
}
impl<'a, Data: Clone + Debug + Default> JsonPathValue<'a, Data> {
/// Transforms given value into data either by moving value out or by cloning
pub fn to_data(self) -> Data {
match self {
Slice(r, _) => r.clone(),
NewValue(val) => val,
NoValue => Data::default(),
}
}
/// Transforms given value into path
pub fn to_path(self) -> Option<JsPathStr> {
match self {
Slice(_, path) => Some(path),
_ => None,
}
}
pub fn from_root(data: &'a Data) -> Self {
Slice(data, String::from("$"))
}
pub fn new_slice(data: &'a Data, path: String) -> Self {
Slice(data, path.to_string())
}
}
impl<'a, Data> JsonPathValue<'a, Data> {
pub fn only_no_value(input: &[JsonPathValue<'a, Data>]) -> bool {
!input.is_empty() && input.iter().filter(|v| v.has_value()).count() == 0
}
pub fn map_vec(data: Vec<(&'a Data, JsPathStr)>) -> Vec<JsonPathValue<'a, Data>> {
data.into_iter()
.map(|(data, pref)| Slice(data, pref))
.collect()
}
pub fn map_slice<F>(self, mapper: F) -> Vec<JsonPathValue<'a, Data>>
where
F: FnOnce(&'a Data, JsPathStr) -> Vec<(&'a Data, JsPathStr)>,
{
match self {
Slice(r, pref) => mapper(r, pref)
.into_iter()
.map(|(d, s)| Slice(d, s))
.collect(),
NewValue(_) => vec![],
no_v => vec![no_v],
}
}
pub fn flat_map_slice<F>(self, mapper: F) -> Vec<JsonPathValue<'a, Data>>
where
F: FnOnce(&'a Data, JsPathStr) -> Vec<JsonPathValue<'a, Data>>,
{
match self {
Slice(r, pref) => mapper(r, pref),
_ => vec![NoValue],
}
}
pub fn has_value(&self) -> bool {
!matches!(self, NoValue)
}
pub fn vec_as_data(input: Vec<JsonPathValue<'a, Data>>) -> Vec<&'a Data> {
input
.into_iter()
.filter_map(|v| match v {
Slice(el, _) => Some(el),
_ => None,
})
.collect()
}
pub fn vec_as_pair(input: Vec<JsonPathValue<'a, Data>>) -> Vec<(&'a Data, JsPathStr)> {
input
.into_iter()
.filter_map(|v| match v {
Slice(el, v) => Some((el, v)),
_ => None,
})
.collect()
}
/// moves a pointer (from slice) out or provides a default value when the value was generated
pub fn slice_or(self, default: &'a Data) -> &'a Data {
match self {
Slice(r, _) => r,
NewValue(_) | NoValue => default,
}
}
}
#[cfg(test)]
mod tests {
use serde_json::Value;
use crate::JsonPath;
use std::str::FromStr;
#[test]
fn to_string_test() {
let path: Box<JsonPath<Value>> = Box::from(
JsonPath::from_str(
"$.['a'].a..book[1:3][*][1]['a','b'][?(@)][?(@.verb == 'TEST')].a.length()",
)
.unwrap(),
);
assert_eq!(
path.to_string(),
"$.'a'.'a'..book[1:3:1][*][1]['a','b'][?(@ exists )][?(@.'verb' == \"TEST\")].'a'.length()"
);
}
}