jsonpath_lib/lib.rs
1//! JsonPath implementation written in Rust.
2//!
3//! # Example
4//! ```
5//! extern crate jsonpath_lib as jsonpath;
6//! #[macro_use] extern crate serde_json;
7//! let json_obj = json!({
8//! "store": {
9//! "book": [
10//! {
11//! "category": "reference",
12//! "author": "Nigel Rees",
13//! "title": "Sayings of the Century",
14//! "price": 8.95
15//! },
16//! {
17//! "category": "fiction",
18//! "author": "Evelyn Waugh",
19//! "title": "Sword of Honour",
20//! "price": 12.99
21//! },
22//! {
23//! "category": "fiction",
24//! "author": "Herman Melville",
25//! "title": "Moby Dick",
26//! "isbn": "0-553-21311-3",
27//! "price": 8.99
28//! },
29//! {
30//! "category": "fiction",
31//! "author": "J. R. R. Tolkien",
32//! "title": "The Lord of the Rings",
33//! "isbn": "0-395-19395-8",
34//! "price": 22.99
35//! }
36//! ],
37//! "bicycle": {
38//! "color": "red",
39//! "price": 19.95
40//! }
41//! },
42//! "expensive": 10
43//! });
44//!
45//! let mut selector = jsonpath::selector(&json_obj);
46//!
47//! assert_eq!(selector("$.store.book[*].author").unwrap(),
48//! vec![
49//! "Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien"
50//! ]);
51//!
52//! assert_eq!(selector("$..author").unwrap(),
53//! vec![
54//! "Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien"
55//! ]);
56//!
57//! assert_eq!(selector("$.store.*").unwrap(),
58//! vec![
59//! &json!([
60//! { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 },
61//! { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 },
62//! { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 },
63//! { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 }
64//! ]),
65//! &json!({ "color": "red", "price": 19.95 })
66//! ]);
67//!
68//! assert_eq!(selector("$.store..price").unwrap(),
69//! vec![
70//! 8.95, 12.99, 8.99, 22.99, 19.95
71//! ]);
72//!
73//! assert_eq!(selector("$..book[2]").unwrap(),
74//! vec![
75//! &json!({
76//! "category" : "fiction",
77//! "author" : "Herman Melville",
78//! "title" : "Moby Dick",
79//! "isbn" : "0-553-21311-3",
80//! "price" : 8.99
81//! })
82//! ]);
83//!
84//! assert_eq!(selector("$..book[-2]").unwrap(),
85//! vec![
86//! &json!({
87//! "category" : "fiction",
88//! "author" : "Herman Melville",
89//! "title" : "Moby Dick",
90//! "isbn" : "0-553-21311-3",
91//! "price" : 8.99
92//! })
93//! ]);
94//!
95//! assert_eq!(selector("$..book[0,1]").unwrap(),
96//! vec![
97//! &json!({"category" : "reference","author" : "Nigel Rees","title" : "Sayings of the Century","price" : 8.95}),
98//! &json!({"category" : "fiction","author" : "Evelyn Waugh","title" : "Sword of Honour","price" : 12.99})
99//! ]);
100//!
101//! assert_eq!(selector("$..book[:2]").unwrap(),
102//! vec![
103//! &json!({"category" : "reference","author" : "Nigel Rees","title" : "Sayings of the Century","price" : 8.95}),
104//! &json!({"category" : "fiction","author" : "Evelyn Waugh","title" : "Sword of Honour","price" : 12.99})
105//! ]);
106//!
107//! assert_eq!(selector("$..book[:2]").unwrap(),
108//! vec![
109//! &json!({"category" : "reference","author" : "Nigel Rees","title" : "Sayings of the Century","price" : 8.95}),
110//! &json!({"category" : "fiction","author" : "Evelyn Waugh","title" : "Sword of Honour","price" : 12.99})
111//! ]);
112//!
113//! assert_eq!(selector("$..book[?(@.isbn)]").unwrap(),
114//! vec![
115//! &json!({"category" : "fiction","author" : "Herman Melville","title" : "Moby Dick","isbn" : "0-553-21311-3","price" : 8.99}),
116//! &json!({"category" : "fiction","author" : "J. R. R. Tolkien","title" : "The Lord of the Rings","isbn" : "0-395-19395-8","price" : 22.99})
117//! ]);
118//!
119//! assert_eq!(selector("$.store.book[?(@.price < 10)]").unwrap(),
120//! vec![
121//! &json!({"category" : "reference","author" : "Nigel Rees","title" : "Sayings of the Century","price" : 8.95}),
122//! &json!({"category" : "fiction","author" : "Herman Melville","title" : "Moby Dick","isbn" : "0-553-21311-3","price" : 8.99})
123//! ]);
124//! ```
125extern crate core;
126#[macro_use]
127extern crate log;
128extern crate serde;
129extern crate serde_json;
130
131use serde_json::Value;
132
133#[allow(deprecated)]
134use parser::Node;
135#[allow(deprecated)]
136pub use parser::Parser;
137#[allow(deprecated)]
138pub use select::{Selector, SelectorMut};
139
140#[deprecated(
141since = "0.4.0",
142note = "It will be move to common module. since 0.5"
143)]
144pub use select::JsonPathError;
145
146pub use selector::{JsonSelector, JsonSelectorMut};
147pub use paths::PathParser;
148use std::rc::Rc;
149
150#[doc(hidden)]
151#[deprecated(
152since = "0.4.0",
153note = "'ffi' is moved to another location like 'wasm' from version 0.5.x"
154)]
155mod ffi;
156#[doc(hidden)]
157mod parser;
158#[doc(hidden)]
159mod select;
160
161mod paths;
162mod selector;
163
164impl From<&paths::TokenError> for JsonPathError {
165 fn from(e: &paths::TokenError) -> Self {
166 match e {
167 paths::TokenError::Eof => JsonPathError::Path("Eof".to_string()),
168 paths::TokenError::Position(pos) => JsonPathError::Path(["Position:", &pos.to_string()].concat())
169 }
170 }
171}
172
173/// It is a high-order function. it compile a jsonpath and then returns a closure that has JSON as argument. if you need to reuse a jsonpath, it is good for performance.
174///
175/// ```rust
176/// extern crate jsonpath_lib as jsonpath;
177/// #[macro_use] extern crate serde_json;
178///
179/// let mut first_firend = jsonpath::compile("$..friends[0]");
180///
181/// let json_obj = json!({
182/// "school": {
183/// "friends": [
184/// {"name": "친구1", "age": 20},
185/// {"name": "친구2", "age": 20}
186/// ]
187/// },
188/// "friends": [
189/// {"name": "친구3", "age": 30},
190/// {"name": "친구4"}
191/// ]});
192///
193/// let json = first_firend(&json_obj).unwrap();
194///
195/// assert_eq!(json, vec![
196/// &json!({"name": "친구3", "age": 30}),
197/// &json!({"name": "친구1", "age": 20})
198/// ]);
199/// ```
200#[deprecated(
201since = "0.2.5",
202note = "Please use the PathCompiled::compile function instead. It will be removed from 0.4.1"
203)]
204pub fn compile(path: &str) -> impl FnMut(&Value) -> Result<Vec<&Value>, JsonPathError> {
205 #[allow(deprecated)]
206 let node = parser::Parser::compile(path);
207 move |json| match &node {
208 Ok(node) => {
209 #[allow(deprecated)]
210 let mut selector = Selector::default();
211 selector.compiled_path(node).value(json).select()
212 }
213 Err(e) => Err(JsonPathError::Path(e.to_string())),
214 }
215}
216
217/// It is a high-order function. it returns a closure that has a jsonpath string as argument. you can use diffenent jsonpath for one JSON object.
218///
219/// ```rust
220/// extern crate jsonpath_lib as jsonpath;
221/// #[macro_use] extern crate serde_json;
222///
223/// let json_obj = json!({
224/// "school": {
225/// "friends": [
226/// {"name": "친구1", "age": 20},
227/// {"name": "친구2", "age": 20}
228/// ]
229/// },
230/// "friends": [
231/// {"name": "친구3", "age": 30},
232/// {"name": "친구4"}
233/// ]});
234///
235/// let mut selector = jsonpath::selector(&json_obj);
236///
237/// let json = selector("$..friends[0]").unwrap();
238///
239/// assert_eq!(json, vec![
240/// &json!({"name": "친구3", "age": 30}),
241/// &json!({"name": "친구1", "age": 20})
242/// ]);
243///
244/// let json = selector("$..friends[1]").unwrap();
245///
246/// assert_eq!(json, vec![
247/// &json!({"name": "친구4"}),
248/// &json!({"name": "친구2", "age": 20})
249/// ]);
250/// ```
251#[allow(clippy::needless_lifetimes)]
252pub fn selector<'a>(json: &'a Value) -> impl FnMut(&'a str) -> Result<Vec<&'a Value>, JsonPathError> {
253 let mut selector = JsonSelector::default();
254 move |path| {
255 let parser = PathParser::compile(path).map_err(|e| JsonPathError::from(&e))?;
256 selector.reset_parser(parser).value(json).reset_value().select()
257 }
258}
259
260/// It is the same to `selector` function. but it deserialize the result as given type `T`.
261///
262/// ```rust
263/// extern crate jsonpath_lib as jsonpath;
264/// extern crate serde;
265/// #[macro_use] extern crate serde_json;
266///
267/// use serde::{Deserialize, Serialize};
268///
269/// let json_obj = json!({
270/// "school": {
271/// "friends": [
272/// {"name": "친구1", "age": 20},
273/// {"name": "친구2", "age": 20}
274/// ]
275/// },
276/// "friends": [
277/// {"name": "친구3", "age": 30},
278/// {"name": "친구4"}
279/// ]});
280///
281/// #[derive(Deserialize, PartialEq, Debug)]
282/// struct Friend {
283/// name: String,
284/// age: Option<u8>,
285/// }
286///
287/// let mut selector = jsonpath::selector_as::<Friend>(&json_obj);
288///
289/// let json = selector("$..friends[0]").unwrap();
290///
291/// let ret = vec!(
292/// Friend { name: "친구3".to_string(), age: Some(30) },
293/// Friend { name: "친구1".to_string(), age: Some(20) }
294/// );
295/// assert_eq!(json, ret);
296///
297/// let json = selector("$..friends[1]").unwrap();
298///
299/// let ret = vec!(
300/// Friend { name: "친구4".to_string(), age: None },
301/// Friend { name: "친구2".to_string(), age: Some(20) }
302/// );
303///
304/// assert_eq!(json, ret);
305/// ```
306pub fn selector_as<'a, T: serde::de::DeserializeOwned>(json: &'a Value)
307 -> impl FnMut(&'a str) -> Result<Vec<T>, JsonPathError> + '_
308{
309 let mut selector = JsonSelector::default();
310 let _ = selector.value(json);
311 move |path: &str| {
312 let parser = PathParser::compile(path).map_err(|e| JsonPathError::from(&e))?;
313 selector.reset_parser(parser).reset_value().select_as()
314 }
315}
316
317/// It is a simple select function. but it compile the jsonpath argument every time.
318///
319/// ```rust
320/// extern crate jsonpath_lib as jsonpath;
321/// #[macro_use] extern crate serde_json;
322///
323/// let json_obj = json!({
324/// "school": {
325/// "friends": [
326/// {"name": "친구1", "age": 20},
327/// {"name": "친구2", "age": 20}
328/// ]
329/// },
330/// "friends": [
331/// {"name": "친구3", "age": 30},
332/// {"name": "친구4"}
333/// ]});
334///
335/// let json = jsonpath::select(&json_obj, "$..friends[0]").unwrap();
336///
337/// assert_eq!(json, vec![
338/// &json!({"name": "친구3", "age": 30}),
339/// &json!({"name": "친구1", "age": 20})
340/// ]);
341/// ```
342pub fn select<'a>(json: &'a Value, path: &'a str) -> Result<Vec<&'a Value>, JsonPathError> {
343 let parser = PathParser::compile(path).map_err(|e| JsonPathError::from(&e))?;
344 JsonSelector::new(parser).value(json).select()
345}
346
347/// It is the same to `select` function but it return the result as string.
348///
349/// ```rust
350/// extern crate jsonpath_lib as jsonpath;
351/// #[macro_use] extern crate serde_json;
352///
353/// let ret = jsonpath::select_as_str(r#"
354/// {
355/// "school": {
356/// "friends": [
357/// {"name": "친구1", "age": 20},
358/// {"name": "친구2", "age": 20}
359/// ]
360/// },
361/// "friends": [
362/// {"name": "친구3", "age": 30},
363/// {"name": "친구4"}
364/// ]
365/// }
366/// "#, "$..friends[0]").unwrap();
367///
368/// assert_eq!(ret, r#"[{"name":"친구3","age":30},{"name":"친구1","age":20}]"#);
369/// ```
370pub fn select_as_str(json_str: &str, path: &str) -> Result<String, JsonPathError> {
371 let json = serde_json::from_str(json_str).map_err(|e| JsonPathError::Serde(e.to_string()))?;
372 let parser = PathParser::compile(path).map_err(|e| JsonPathError::from(&e))?;
373 let ret = JsonSelector::new(parser).value(&json).select()?;
374 serde_json::to_string(&ret).map_err(|e| JsonPathError::Serde(e.to_string()))
375}
376
377/// It is the same to `select` function but it deserialize the the result as given type `T`.
378///
379/// ```rust
380/// extern crate jsonpath_lib as jsonpath;
381/// extern crate serde;
382/// #[macro_use] extern crate serde_json;
383///
384/// use serde::{Deserialize, Serialize};
385///
386/// #[derive(Deserialize, PartialEq, Debug)]
387/// struct Person {
388/// name: String,
389/// age: u8,
390/// phones: Vec<String>,
391/// }
392///
393/// let ret: Vec<Person> = jsonpath::select_as(r#"
394/// {
395/// "person":
396/// {
397/// "name": "Doe John",
398/// "age": 44,
399/// "phones": [
400/// "+44 1234567",
401/// "+44 2345678"
402/// ]
403/// }
404/// }
405/// "#, "$.person").unwrap();
406///
407/// let person = Person {
408/// name: "Doe John".to_string(),
409/// age: 44,
410/// phones: vec!["+44 1234567".to_string(), "+44 2345678".to_string()],
411/// };
412///
413/// assert_eq!(ret[0], person);
414/// ```
415pub fn select_as<T: serde::de::DeserializeOwned>(
416 json_str: &str,
417 path: &str,
418) -> Result<Vec<T>, JsonPathError> {
419 let json = serde_json::from_str(json_str).map_err(|e| JsonPathError::Serde(e.to_string()))?;
420 let parser = PathParser::compile(path).map_err(|e| JsonPathError::from(&e))?;
421 JsonSelector::new(parser).value(&json).select_as()
422}
423
424/// Delete(= replace with null) the JSON property using the jsonpath.
425///
426/// ```rust
427/// extern crate jsonpath_lib as jsonpath;
428/// #[macro_use] extern crate serde_json;
429///
430/// let json_obj = json!({
431/// "school": {
432/// "friends": [
433/// {"name": "친구1", "age": 20},
434/// {"name": "친구2", "age": 20}
435/// ]
436/// },
437/// "friends": [
438/// {"name": "친구3", "age": 30},
439/// {"name": "친구4"}
440/// ]});
441///
442/// let ret = jsonpath::delete(json_obj, "$..[?(20 == @.age)]").unwrap();
443///
444/// assert_eq!(ret, json!({
445/// "school": {
446/// "friends": [
447/// null,
448/// null
449/// ]
450/// },
451/// "friends": [
452/// {"name": "친구3", "age": 30},
453/// {"name": "친구4"}
454/// ]}));
455/// ```
456pub fn delete(value: Value, path: &str) -> Result<Value, JsonPathError> {
457 let parser = PathParser::compile(path).map_err(|e| JsonPathError::from(&e))?;
458 let mut selector = JsonSelectorMut::new(parser);
459 let value = selector.value(value).delete()?;
460 Ok(value.take().unwrap_or(Value::Null))
461}
462
463/// Select JSON properties using a jsonpath and transform the result and then replace it. via closure that implements `FnMut` you can transform the selected results.
464///
465/// ```rust
466/// extern crate jsonpath_lib as jsonpath;
467/// #[macro_use] extern crate serde_json;
468///
469/// use serde_json::Value;
470///
471/// let json_obj = json!({
472/// "school": {
473/// "friends": [
474/// {"name": "친구1", "age": 20},
475/// {"name": "친구2", "age": 20}
476/// ]
477/// },
478/// "friends": [
479/// {"name": "친구3", "age": 30},
480/// {"name": "친구4"}
481/// ]});
482///
483/// let ret = jsonpath::replace_with(json_obj, "$..[?(@.age == 20)].age", &mut |v| {
484/// let age = if let Value::Number(n) = v {
485/// n.as_u64().unwrap() * 2
486/// } else {
487/// 0
488/// };
489///
490/// Some(json!(age))
491/// }).unwrap();
492///
493/// assert_eq!(ret, json!({
494/// "school": {
495/// "friends": [
496/// {"name": "친구1", "age": 40},
497/// {"name": "친구2", "age": 40}
498/// ]
499/// },
500/// "friends": [
501/// {"name": "친구3", "age": 30},
502/// {"name": "친구4"}
503/// ]}));
504/// ```
505pub fn replace_with<F>(value: Value, path: &str, fun: &mut F) -> Result<Value, JsonPathError>
506 where
507 F: FnMut(Value) -> Option<Value>,
508{
509 let parser = PathParser::compile(path).map_err(|e| JsonPathError::from(&e))?;
510 let mut selector = JsonSelectorMut::new(parser);
511 let value = selector.value(value).replace_with(fun)?;
512 Ok(value.take().unwrap_or(Value::Null))
513}
514
515/// A pre-compiled expression.
516///
517/// Calling the select function of this struct will re-use the existing, compiled expression.
518///
519/// ## Example
520///
521/// ```rust
522/// extern crate jsonpath_lib as jsonpath;
523/// #[macro_use] extern crate serde_json;
524///
525/// let mut first_friend = jsonpath::Compiled::compile("$..friends[0]").unwrap();
526///
527/// let json_obj = json!({
528/// "school": {
529/// "friends": [
530/// {"name": "친구1", "age": 20},
531/// {"name": "친구2", "age": 20}
532/// ]
533/// },
534/// "friends": [
535/// {"name": "친구3", "age": 30},
536/// {"name": "친구4"}
537/// ]});
538///
539/// // call a first time
540///
541/// let json = first_friend.select(&json_obj).unwrap();
542///
543/// assert_eq!(json, vec![
544/// &json!({"name": "친구3", "age": 30}),
545/// &json!({"name": "친구1", "age": 20})
546/// ]);
547///
548/// // call a second time
549///
550/// let json = first_friend.select(&json_obj).unwrap();
551///
552/// assert_eq!(json, vec![
553/// &json!({"name": "친구3", "age": 30}),
554/// &json!({"name": "친구1", "age": 20})
555/// ]);
556/// ```
557#[derive(Clone, Debug)]
558#[deprecated(
559since = "0.4.0",
560note = "Please use PathCompiled."
561)]
562pub struct Compiled {
563 #[allow(deprecated)]
564 node: Node,
565}
566
567#[allow(deprecated)]
568impl Compiled {
569 /// Compile a path expression and return a compiled instance.
570 ///
571 /// If parsing the path fails, it will return an error.
572 pub fn compile(path: &str) -> Result<Self, String> {
573 let node = parser::Parser::compile(path)?;
574 Ok(Self {
575 node
576 })
577 }
578
579 /// Execute the select operation on the pre-compiled path.
580 pub fn select<'a>(&self, value: &'a Value) -> Result<Vec<&'a Value>, JsonPathError> {
581 let mut selector = Selector::default();
582 selector.compiled_path(&self.node).value(value).select()
583 }
584
585 /// Execute the delete operation on the pre-compiled path.
586 pub fn delete(&self, value: Value) -> Result<Value, JsonPathError> {
587 let mut selector = SelectorMut::default();
588 let value = selector.compiled_path(self.node.clone()).value(value).delete()?;
589 Ok(value.take().unwrap_or(Value::Null))
590 }
591
592 /// Execute the replace operation on the pre-compiled path.
593 pub fn replace_with<F>(&self, value: Value, fun: &mut F) -> Result<Value, JsonPathError>
594 where
595 F: FnMut(Value) -> Option<Value>,
596 {
597 let mut selector = SelectorMut::default();
598 let value = selector.compiled_path(self.node.clone()).value(value).replace_with(fun)?;
599 Ok(value.take().unwrap_or(Value::Null))
600 }
601}
602
603/// A pre-compiled expression.
604///
605/// Calling the select function of this struct will re-use the existing, compiled expression.
606///
607/// ## Example
608///
609/// ```rust
610/// extern crate jsonpath_lib as jsonpath;
611/// #[macro_use] extern crate serde_json;
612///
613/// let mut first_friend = jsonpath::PathCompiled::compile("$..friends[0]").unwrap();
614///
615/// let json_obj = json!({
616/// "school": {
617/// "friends": [
618/// {"name": "친구1", "age": 20},
619/// {"name": "친구2", "age": 20}
620/// ]
621/// },
622/// "friends": [
623/// {"name": "친구3", "age": 30},
624/// {"name": "친구4"}
625/// ]});
626///
627/// // call a first time
628///
629/// let json = first_friend.select(&json_obj).unwrap();
630///
631/// assert_eq!(json, vec![
632/// &json!({"name": "친구3", "age": 30}),
633/// &json!({"name": "친구1", "age": 20})
634/// ]);
635///
636/// // call a second time
637///
638/// let json = first_friend.select(&json_obj).unwrap();
639///
640/// assert_eq!(json, vec![
641/// &json!({"name": "친구3", "age": 30}),
642/// &json!({"name": "친구1", "age": 20})
643/// ]);
644/// ```
645#[derive(Clone, Debug)]
646pub struct PathCompiled<'a> {
647 parser: Rc<PathParser<'a>>,
648}
649
650impl<'a> PathCompiled<'a> {
651 /// Compile a path expression and return a compiled instance.
652 ///
653 /// If parsing the path fails, it will return an error.
654 pub fn compile(path: &str) -> Result<PathCompiled, JsonPathError> {
655 let parser = PathParser::compile(path).map_err(|e| JsonPathError::from(&e))?;
656 Ok(PathCompiled {
657 parser: Rc::new(parser)
658 })
659 }
660
661 /// Execute the select operation on the pre-compiled path.
662 pub fn select(&self, value: &'a Value) -> Result<Vec<&'a Value>, JsonPathError> {
663 let mut selector = JsonSelector::new_ref(Rc::clone(&self.parser));
664 selector.value(value).select()
665 }
666}