1
1
use crate :: path:: json_path_instance;
2
+ use crate :: path:: JsonLike ;
2
3
use crate :: JsonPath ;
3
4
use crate :: JsonPathValue ;
4
5
use crate :: JsonPathValue :: NoValue ;
5
6
use crate :: JsonPtr ;
6
- use crate :: Value ;
7
7
8
- impl JsonPath {
8
+ impl < T > JsonPath < T >
9
+ where
10
+ T : JsonLike ,
11
+ {
9
12
/// finds a slice of data in the set json.
10
13
/// The result is a vector of references to the incoming structure.
11
14
///
@@ -29,12 +32,11 @@ impl JsonPath {
29
32
/// vec![JsonPathValue::Slice(&expected_value, expected_path)]
30
33
/// );
31
34
/// ```
32
- pub fn find_slice < ' a > ( & ' a self , json : & ' a Value ) -> Vec < JsonPathValue < ' a , Value > > {
35
+ pub fn find_slice < ' a > ( & ' a self , json : & ' a T ) -> Vec < JsonPathValue < ' a , T > > {
33
36
use crate :: path:: Path ;
34
37
let instance = json_path_instance ( self , json) ;
35
38
let res = instance. find ( JsonPathValue :: from_root ( json) ) ;
36
- let has_v: Vec < JsonPathValue < ' _ , Value > > =
37
- res. into_iter ( ) . filter ( |v| v. has_value ( ) ) . collect ( ) ;
39
+ let has_v: Vec < JsonPathValue < ' _ , T > > = res. into_iter ( ) . filter ( |v| v. has_value ( ) ) . collect ( ) ;
38
40
39
41
if has_v. is_empty ( ) {
40
42
vec ! [ NoValue ]
@@ -45,7 +47,7 @@ impl JsonPath {
45
47
46
48
/// like [`Self::find_slice`] but returns a vector of [`JsonPtr`], which has no [`JsonPathValue::NoValue`].
47
49
/// if there is no match, it will return an empty vector
48
- pub fn find_slice_ptr < ' a > ( & ' a self , json : & ' a Value ) -> Vec < JsonPtr < ' a , Value > > {
50
+ pub fn find_slice_ptr < ' a > ( & ' a self , json : & ' a T ) -> Vec < JsonPtr < ' a , T > > {
49
51
use crate :: path:: Path ;
50
52
json_path_instance ( self , json)
51
53
. find ( JsonPathValue :: from_root ( json) )
@@ -76,13 +78,13 @@ impl JsonPath {
76
78
///
77
79
/// assert_eq!(cloned_data, Value::Array(vec![json!({"active":1})]));
78
80
/// ```
79
- pub fn find ( & self , json : & Value ) -> Value {
81
+ pub fn find ( & self , json : & T ) -> T {
80
82
let slice = self . find_slice ( json) ;
81
83
if !slice. is_empty ( ) {
82
84
if JsonPathValue :: only_no_value ( & slice) {
83
- Value :: Null
85
+ T :: null ( )
84
86
} else {
85
- Value :: Array (
87
+ T :: array (
86
88
slice
87
89
. into_iter ( )
88
90
. filter ( |v| v. has_value ( ) )
@@ -91,7 +93,7 @@ impl JsonPath {
91
93
)
92
94
}
93
95
} else {
94
- Value :: Array ( vec ! [ ] )
96
+ T :: array ( vec ! [ ] )
95
97
}
96
98
}
97
99
@@ -114,8 +116,8 @@ impl JsonPath {
114
116
/// let expected_path = "$.['first'].['second'][0]".to_string();
115
117
/// assert_eq!(slice_of_data, Value::Array(vec![Value::String(expected_path)]));
116
118
/// ```
117
- pub fn find_as_path ( & self , json : & Value ) -> Value {
118
- Value :: Array (
119
+ pub fn find_as_path ( & self , json : & T ) -> T {
120
+ T :: array (
119
121
self . find_slice ( json)
120
122
. into_iter ( )
121
123
. flat_map ( |v| v. to_path ( ) )
@@ -554,7 +556,7 @@ mod tests {
554
556
#[ test]
555
557
fn find_slice_test ( ) {
556
558
let json: Box < Value > = serde_json:: from_str ( template_json ( ) ) . expect ( "to get json" ) ;
557
- let path: Box < JsonPath > = Box :: from (
559
+ let path: Box < JsonPath < Value > > = Box :: from (
558
560
JsonPath :: try_from ( "$..book[?(@.author size 10)].title" ) . expect ( "the path is correct" ) ,
559
561
) ;
560
562
let v = path. find_slice ( & json) ;
@@ -565,7 +567,7 @@ mod tests {
565
567
#[ test]
566
568
fn find_in_array_test ( ) {
567
569
let json: Box < Value > = Box :: new ( json ! ( [ { "verb" : "TEST" } , { "verb" : "RUN" } ] ) ) ;
568
- let path: Box < JsonPath > =
570
+ let path: Box < JsonPath < Value > > =
569
571
Box :: from ( JsonPath :: try_from ( "$.[?(@.verb == 'TEST')]" ) . expect ( "the path is correct" ) ) ;
570
572
let v = path. find_slice ( & json) ;
571
573
let js = json ! ( { "verb" : "TEST" } ) ;
@@ -576,7 +578,7 @@ mod tests {
576
578
fn length_test ( ) {
577
579
let json: Box < Value > =
578
580
Box :: new ( json ! ( [ { "verb" : "TEST" } , { "verb" : "TEST" } , { "verb" : "RUN" } ] ) ) ;
579
- let path: Box < JsonPath > = Box :: from (
581
+ let path: Box < JsonPath < Value > > = Box :: from (
580
582
JsonPath :: try_from ( "$.[?(@.verb == 'TEST')].length()" ) . expect ( "the path is correct" ) ,
581
583
) ;
582
584
let v = path. find ( & json) ;
@@ -585,48 +587,48 @@ mod tests {
585
587
586
588
let json: Box < Value > =
587
589
Box :: new ( json ! ( [ { "verb" : "TEST" } , { "verb" : "TEST" } , { "verb" : "RUN" } ] ) ) ;
588
- let path: Box < JsonPath > =
590
+ let path: Box < JsonPath < Value > > =
589
591
Box :: from ( JsonPath :: try_from ( "$.length()" ) . expect ( "the path is correct" ) ) ;
590
592
assert_eq ! ( path. find( & json) , json!( [ 3 ] ) ) ;
591
593
592
594
// length of search following the wildcard returns correct result
593
595
let json: Box < Value > =
594
596
Box :: new ( json ! ( [ { "verb" : "TEST" } , { "verb" : "TEST" , "x" : 3 } , { "verb" : "RUN" } ] ) ) ;
595
- let path: Box < JsonPath > = Box :: from (
597
+ let path: Box < JsonPath < Value > > = Box :: from (
596
598
JsonPath :: try_from ( "$.[?(@.verb == 'TEST')].[*].length()" )
597
599
. expect ( "the path is correct" ) ,
598
600
) ;
599
601
assert_eq ! ( path. find( & json) , json!( [ 3 ] ) ) ;
600
602
601
603
// length of object returns 0
602
604
let json: Box < Value > = Box :: new ( json ! ( { "verb" : "TEST" } ) ) ;
603
- let path: Box < JsonPath > =
605
+ let path: Box < JsonPath < Value > > =
604
606
Box :: from ( JsonPath :: try_from ( "$.length()" ) . expect ( "the path is correct" ) ) ;
605
607
assert_eq ! ( path. find( & json) , Value :: Null ) ;
606
608
607
609
// length of integer returns null
608
610
let json: Box < Value > = Box :: new ( json ! ( 1 ) ) ;
609
- let path: Box < JsonPath > =
611
+ let path: Box < JsonPath < Value > > =
610
612
Box :: from ( JsonPath :: try_from ( "$.length()" ) . expect ( "the path is correct" ) ) ;
611
613
assert_eq ! ( path. find( & json) , Value :: Null ) ;
612
614
613
615
// length of array returns correct result
614
616
let json: Box < Value > = Box :: new ( json ! ( [ [ 1 ] , [ 2 ] , [ 3 ] ] ) ) ;
615
- let path: Box < JsonPath > =
617
+ let path: Box < JsonPath < Value > > =
616
618
Box :: from ( JsonPath :: try_from ( "$.length()" ) . expect ( "the path is correct" ) ) ;
617
619
assert_eq ! ( path. find( & json) , json!( [ 3 ] ) ) ;
618
620
619
621
// path does not exist returns length null
620
622
let json: Box < Value > =
621
623
Box :: new ( json ! ( [ { "verb" : "TEST" } , { "verb" : "TEST" } , { "verb" : "RUN" } ] ) ) ;
622
- let path: Box < JsonPath > =
624
+ let path: Box < JsonPath < Value > > =
623
625
Box :: from ( JsonPath :: try_from ( "$.not.exist.length()" ) . expect ( "the path is correct" ) ) ;
624
626
assert_eq ! ( path. find( & json) , Value :: Null ) ;
625
627
626
628
// seraching one value returns correct length
627
629
let json: Box < Value > =
628
630
Box :: new ( json ! ( [ { "verb" : "TEST" } , { "verb" : "TEST" } , { "verb" : "RUN" } ] ) ) ;
629
- let path: Box < JsonPath > = Box :: from (
631
+ let path: Box < JsonPath < Value > > = Box :: from (
630
632
JsonPath :: try_from ( "$.[?(@.verb == 'RUN')].length()" ) . expect ( "the path is correct" ) ,
631
633
) ;
632
634
@@ -637,7 +639,7 @@ mod tests {
637
639
// searching correct path following unexisting key returns length 0
638
640
let json: Box < Value > =
639
641
Box :: new ( json ! ( [ { "verb" : "TEST" } , { "verb" : "TEST" } , { "verb" : "RUN" } ] ) ) ;
640
- let path: Box < JsonPath > = Box :: from (
642
+ let path: Box < JsonPath < Value > > = Box :: from (
641
643
JsonPath :: try_from ( "$.[?(@.verb == 'RUN')].key123.length()" )
642
644
. expect ( "the path is correct" ) ,
643
645
) ;
@@ -649,7 +651,7 @@ mod tests {
649
651
// fetching first object returns length null
650
652
let json: Box < Value > =
651
653
Box :: new ( json ! ( [ { "verb" : "TEST" } , { "verb" : "TEST" } , { "verb" : "RUN" } ] ) ) ;
652
- let path: Box < JsonPath > =
654
+ let path: Box < JsonPath < Value > > =
653
655
Box :: from ( JsonPath :: try_from ( "$.[0].length()" ) . expect ( "the path is correct" ) ) ;
654
656
655
657
let v = path. find ( & json) ;
@@ -658,7 +660,7 @@ mod tests {
658
660
659
661
// length on fetching the index after search gives length of the object (array)
660
662
let json: Box < Value > = Box :: new ( json ! ( [ { "prop" : [ [ "a" , "b" , "c" ] , "d" ] } ] ) ) ;
661
- let path: Box < JsonPath > = Box :: from (
663
+ let path: Box < JsonPath < Value > > = Box :: from (
662
664
JsonPath :: try_from ( "$.[?(@.prop)].prop.[0].length()" ) . expect ( "the path is correct" ) ,
663
665
) ;
664
666
@@ -668,7 +670,7 @@ mod tests {
668
670
669
671
// length on fetching the index after search gives length of the object (string)
670
672
let json: Box < Value > = Box :: new ( json ! ( [ { "prop" : [ [ "a" , "b" , "c" ] , "d" ] } ] ) ) ;
671
- let path: Box < JsonPath > = Box :: from (
673
+ let path: Box < JsonPath < Value > > = Box :: from (
672
674
JsonPath :: try_from ( "$.[?(@.prop)].prop.[1].length()" ) . expect ( "the path is correct" ) ,
673
675
) ;
674
676
@@ -683,7 +685,7 @@ mod tests {
683
685
"field" : "field" ,
684
686
} ) ) ;
685
687
686
- let path: Box < JsonPath > =
688
+ let path: Box < JsonPath < Value > > =
687
689
Box :: from ( JsonPath :: try_from ( "$.field[1]" ) . expect ( "the path is correct" ) ) ;
688
690
let v = path. find_slice ( & json) ;
689
691
assert_eq ! ( v, vec![ NoValue ] ) ;
@@ -692,7 +694,7 @@ mod tests {
692
694
"field" : [ 0 ] ,
693
695
} ) ) ;
694
696
695
- let path: Box < JsonPath > =
697
+ let path: Box < JsonPath < Value > > =
696
698
Box :: from ( JsonPath :: try_from ( "$.field[1]" ) . expect ( "the path is correct" ) ) ;
697
699
let v = path. find_slice ( & json) ;
698
700
assert_eq ! ( v, vec![ NoValue ] ) ;
@@ -704,7 +706,7 @@ mod tests {
704
706
"field" : "field" ,
705
707
} ) ) ;
706
708
707
- let path: Box < JsonPath > =
709
+ let path: Box < JsonPath < Value > > =
708
710
Box :: from ( JsonPath :: try_from ( "$.field[?(@ == 0)]" ) . expect ( "the path is correct" ) ) ;
709
711
let v = path. find_slice ( & json) ;
710
712
assert_eq ! ( v, vec![ NoValue ] ) ;
@@ -716,7 +718,7 @@ mod tests {
716
718
"field" : [ { "f" : 1 } , { "f" : 0 } ] ,
717
719
} ) ) ;
718
720
719
- let path: Box < JsonPath > =
721
+ let path: Box < JsonPath < Value > > =
720
722
Box :: from ( JsonPath :: try_from ( "$.field[?(@.f_ == 0)]" ) . expect ( "the path is correct" ) ) ;
721
723
let v = path. find_slice ( & json) ;
722
724
assert_eq ! ( v, vec![ NoValue ] ) ;
@@ -728,7 +730,7 @@ mod tests {
728
730
"field" : [ { "f" : 1 } , { "f" : { "f_" : 1 } } ] ,
729
731
} ) ) ;
730
732
731
- let path: Box < JsonPath > =
733
+ let path: Box < JsonPath < Value > > =
732
734
Box :: from ( JsonPath :: try_from ( "$..f_" ) . expect ( "the path is correct" ) ) ;
733
735
let v = path. find_slice ( & json) ;
734
736
assert_eq ! (
@@ -743,12 +745,12 @@ mod tests {
743
745
"field" : { "field" : [ 1 ] } ,
744
746
} ) ) ;
745
747
746
- let path: Box < JsonPath > =
748
+ let path: Box < JsonPath < Value > > =
747
749
Box :: from ( JsonPath :: try_from ( "$.field_.field" ) . expect ( "the path is correct" ) ) ;
748
750
let v = path. find_slice ( & json) ;
749
751
assert_eq ! ( v, vec![ NoValue ] ) ;
750
752
751
- let path: Box < JsonPath > = Box :: from (
753
+ let path: Box < JsonPath < Value > > = Box :: from (
752
754
JsonPath :: try_from ( "$.field_.field[?(@ == 1)]" ) . expect ( "the path is correct" ) ,
753
755
) ;
754
756
let v = path. find_slice ( & json) ;
@@ -760,7 +762,7 @@ mod tests {
760
762
// searching unexisting value returns length 0
761
763
let json: Box < Value > =
762
764
Box :: new ( json ! ( [ { "verb" : "TEST" } , { "verb" : "TEST" } , { "verb" : "RUN" } ] ) ) ;
763
- let path: Box < JsonPath > = Box :: from (
765
+ let path: Box < JsonPath < Value > > = Box :: from (
764
766
JsonPath :: try_from ( "$.[?(@.verb == \" RUN1\" )]" ) . expect ( "the path is correct" ) ,
765
767
) ;
766
768
let v = path. find ( & json) ;
@@ -774,15 +776,15 @@ mod tests {
774
776
"field" : { "field" : 1 } ,
775
777
} ) ) ;
776
778
777
- let path: Box < JsonPath > =
779
+ let path: Box < JsonPath < Value > > =
778
780
Box :: from ( JsonPath :: try_from ( "$.field.field.length()" ) . expect ( "the path is correct" ) ) ;
779
781
let v = path. find_slice ( & json) ;
780
782
assert_eq ! ( v, vec![ NoValue ] ) ;
781
783
782
784
let json: Box < Value > = Box :: new ( json ! ( {
783
785
"field" : [ { "a" : 1 } , { "a" : 1 } ] ,
784
786
} ) ) ;
785
- let path: Box < JsonPath > = Box :: from (
787
+ let path: Box < JsonPath < Value > > = Box :: from (
786
788
JsonPath :: try_from ( "$.field[?(@.a == 0)].f.length()" ) . expect ( "the path is correct" ) ,
787
789
) ;
788
790
let v = path. find_slice ( & json) ;
@@ -813,14 +815,14 @@ mod tests {
813
815
fn logical_exp_test ( ) {
814
816
let json: Box < Value > = Box :: new ( json ! ( { "first" : { "second" : [ { "active" : 1 } , { "passive" : 1 } ] } } ) ) ;
815
817
816
- let path: Box < JsonPath > = Box :: from (
818
+ let path: Box < JsonPath < Value > > = Box :: from (
817
819
JsonPath :: try_from ( "$.first[?(@.does_not_exist && @.does_not_exist >= 1.0)]" )
818
820
. expect ( "the path is correct" ) ,
819
821
) ;
820
822
let v = path. find_slice ( & json) ;
821
823
assert_eq ! ( v, vec![ NoValue ] ) ;
822
824
823
- let path: Box < JsonPath > = Box :: from (
825
+ let path: Box < JsonPath < Value > > = Box :: from (
824
826
JsonPath :: try_from ( "$.first[?(@.does_not_exist >= 1.0)]" ) . expect ( "the path is correct" ) ,
825
827
) ;
826
828
let v = path. find_slice ( & json) ;
@@ -833,7 +835,7 @@ mod tests {
833
835
"author" : "abcd(Rees)" ,
834
836
} ) ) ;
835
837
836
- let path: Box < JsonPath > = Box :: from (
838
+ let path: Box < JsonPath < Value > > = Box :: from (
837
839
JsonPath :: try_from ( "$.[?(@.author ~= '(?i)d\\ (Rees\\ )')]" )
838
840
. expect ( "the path is correct" ) ,
839
841
) ;
@@ -846,7 +848,7 @@ mod tests {
846
848
#[ test]
847
849
fn logical_not_exp_test ( ) {
848
850
let json: Box < Value > = Box :: new ( json ! ( { "first" : { "second" : { "active" : 1 } } } ) ) ;
849
- let path: Box < JsonPath > = Box :: from (
851
+ let path: Box < JsonPath < Value > > = Box :: from (
850
852
JsonPath :: try_from ( "$.first[?([email protected] _not_exist >= 1.0)]" )
851
853
. expect ( "the path is correct" ) ,
852
854
) ;
@@ -859,7 +861,7 @@ mod tests {
859
861
) ]
860
862
) ;
861
863
862
- let path: Box < JsonPath > = Box :: from (
864
+ let path: Box < JsonPath < Value > > = Box :: from (
863
865
JsonPath :: try_from ( "$.first[?(!(@.does_not_exist >= 1.0))]" )
864
866
. expect ( "the path is correct" ) ,
865
867
) ;
@@ -872,7 +874,7 @@ mod tests {
872
874
) ]
873
875
) ;
874
876
875
- let path: Box < JsonPath > = Box :: from (
877
+ let path: Box < JsonPath < Value > > = Box :: from (
876
878
JsonPath :: try_from ( "$.first[?(!(@.second.active == 1) || @.second.active == 1)]" )
877
879
. expect ( "the path is correct" ) ,
878
880
) ;
@@ -885,7 +887,7 @@ mod tests {
885
887
) ]
886
888
) ;
887
889
888
- let path: Box < JsonPath > = Box :: from (
890
+ let path: Box < JsonPath < Value > > = Box :: from (
889
891
JsonPath :: try_from ( "$.first[?([email protected] == 1 && [email protected] == 1 || [email protected] == 2)]" )
890
892
. expect ( "the path is correct" ) ,
891
893
) ;
0 commit comments