rollup merge of #18544 : whataloadofwhat/json
This commit is contained in:
commit
dbb9c99911
1 changed files with 39 additions and 17 deletions
|
@ -203,6 +203,7 @@ use std::num::{FPNaN, FPInfinite};
|
|||
use std::str::ScalarValue;
|
||||
use std::string;
|
||||
use std::vec::Vec;
|
||||
use std::ops;
|
||||
|
||||
use Encodable;
|
||||
|
||||
|
@ -889,9 +890,9 @@ impl Json {
|
|||
|
||||
/// If the Json value is an Object, returns the value associated with the provided key.
|
||||
/// Otherwise, returns None.
|
||||
pub fn find<'a>(&'a self, key: &string::String) -> Option<&'a Json>{
|
||||
pub fn find<'a>(&'a self, key: &str) -> Option<&'a Json>{
|
||||
match self {
|
||||
&Object(ref map) => map.find(key),
|
||||
&Object(ref map) => map.find_with(|s| key.cmp(&s.as_slice())),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
@ -899,7 +900,7 @@ impl Json {
|
|||
/// Attempts to get a nested Json Object for each key in `keys`.
|
||||
/// If any key is found not to exist, find_path will return None.
|
||||
/// Otherwise, it will return the Json value associated with the final key.
|
||||
pub fn find_path<'a>(&'a self, keys: &[&string::String]) -> Option<&'a Json>{
|
||||
pub fn find_path<'a>(&'a self, keys: &[&str]) -> Option<&'a Json>{
|
||||
let mut target = self;
|
||||
for key in keys.iter() {
|
||||
match target.find(*key) {
|
||||
|
@ -913,20 +914,19 @@ impl Json {
|
|||
/// If the Json value is an Object, performs a depth-first search until
|
||||
/// a value associated with the provided key is found. If no value is found
|
||||
/// or the Json value is not an Object, returns None.
|
||||
pub fn search<'a>(&'a self, key: &string::String) -> Option<&'a Json> {
|
||||
pub fn search<'a>(&'a self, key: &str) -> Option<&'a Json> {
|
||||
match self {
|
||||
&Object(ref map) => {
|
||||
match map.find(key) {
|
||||
match map.find_with(|s| key.cmp(&s.as_slice())) {
|
||||
Some(json_value) => Some(json_value),
|
||||
None => {
|
||||
let mut value : Option<&'a Json> = None;
|
||||
for (_, v) in map.iter() {
|
||||
value = v.search(key);
|
||||
if value.is_some() {
|
||||
break;
|
||||
match v.search(key) {
|
||||
x if x.is_some() => return x,
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
value
|
||||
None
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -1068,6 +1068,21 @@ impl Json {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> ops::Index<&'a str, Json> for Json {
|
||||
fn index<'a>(&'a self, idx: & &str) -> &'a Json {
|
||||
self.find(*idx).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl ops::Index<uint, Json> for Json {
|
||||
fn index<'a>(&'a self, idx: &uint) -> &'a Json {
|
||||
match self {
|
||||
&List(ref v) => v.index(idx),
|
||||
_ => panic!("can only index Json with uint if it is a list")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The output of the streaming parser.
|
||||
#[deriving(PartialEq, Clone, Show)]
|
||||
pub enum JsonEvent {
|
||||
|
@ -3089,26 +3104,33 @@ mod tests {
|
|||
#[test]
|
||||
fn test_find(){
|
||||
let json_value = from_str("{\"dog\" : \"cat\"}").unwrap();
|
||||
let found_str = json_value.find(&"dog".to_string());
|
||||
assert!(found_str.is_some() && found_str.unwrap().as_string().unwrap() == "cat");
|
||||
let found_str = json_value.find("dog");
|
||||
assert!(found_str.unwrap().as_string().unwrap() == "cat");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_path(){
|
||||
let json_value = from_str("{\"dog\":{\"cat\": {\"mouse\" : \"cheese\"}}}").unwrap();
|
||||
let found_str = json_value.find_path(&[&"dog".to_string(),
|
||||
&"cat".to_string(), &"mouse".to_string()]);
|
||||
assert!(found_str.is_some() && found_str.unwrap().as_string().unwrap() == "cheese");
|
||||
let found_str = json_value.find_path(&["dog", "cat", "mouse"]);
|
||||
assert!(found_str.unwrap().as_string().unwrap() == "cheese");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_search(){
|
||||
let json_value = from_str("{\"dog\":{\"cat\": {\"mouse\" : \"cheese\"}}}").unwrap();
|
||||
let found_str = json_value.search(&"mouse".to_string()).and_then(|j| j.as_string());
|
||||
assert!(found_str.is_some());
|
||||
let found_str = json_value.search("mouse").and_then(|j| j.as_string());
|
||||
assert!(found_str.unwrap() == "cheese");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_index(){
|
||||
let json_value = from_str("{\"animals\":[\"dog\",\"cat\",\"mouse\"]}").unwrap();
|
||||
let ref list = json_value["animals"];
|
||||
assert_eq!(list[0].as_string().unwrap(), "dog");
|
||||
assert_eq!(list[1].as_string().unwrap(), "cat");
|
||||
assert_eq!(list[2].as_string().unwrap(), "mouse");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_object(){
|
||||
let json_value = from_str("{}").unwrap();
|
||||
|
|
Loading…
Reference in a new issue