jsonpath-rust 1.0.4

The library provides the basic functionality to find the set of the data according to the filtering query.
Documentation
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
use crate::parser::errors::JsonPathError;
use crate::parser::model::{JpQuery, Segment, Selector};
use crate::parser::{parse_json_path, Parsed};
use crate::query::{Queried, QueryPath};
use crate::JsonPath;
use serde_json::Value;
use std::borrow::Cow;
use std::fmt::Debug;

/// A trait that abstracts JSON-like data structures for JSONPath queries
///
/// This trait provides the essential operations needed to traverse and query
/// hierarchical data structures in a JSONPath-compatible way. Implementors of
/// this trait can be used with the JSONPath query engine.
///
/// The trait requires several standard type conversions to be implemented to
/// ensure that query operations can properly handle various data types.
///
/// # Type Requirements
///
/// Implementing types must satisfy these trait bounds:
/// - `Default`: Provides a default value for the type
/// - `Clone`: Allows creation of copies of values
/// - `Debug`: Enables debug formatting
/// - `From<&str>`: Conversion from string slices
/// - `From<bool>`: Conversion from boolean values
/// - `From<i64>`: Conversion from 64-bit integers
/// - `From<f64>`: Conversion from 64-bit floating point values
/// - `From<Vec<Self>>`: Conversion from vectors of the same type
/// - `From<String>`: Conversion from owned strings
/// - `PartialEq`: Allows equality comparisons
///
/// # Examples
///
/// The trait is primarily implemented for `serde_json::Value` to enable
/// JSONPath queries on JSON data structures:
///
/// ```
/// use serde_json::json;
/// use jsonpath_rust::JsonPath;
///
/// let data = json!({
///     "store": {
///         "books": [
///             {"title": "Book 1", "price": 10},
///             {"title": "Book 2", "price": 15}
///         ]
///     }
/// });
///
/// // Access data using the Queryable trait
/// let books = data.query("$.store.books[*].title").expect("no errors");
/// ```
pub trait Queryable
where
    Self: Default
        + Clone
        + Debug
        + for<'a> From<&'a str>
        + From<bool>
        + From<i64>
        + From<f64>
        + From<Vec<Self>>
        + From<String>
        + PartialEq,
{
    /// Retrieves a reference to the value associated with the given key.
    /// It is the responsibility of the implementation to handle enclosing single and double quotes.
    /// The key will be normalized (quotes trimmed, whitespace handled, the escape symbols handled) before lookup.
    fn get(&self, key: &str) -> Option<&Self>;

    fn as_array(&self) -> Option<&Vec<Self>>;

    fn as_object(&self) -> Option<Vec<(&str, &Self)>>;

    fn as_str(&self) -> Option<&str>;

    fn as_i64(&self) -> Option<i64>;
    fn as_f64(&self) -> Option<f64>;
    fn as_bool(&self) -> Option<bool>;

    /// Returns a null value.
    fn null() -> Self;

    fn extension_custom(_name: &str, _args: Vec<Cow<Self>>) -> Self {
        Self::null()
    }

    /// Retrieves a reference to the element at the specified path.
    /// The path is specified as a string and can be obtained from the query.
    ///
    /// # Arguments
    /// * `path` -  A json path to the element specified as a string (root, field, index only).
    fn reference<T>(&self, _path: T) -> Option<&Self>
    where
        T: Into<QueryPath>,
    {
        None
    }

    /// Retrieves a mutable reference to the element at the specified path.
    ///
    /// # Arguments
    /// * `path` -  A json path to the element specified as a string (root, field, index only).
    ///
    /// # Examples
    ///
    /// ```
    /// use serde_json::json;
    /// use jsonpath_rust::JsonPath;
    /// use jsonpath_rust::query::queryable::Queryable;
    /// let mut json = json!({
    ///             "a": {
    ///                 "b": {
    ///                     "c": 42
    ///                 }
    ///             }
    ///         });
    ///         if let Some(path) = json.query_only_path("$.a.b.c").unwrap().first() {
    ///             if let Some(v) = json.reference_mut("$.a.b.c") {
    ///                 *v = json!(43);
    ///             }
    ///
    ///             assert_eq!(
    ///                 json,
    ///                 json!({
    ///                     "a": {
    ///                         "b": {
    ///                             "c": 43
    ///                         }
    ///                     }
    ///                 })
    ///             );
    /// }
    //// ```
    fn reference_mut<T>(&mut self, _path: T) -> Option<&mut Self>
    where
        T: Into<QueryPath>,
    {
        None
    }

    /// Deletes all elements matching the given JSONPath
    ///
    /// # Arguments
    /// * `path` - JSONPath string specifying elements to delete
    ///
    /// # Returns
    /// * `Ok(usize)` - Number of elements deleted
    /// * `Err(JsonPathError)` - If the path is invalid or deletion fails
    ///
    /// # Examples
    /// ```
    /// use serde_json::json;
    /// use jsonpath_rust::JsonPath;
    /// use crate::jsonpath_rust::query::queryable::Queryable;
    ///
    /// let mut data = json!({
    ///     "users": [
    ///         {"name": "Alice", "age": 30},
    ///         {"name": "Bob", "age": 25},
    ///         {"name": "Charlie", "age": 35}
    ///     ]
    /// });
    ///
    /// // Delete users older than 30
    /// let deleted = data.delete_by_path("$.users[?(@.age > 30)]").unwrap();
    /// assert_eq!(deleted, 1);
    /// ```
    fn delete_by_path(&mut self, _path: &str) -> Queried<usize> {
        Err(JsonPathError::InvalidJsonPath(
            "Deletion not supported".to_string(),
        ))
    }
}

impl Queryable for Value {
    fn get(&self, key: &str) -> Option<&Self> {
        let key = if key.starts_with("'") && key.ends_with("'") {
            key.trim_matches(|c| c == '\'')
        } else if key.starts_with('"') && key.ends_with('"') {
            key.trim_matches(|c| c == '"')
        } else {
            key
        };
        self.get(key)
    }

    fn as_array(&self) -> Option<&Vec<Self>> {
        self.as_array()
    }

    fn as_object(&self) -> Option<Vec<(&str, &Self)>> {
        self.as_object()
            .map(|v| v.into_iter().map(|(k, v)| (k.as_str(), v)).collect())
    }

    fn as_str(&self) -> Option<&str> {
        self.as_str()
    }

    fn as_i64(&self) -> Option<i64> {
        self.as_i64()
    }

    fn as_f64(&self) -> Option<f64> {
        self.as_f64()
    }

    fn as_bool(&self) -> Option<bool> {
        self.as_bool()
    }

    fn null() -> Self {
        Value::Null
    }

    /// Custom extension function for JSONPath queries.
    ///
    /// This function allows for custom operations to be performed on JSON data
    /// based on the provided `name` and `args`.
    ///
    /// # Arguments
    ///
    /// * `name` - A string slice that holds the name of the custom function.
    /// * `args` - A vector of `Cow<Self>` that holds the arguments for the custom function.
    ///
    /// # Returns
    ///
    /// Returns a `Self` value which is the result of the custom function. If the function
    /// name is not recognized, it returns `Self::null()`.
    ///
    /// # Custom Functions
    ///
    /// * `"in"` - Checks if the first argument is in the array provided as the second argument.
    ///   Example: `$.elems[?in(@, $.list)]` - Returns elements from $.elems that are present in $.list
    ///
    /// * `"nin"` - Checks if the first argument is not in the array provided as the second argument.
    ///   Example: `$.elems[?nin(@, $.list)]` - Returns elements from $.elems that are not present in $.list
    ///
    /// * `"none_of"` - Checks if none of the elements in the first array are in the second array.
    ///   Example: `$.elems[?none_of(@, $.list)]` - Returns arrays from $.elems that have no elements in common with $.list
    ///
    /// * `"any_of"` - Checks if any of the elements in the first array are in the second array.
    ///   Example: `$.elems[?any_of(@, $.list)]` - Returns arrays from $.elems that have at least one element in common with $.list
    ///
    /// * `"subset_of"` - Checks if all elements in the first array are in the second array.
    ///   Example: `$.elems[?subset_of(@, $.list)]` - Returns arrays from $.elems where all elements are present in $.list
    fn extension_custom(name: &str, args: Vec<Cow<Self>>) -> Self {
        match name {
            "in" => match args.as_slice() {
                [lhs, rhs] => match rhs.as_array() {
                    Some(elements) => elements.iter().any(|item| item == lhs.as_ref()).into(),
                    None => Self::null(),
                },
                _ => Self::null(),
            },
            "nin" => match args.as_slice() {
                [lhs, rhs] => match rhs.as_array() {
                    Some(elements) => (!elements.iter().any(|item| item == lhs.as_ref())).into(),
                    None => Self::null(),
                },
                _ => Self::null(),
            },
            "none_of" => match args.as_slice() {
                [lhs, rhs] => match (lhs.as_array(), rhs.as_array()) {
                    (Some(lhs_arr), Some(rhs_arr)) => lhs_arr
                        .iter()
                        .all(|lhs| !rhs_arr.iter().any(|rhs| lhs == rhs))
                        .into(),
                    _ => Self::null(),
                },
                _ => Self::null(),
            },
            "any_of" => match args.as_slice() {
                [lhs, rhs] => match (lhs.as_array(), rhs.as_array()) {
                    (Some(lhs_arr), Some(rhs_arr)) => lhs_arr
                        .iter()
                        .any(|lhs| rhs_arr.iter().any(|rhs| lhs == rhs))
                        .into(),
                    _ => Self::null(),
                },
                _ => Self::null(),
            },
            "subset_of" => match args.as_slice() {
                [lhs, rhs] => match (lhs.as_array(), rhs.as_array()) {
                    (Some(lhs_arr), Some(rhs_arr)) => lhs_arr
                        .iter()
                        .all(|lhs| rhs_arr.iter().any(|rhs| lhs == rhs))
                        .into(),
                    _ => Self::null(),
                },
                _ => Self::null(),
            },
            _ => Self::null(),
        }
    }

    fn reference<T>(&self, path: T) -> Option<&Self>
    where
        T: Into<QueryPath>,
    {
        convert_js_path(&path.into())
            .ok()
            .and_then(|p| self.pointer(p.as_str()))
    }

    fn reference_mut<T>(&mut self, path: T) -> Option<&mut Self>
    where
        T: Into<QueryPath>,
    {
        convert_js_path(&path.into())
            .ok()
            .and_then(|p| self.pointer_mut(p.as_str()))
    }

    fn delete_by_path(&mut self, path: &str) -> Queried<usize> {
        let mut deletions = Vec::new();
        for query_path in &self.query_only_path(path)? {
            if let Some(deletion_info) = parse_deletion_path(query_path)? {
                deletions.push(deletion_info);
            }
        }

        // Sort deletions to handle array indices correctly (delete from end to start)
        deletions.sort_by(|a, b| {
            b.path_depth()
                .cmp(&a.path_depth())
                .then_with(|| match (a, b) {
                    (
                        DeletionInfo::ArrayIndex { index: idx_a, .. },
                        DeletionInfo::ArrayIndex { index: idx_b, .. },
                    ) => idx_b.cmp(idx_a),
                    _ => std::cmp::Ordering::Equal,
                })
        });

        // Perform deletions
        let deleted_count = deletions.iter().try_fold(0, |c, d| {
            execute_deletion(self, d).map(|deleted| if deleted { c + 1 } else { c })
        })?;

        Ok(deleted_count)
    }
}

#[derive(Debug, Clone)]
enum DeletionInfo {
    ObjectField {
        parent_path: String,
        field_name: String,
    },
    ArrayIndex {
        parent_path: String,
        index: usize,
    },
    Root,
}

impl DeletionInfo {
    fn path_depth(&self) -> usize {
        match self {
            DeletionInfo::Root => 0,
            DeletionInfo::ObjectField { parent_path, .. }
            | DeletionInfo::ArrayIndex { parent_path, .. } => parent_path.matches('/').count(),
        }
    }
}

fn parse_deletion_path(query_path: &str) -> Result<Option<DeletionInfo>, JsonPathError> {
    if query_path == "$" {
        return Ok(Some(DeletionInfo::Root));
    }

    let JpQuery { segments } = parse_json_path(query_path)?;

    if segments.is_empty() {
        return Ok(None);
    }

    let mut parent_path = String::new();
    let mut segments_iter = segments.iter().peekable();

    while let Some(segment) = segments_iter.next() {
        if segments_iter.peek().is_some() {
            // Not the last segment, add to parent path
            match segment {
                Segment::Selector(Selector::Name(name)) => {
                    parent_path.push_str(&format!("/{}", name.trim_matches(|c| c == '\'')));
                }
                Segment::Selector(Selector::Index(index)) => {
                    parent_path.push_str(&format!("/{}", index));
                }
                e => {
                    return Err(JsonPathError::InvalidJsonPath(format!(
                        "Unsupported segment to be deleted: {:?}",
                        e
                    )));
                }
            }
        } else {
            match segment {
                Segment::Selector(Selector::Name(name)) => {
                    let field_name = name.trim_matches(|c| c == '\'').to_string();
                    return Ok(Some(DeletionInfo::ObjectField {
                        parent_path,
                        field_name,
                    }));
                }
                Segment::Selector(Selector::Index(index)) => {
                    return Ok(Some(DeletionInfo::ArrayIndex {
                        parent_path,
                        index: *index as usize,
                    }));
                }
                e => {
                    return Err(JsonPathError::InvalidJsonPath(format!(
                        "Unsupported segment to be deleted: {:?}",
                        e
                    )));
                }
            }
        }
    }

    Ok(None)
}

fn execute_deletion(value: &mut Value, deletion: &DeletionInfo) -> Queried<bool> {
    match deletion {
        DeletionInfo::Root => {
            *value = Value::Null;
            Ok(true)
        }
        DeletionInfo::ObjectField {
            parent_path,
            field_name,
        } => {
            let parent = if parent_path.is_empty() {
                value
            } else {
                value.pointer_mut(parent_path).ok_or_else(|| {
                    JsonPathError::InvalidJsonPath("Parent path not found".to_string())
                })?
            };

            if let Some(obj) = parent.as_object_mut() {
                Ok(obj.remove(field_name).is_some())
            } else {
                Err(JsonPathError::InvalidJsonPath(
                    "Parent is not an object".to_string(),
                ))
            }
        }
        DeletionInfo::ArrayIndex { parent_path, index } => {
            let parent = if parent_path.is_empty() {
                value
            } else {
                value.pointer_mut(parent_path).ok_or_else(|| {
                    JsonPathError::InvalidJsonPath("Parent path not found".to_string())
                })?
            };

            if let Some(arr) = parent.as_array_mut() {
                if *index < arr.len() {
                    arr.remove(*index);
                    Ok(true)
                } else {
                    Ok(false) // Index out of bounds
                }
            } else {
                Err(JsonPathError::InvalidJsonPath(
                    "Parent is not an array".to_string(),
                ))
            }
        }
    }
}

fn convert_js_path(path: &str) -> Parsed<String> {
    let JpQuery { segments } = parse_json_path(path)?;

    let mut path = String::new();
    for segment in segments {
        match segment {
            Segment::Selector(Selector::Name(name)) => {
                path.push_str(&format!("/{}", name.trim_matches(|c| c == '\'')));
            }
            Segment::Selector(Selector::Index(index)) => {
                path.push_str(&format!("/{}", index));
            }
            s => {
                return Err(JsonPathError::InvalidJsonPath(format!(
                    "Invalid segment: {:?}",
                    s
                )));
            }
        }
    }
    Ok(path)
}

#[cfg(test)]
mod tests {
    use crate::parser::Parsed;
    use crate::query::queryable::{convert_js_path, Queryable};
    use crate::query::Queried;
    use crate::JsonPath;
    use serde_json::{json, Value};

    #[test]
    fn in_smoke() -> Queried<()> {
        let json = json!({
            "elems": ["test", "t1", "t2"],
            "list": ["test", "test2", "test3"],
        });

        let res = json.query("$.elems[?in(@, $.list)]")?;

        assert_eq!(res, [&json!("test")]);

        Ok(())
    }
    #[test]
    fn nin_smoke() -> Queried<()> {
        let json = json!({
            "elems": ["test", "t1", "t2"],
            "list": ["test", "test2", "test3"],
        });

        let res = json.query("$.elems[?nin(@, $.list)]")?;

        assert_eq!(res, [&json!("t1"), &json!("t2")]);

        Ok(())
    }
    #[test]
    fn none_of_smoke() -> Queried<()> {
        let json = json!({
            "elems": [  ["t1", "_"], ["t2", "t5"], ["t4"]],
            "list": ["t1","t2", "t3"],
        });

        let res = json.query("$.elems[?none_of(@, $.list)]")?;

        assert_eq!(res, [&json!(["t4"])]);

        Ok(())
    }
    #[test]
    fn any_of_smoke() -> Queried<()> {
        let json = json!({
            "elems": [  ["t1", "_"], ["t4", "t5"], ["t4"]],
            "list": ["t1","t2", "t3"],
        });

        let res = json.query("$.elems[?any_of(@, $.list)]")?;

        assert_eq!(res, [&json!(["t1", "_"])]);

        Ok(())
    }
    #[test]
    fn subset_of_smoke() -> Queried<()> {
        let json = json!({
            "elems": [  ["t1", "t2"], ["t4", "t5"], ["t6"]],
            "list": ["t1","t2", "t3"],
        });

        let res = json.query("$.elems[?subset_of(@, $.list)]")?;

        assert_eq!(res, [&json!(["t1", "t2"])]);

        Ok(())
    }

    #[test]
    fn convert_paths() -> Parsed<()> {
        let r = convert_js_path("$.a.b[2]")?;
        assert_eq!(r, "/a/b/2");

        Ok(())
    }

    #[test]
    fn test_references() -> Parsed<()> {
        let mut json = json!({
            "a": {
                "b": {
                    "c": 42
                }
            }
        });

        let r = convert_js_path("$.a.b.c")?;

        if let Some(v) = json.pointer_mut(r.as_str()) {
            *v = json!(43);
        }

        assert_eq!(
            json,
            json!({
                "a": {
                    "b": {
                        "c": 43
                    }
                }
            })
        );

        Ok(())
    }
    #[test]
    fn test_js_reference() -> Parsed<()> {
        let mut json = json!({
            "a": {
                "b": {
                    "c": 42
                }
            }
        });

        if let Some(path) = json.query_only_path("$.a.b.c")?.first() {
            if let Some(v) = json.reference_mut(path) {
                *v = json!(43);
            }

            assert_eq!(
                json,
                json!({
                    "a": {
                        "b": {
                            "c": 43
                        }
                    }
                })
            );
        } else {
            panic!("no path found");
        }

        Ok(())
    }
    #[test]
    fn test_delete_object_field() {
        let mut data = json!({
            "users": {
                "alice": {"age": 30},
                "bob": {"age": 25}
            }
        });

        let deleted = data.delete_by_path("$.users.alice").unwrap();
        assert_eq!(deleted, 1);

        let expected = json!({
            "users": {
                "bob": {"age": 25}
            }
        });
        assert_eq!(data, expected);
    }

    #[test]
    fn test_delete_array_element() {
        let mut data = json!({
            "numbers": [1, 2, 3, 4, 5]
        });

        let deleted = data.delete_by_path("$.numbers[2]").unwrap();
        assert_eq!(deleted, 1);

        let expected = json!({
            "numbers": [1, 2, 4, 5]
        });
        assert_eq!(data, expected);
    }

    #[test]
    fn test_delete_multiple_elements() {
        let mut data = json!({
            "users": [
                {"name": "Alice", "age": 30},
                {"name": "Bob", "age": 25},
                {"name": "Charlie", "age": 35},
                {"name": "David", "age": 22}
            ]
        });

        // Delete users older than 24
        let deleted = data.delete_by_path("$.users[?(@.age > 24)]").unwrap();
        assert_eq!(deleted, 3);

        let expected = json!({
            "users": [
                {"name": "David", "age": 22}
            ]
        });
        assert_eq!(data, expected);
    }

    #[test]
    fn test_delete_nested_fields() {
        let mut data = json!({
            "company": {
                "departments": {
                    "engineering": {"budget": 100000},
                    "marketing": {"budget": 50000},
                    "hr": {"budget": 30000}
                }
            }
        });

        let deleted = data
            .delete_by_path("$.company.departments.marketing")
            .unwrap();
        assert_eq!(deleted, 1);

        let expected = json!({
            "company": {
                "departments": {
                    "engineering": {"budget": 100000},
                    "hr": {"budget": 30000}
                }
            }
        });
        assert_eq!(data, expected);
    }

    #[test]
    fn test_delete_nonexistent_path() {
        let mut data = json!({
            "test": "value"
        });

        let deleted = data.delete_by_path("$.nonexistent").unwrap();
        assert_eq!(deleted, 0);

        // Data should remain unchanged
        let expected = json!({
            "test": "value"
        });
        assert_eq!(data, expected);
    }

    #[test]
    fn test_delete_root() {
        let mut data = json!({
            "test": "value"
        });

        let deleted = data.delete_by_path("$").unwrap();
        assert_eq!(deleted, 1);
        assert_eq!(data, Value::Null);
    }
}