@@ -720,10 +720,17 @@ impl Json {
720
720
}
721
721
722
722
/// Attempts to get a nested Json Object for each key in `keys`.
723
- /// If any key is found not to exist, get_path will return None.
723
+ /// If any key is found not to exist, find_path will return None.
724
724
/// Otherwise, it will return the Json value associated with the final key.
725
725
pub fn find_path < ' a > ( & ' a self, keys: & [ & ~str] ) -> Option <& ' a Json >{
726
- keys. iter( ) . fold( Some ( self ) , |target, key| target. map_or( None , |t| t. find( * key) ) )
726
+ let mut target = self ;
727
+ for key in keys. iter( ) {
728
+ match target. find( * key) {
729
+ Some ( t) => { target = t; } ,
730
+ None => return None
731
+ }
732
+ }
733
+ Some ( target)
727
734
}
728
735
729
736
/// If the Json value is an Object, performs a depth-first search until
@@ -752,10 +759,7 @@ impl Json {
752
759
753
760
/// Returns true if the Json value is an Object. Returns false otherwise.
754
761
pub fn is_object < ' a > ( & ' a self ) -> bool {
755
- match self {
756
- & Object ( _) => true ,
757
- _ => false
758
- }
762
+ self . as_object ( ) . is_some ( )
759
763
}
760
764
761
765
/// If the Json value is an Object, returns the associated TreeMap.
@@ -769,10 +773,7 @@ impl Json {
769
773
770
774
/// Returns true if the Json value is a List. Returns false otherwise.
771
775
pub fn is_list < ' a > ( & ' a self ) -> bool {
772
- match self {
773
- & List ( _) => true ,
774
- _ => false
775
- }
776
+ self . as_list ( ) . is_some ( )
776
777
}
777
778
778
779
/// If the Json value is a List, returns the associated vector.
@@ -785,16 +786,13 @@ impl Json {
785
786
}
786
787
787
788
/// Returns true if the Json value is a String. Returns false otherwise.
788
- pub fn is_str < ' a > ( & ' a self ) -> bool {
789
- match self {
790
- & String ( _) => true ,
791
- _ => false
792
- }
789
+ pub fn is_string < ' a > ( & ' a self ) -> bool {
790
+ self . as_string ( ) . is_some ( )
793
791
}
794
792
795
793
/// If the Json value is a String, returns the associated str.
796
794
/// Returns None otherwise.
797
- pub fn as_str < ' a > ( & ' a self ) -> Option < & ' a str > {
795
+ pub fn as_string < ' a > ( & ' a self ) -> Option < & ' a str > {
798
796
match * self {
799
797
String ( ref s) => Some ( s. as_slice ( ) ) ,
800
798
_ => None
@@ -803,10 +801,7 @@ impl Json {
803
801
804
802
/// Returns true if the Json value is a Number. Returns false otherwise.
805
803
pub fn is_number ( & self ) -> bool {
806
- match self {
807
- & Number ( _) => true ,
808
- _ => false
809
- }
804
+ self . as_number ( ) . is_some ( )
810
805
}
811
806
812
807
/// If the Json value is a Number, returns the associated f64.
@@ -820,10 +815,7 @@ impl Json {
820
815
821
816
/// Returns true if the Json value is a Boolean. Returns false otherwise.
822
817
pub fn is_boolean ( & self ) -> bool {
823
- match self {
824
- & Boolean ( _) => true ,
825
- _ => false
826
- }
818
+ self . as_boolean ( ) . is_some ( )
827
819
}
828
820
829
821
/// If the Json value is a Boolean, returns the associated bool.
@@ -837,10 +829,7 @@ impl Json {
837
829
838
830
/// Returns true if the Json value is a Null. Returns false otherwise.
839
831
pub fn is_null ( & self ) -> bool {
840
- match self {
841
- & Null => true ,
842
- _ => false
843
- }
832
+ self . as_null ( ) . is_some ( )
844
833
}
845
834
846
835
/// If the Json value is a Null, returns ().
@@ -2430,20 +2419,20 @@ mod tests {
2430
2419
fn test_find( ) {
2431
2420
let json_value = from_str( "{\" dog\" : \" cat\" }" ) . unwrap( ) ;
2432
2421
let found_str = json_value. find( & ~"dog");
2433
- assert!(found_str.is_some() && found_str.unwrap().as_str ().unwrap() == &" cat");
2422
+ assert!(found_str.is_some() && found_str.unwrap().as_string ().unwrap() == &" cat");
2434
2423
}
2435
2424
2436
2425
#[test]
2437
2426
fn test_find_path(){
2438
2427
let json_value = from_str(" { \" dog\" : { \" cat\" : { \" mouse\" : \"cheese\" } } } ").unwrap();
2439
2428
let found_str = json_value.find_path(&[&~" dog", &~" cat", &~" mouse"]);
2440
- assert!(found_str.is_some() && found_str.unwrap().as_str ().unwrap() == &" cheese");
2429
+ assert!(found_str.is_some() && found_str.unwrap().as_string ().unwrap() == &" cheese");
2441
2430
}
2442
2431
2443
2432
#[test]
2444
2433
fn test_search(){
2445
2434
let json_value = from_str(" { \" dog\" : { \" cat\" : { \" mouse\" : \"cheese\" } } } ").unwrap();
2446
- let found_str = json_value.search(&~" mouse").and_then(|j| j.as_str ());
2435
+ let found_str = json_value.search(&~" mouse").and_then(|j| j.as_string ());
2447
2436
assert!(found_str.is_some());
2448
2437
assert!(found_str.unwrap() == &" cheese");
2449
2438
}
@@ -2476,15 +2465,15 @@ mod tests {
2476
2465
}
2477
2466
2478
2467
#[test]
2479
- fn test_is_str (){
2468
+ fn test_is_string (){
2480
2469
let json_value = from_str("\" dog\" " ) . unwrap( ) ;
2481
- assert!( json_value. is_str ( ) ) ;
2470
+ assert!( json_value. is_string ( ) ) ;
2482
2471
}
2483
2472
2484
2473
#[ test]
2485
- fn test_as_str ( ) {
2474
+ fn test_as_string ( ) {
2486
2475
let json_value = from_str( "\" dog\" " ) . unwrap( ) ;
2487
- let json_str = json_value. as_str ( ) ;
2476
+ let json_str = json_value. as_string ( ) ;
2488
2477
let expected_str = & "dog" ;
2489
2478
assert_eq!( json_str, Some ( expected_str) ) ;
2490
2479
}
0 commit comments