@@ -185,32 +185,47 @@ where
185185
186186/// get_many returns multiple fields from the `PointerTree`.
187187///
188- /// The result is a `Result<Vec<LazyValue>>`. The order of the `Vec` is same as the order of the
189- /// tree.
188+ /// The result is a `Result<Vec<Option< LazyValue>>> `. The order of the `Vec` is same as the order of
189+ /// the tree.
190190///
191+ /// If a key is unknown, the value at the corresponding position in `Vec` will be None.
191192/// If json is invalid, or the field not be found, it will return a err.
192193///
193194/// # Safety
194195/// The JSON must be valid and well-formed, otherwise it may return unexpected result.
195196///
196197/// # Examples
197198/// ```
198- /// use sonic_rs::pointer;
199+ /// # use sonic_rs::pointer;
200+ /// # use sonic_rs::PointerTree;
201+ ///
199202/// let json = r#"
200- /// {"u": 123, "a": {"b" : {"c": [null, "found"]}}}"#;
203+ /// {"u": 123, "a": {"b" : {"c": [null, "found"]}}}"#;
204+ ///
201205/// // build a pointer tree, representing multiple json path
202- /// let mut tree = sonic_rs::PointerTree::new();
206+ /// let mut tree = PointerTree::new();
207+ ///
203208/// tree.add_path(&["u"]);
209+ /// tree.add_path(&["unknown_key"]);
204210/// tree.add_path(&pointer!["a", "b", "c", 1]);
205- /// let nodes = unsafe { sonic_rs::get_many_unchecked(json, &tree).unwrap() };
206- /// // the node order is as the order of `add_path`
207- /// assert_eq!(nodes[0].as_raw_str(), "123");
208- /// assert_eq!(nodes[1].as_raw_str(), "\"found\"");
211+ ///
212+ /// let nodes = unsafe { sonic_rs::get_many_unchecked(json, &tree) };
213+ ///
214+ /// match nodes {
215+ /// Ok(vals) => {
216+ /// assert_eq!(vals[0].as_ref().unwrap().as_raw_str(), "123");
217+ /// assert!(vals[1].is_none());
218+ /// assert_eq!(vals[2].as_ref().unwrap().as_raw_str(), "\"found\"");
219+ /// }
220+ /// Err(e) => {
221+ /// panic!("err: {:?}", e)
222+ /// }
223+ /// }
209224/// ```
210225pub unsafe fn get_many_unchecked < ' de , Input > (
211226 json : Input ,
212227 tree : & PointerTree ,
213- ) -> Result < Vec < LazyValue < ' de > > >
228+ ) -> Result < Vec < Option < LazyValue < ' de > > > >
214229where
215230 Input : JsonInput < ' de > ,
216231{
@@ -401,25 +416,41 @@ where
401416
402417/// get_many returns multiple fields from the [`PointerTree`].
403418///
404- /// The result is a `Result<Vec<LazyValue>>`. The order of the `Vec` is same as the order of the
405- /// tree.
419+ /// The result is a `Result<Vec<Option<LazyValue>>>`. The order of the `Vec` is same as the order of
420+ /// the tree.
421+ ///
422+ /// If a key is unknown, the value at the corresponding position in `Vec` will be None.
406423/// If json is invalid, or the field not be found, it will return a err.
407424///
408425/// # Examples
409426/// ```
410- /// use sonic_rs::pointer;
427+ /// # use sonic_rs::pointer;
428+ /// # use sonic_rs::PointerTree;
429+ ///
411430/// let json = r#"
412- /// {"u": 123, "a": {"b" : {"c": [null, "found"]}}}"#;
431+ /// {"u": 123, "a": {"b" : {"c": [null, "found"]}}}"#;
432+ ///
413433/// // build a pointer tree, representing multiple json path
414- /// let mut tree = sonic_rs::PointerTree::new();
434+ /// let mut tree = PointerTree::new();
435+ ///
415436/// tree.add_path(&["u"]);
437+ /// tree.add_path(&["unknown_key"]);
416438/// tree.add_path(&pointer!["a", "b", "c", 1]);
417- /// let nodes = sonic_rs::get_many(json, &tree).unwrap();
418- /// // the node order is as the order of `add_path`
419- /// assert_eq!(nodes[0].as_raw_str(), "123");
420- /// assert_eq!(nodes[1].as_raw_str(), "\"found\"");
439+ ///
440+ /// let nodes = unsafe { sonic_rs::get_many(json, &tree) };
441+ ///
442+ /// match nodes {
443+ /// Ok(vals) => {
444+ /// assert_eq!(vals[0].as_ref().unwrap().as_raw_str(), "123");
445+ /// assert!(vals[1].is_none());
446+ /// assert_eq!(vals[2].as_ref().unwrap().as_raw_str(), "\"found\"");
447+ /// }
448+ /// Err(e) => {
449+ /// panic!("err: {:?}", e)
450+ /// }
451+ /// }
421452/// ```
422- pub fn get_many < ' de , Input > ( json : Input , tree : & PointerTree ) -> Result < Vec < LazyValue < ' de > > >
453+ pub fn get_many < ' de , Input > ( json : Input , tree : & PointerTree ) -> Result < Vec < Option < LazyValue < ' de > > > >
423454where
424455 Input : JsonInput < ' de > ,
425456{
@@ -599,7 +630,8 @@ mod test {
599630 tree. add_path ( pointer ! [ "a" ] . iter ( ) ) ; // 4
600631 tree. add_path ( pointer ! [ "b" , 2 ] . iter ( ) ) ; // 5
601632 tree. add_path ( pointer ! [ ] . iter ( ) ) ; // 6
602- assert_eq ! ( tree. size( ) , 7 ) ;
633+ tree. add_path ( pointer ! [ "unknown_key" ] . iter ( ) ) ; // 7
634+ assert_eq ! ( tree. size( ) , 8 ) ;
603635 tree
604636 }
605637
@@ -622,18 +654,22 @@ mod test {
622654 test_many_ok ( unsafe { get_many_unchecked ( & json, & tree) . unwrap ( ) } ) ;
623655 test_many_ok ( get_many ( & json, & tree) . unwrap ( ) ) ;
624656
625- fn test_many_ok ( many : Vec < LazyValue < ' _ > > ) {
626- assert_eq ! ( many[ 0 ] . as_raw_str( ) , "\" hello, world!\" " ) ;
657+ fn test_many_ok ( many : Vec < Option < LazyValue < ' _ > > > ) {
658+ assert_eq ! ( many[ 0 ] . as_ref ( ) . unwrap ( ) . as_raw_str( ) , "\" hello, world!\" " ) ;
627659 assert_eq ! (
628- many[ 1 ] . as_raw_str( ) ,
660+ many[ 1 ] . as_ref ( ) . unwrap ( ) . as_raw_str( ) ,
629661 "{\n \" a_b_c\" :\" hello, world!\" \n }"
630662 ) ;
631- assert_eq ! ( many[ 2 ] . as_raw_str( ) , "1" ) ;
632- assert_eq ! ( many[ 3 ] . as_raw_str( ) , "{\n \" a_b\" :{\n \" a_b_c\" :\" hello, world!\" \n },\n \" a_a\" : [0, 1, 2]\n }" ) ;
633- assert_eq ! ( many[ 4 ] . as_raw_str( ) , many[ 3 ] . as_raw_str( ) ) ;
634- assert_eq ! ( many[ 5 ] . as_raw_str( ) , "true" ) ;
663+ assert_eq ! ( many[ 2 ] . as_ref( ) . unwrap( ) . as_raw_str( ) , "1" ) ;
664+ assert_eq ! ( many[ 3 ] . as_ref( ) . unwrap( ) . as_raw_str( ) , "{\n \" a_b\" :{\n \" a_b_c\" :\" hello, world!\" \n },\n \" a_a\" : [0, 1, 2]\n }" ) ;
665+ assert_eq ! (
666+ many[ 4 ] . as_ref( ) . unwrap( ) . as_raw_str( ) ,
667+ many[ 3 ] . as_ref( ) . unwrap( ) . as_raw_str( )
668+ ) ;
669+ assert_eq ! ( many[ 5 ] . as_ref( ) . unwrap( ) . as_raw_str( ) , "true" ) ;
635670 // we have strip the leading or trailing spaces
636- assert_eq ! ( many[ 6 ] . as_raw_str( ) , "{\n \" b\" : [0, 1, true],\n \" a\" : {\n \" a_b\" :{\n \" a_b_c\" :\" hello, world!\" \n },\n \" a_a\" : [0, 1, 2]\n }\n }" ) ;
671+ assert_eq ! ( many[ 6 ] . as_ref( ) . unwrap( ) . as_raw_str( ) , "{\n \" b\" : [0, 1, true],\n \" a\" : {\n \" a_b\" :{\n \" a_b_c\" :\" hello, world!\" \n },\n \" a_a\" : [0, 1, 2]\n }\n }" ) ;
672+ assert ! ( many[ 7 ] . is_none( ) )
637673 }
638674 }
639675}
0 commit comments