Skip to content

Commit 0bfb61e

Browse files
zslaytonalexcrichton
authored andcommitted
Closes #12829. Names changed for consistency, find_path optimized, method impls refactored to reduce repitition.
Fixed formatting, reworked find_path to use fewer Options. Removed stray tab.
1 parent d28d5b7 commit 0bfb61e

File tree

1 file changed

+24
-35
lines changed

1 file changed

+24
-35
lines changed

src/libserialize/json.rs

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -720,10 +720,17 @@ impl Json {
720720
}
721721

722722
/// 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.
724724
/// Otherwise, it will return the Json value associated with the final key.
725725
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)
727734
}
728735

729736
/// If the Json value is an Object, performs a depth-first search until
@@ -752,10 +759,7 @@ impl Json {
752759

753760
/// Returns true if the Json value is an Object. Returns false otherwise.
754761
pub fn is_object<'a>(&'a self) -> bool {
755-
match self {
756-
&Object(_) => true,
757-
_ => false
758-
}
762+
self.as_object().is_some()
759763
}
760764

761765
/// If the Json value is an Object, returns the associated TreeMap.
@@ -769,10 +773,7 @@ impl Json {
769773

770774
/// Returns true if the Json value is a List. Returns false otherwise.
771775
pub fn is_list<'a>(&'a self) -> bool {
772-
match self {
773-
&List(_) => true,
774-
_ => false
775-
}
776+
self.as_list().is_some()
776777
}
777778

778779
/// If the Json value is a List, returns the associated vector.
@@ -785,16 +786,13 @@ impl Json {
785786
}
786787

787788
/// 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()
793791
}
794792

795793
/// If the Json value is a String, returns the associated str.
796794
/// 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> {
798796
match *self {
799797
String(ref s) => Some(s.as_slice()),
800798
_ => None
@@ -803,10 +801,7 @@ impl Json {
803801

804802
/// Returns true if the Json value is a Number. Returns false otherwise.
805803
pub fn is_number(&self) -> bool {
806-
match self {
807-
&Number(_) => true,
808-
_ => false
809-
}
804+
self.as_number().is_some()
810805
}
811806

812807
/// If the Json value is a Number, returns the associated f64.
@@ -820,10 +815,7 @@ impl Json {
820815

821816
/// Returns true if the Json value is a Boolean. Returns false otherwise.
822817
pub fn is_boolean(&self) -> bool {
823-
match self {
824-
&Boolean(_) => true,
825-
_ => false
826-
}
818+
self.as_boolean().is_some()
827819
}
828820

829821
/// If the Json value is a Boolean, returns the associated bool.
@@ -837,10 +829,7 @@ impl Json {
837829

838830
/// Returns true if the Json value is a Null. Returns false otherwise.
839831
pub fn is_null(&self) -> bool {
840-
match self {
841-
&Null => true,
842-
_ => false
843-
}
832+
self.as_null().is_some()
844833
}
845834

846835
/// If the Json value is a Null, returns ().
@@ -2430,20 +2419,20 @@ mod tests {
24302419
fn test_find(){
24312420
let json_value = from_str("{\"dog\" : \"cat\"}").unwrap();
24322421
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");
24342423
}
24352424
24362425
#[test]
24372426
fn test_find_path(){
24382427
let json_value = from_str("{\"dog\":{\"cat\": {\"mouse\" : \"cheese\"}}}").unwrap();
24392428
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");
24412430
}
24422431
24432432
#[test]
24442433
fn test_search(){
24452434
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());
24472436
assert!(found_str.is_some());
24482437
assert!(found_str.unwrap() == &"cheese");
24492438
}
@@ -2476,15 +2465,15 @@ mod tests {
24762465
}
24772466
24782467
#[test]
2479-
fn test_is_str(){
2468+
fn test_is_string(){
24802469
let json_value = from_str("\"dog\"").unwrap();
2481-
assert!(json_value.is_str());
2470+
assert!(json_value.is_string());
24822471
}
24832472

24842473
#[test]
2485-
fn test_as_str(){
2474+
fn test_as_string(){
24862475
let json_value = from_str("\"dog\"").unwrap();
2487-
let json_str = json_value.as_str();
2476+
let json_str = json_value.as_string();
24882477
let expected_str = &"dog";
24892478
assert_eq!(json_str, Some(expected_str));
24902479
}

0 commit comments

Comments
 (0)