mirror of
https://github.com/serde-rs/json.git
synced 2025-10-03 07:46:05 +00:00
Remove crufty Value methods
This commit is contained in:
parent
38a7682765
commit
56d58eb3e6
@ -77,31 +77,6 @@ fn parse_index(s: &str) -> Option<usize> {
|
||||
}
|
||||
|
||||
impl Value {
|
||||
/// If the `Value` is an Object, returns the value associated with the provided key.
|
||||
/// Otherwise, returns None.
|
||||
pub fn find<'a>(&'a self, key: &str) -> Option<&'a Value> {
|
||||
match *self {
|
||||
Value::Object(ref map) => map.get(key),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Attempts to get a nested Value Object for each key in `keys`.
|
||||
/// If any key is found not to exist, find_path will return None.
|
||||
/// Otherwise, it will return the `Value` associated with the final key.
|
||||
pub fn find_path<'a>(&'a self, keys: &[&str]) -> Option<&'a Value> {
|
||||
let mut target = self;
|
||||
for key in keys {
|
||||
match target.find(key) {
|
||||
Some(t) => {
|
||||
target = t;
|
||||
}
|
||||
None => return None,
|
||||
}
|
||||
}
|
||||
Some(target)
|
||||
}
|
||||
|
||||
/// Looks up a value by a JSON Pointer.
|
||||
///
|
||||
/// JSON Pointer defines a string syntax for identifying a specific value
|
||||
@ -204,29 +179,6 @@ impl Value {
|
||||
Some(target)
|
||||
}
|
||||
|
||||
/// If the `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 `Value` is not an Object, returns None.
|
||||
pub fn search<'a>(&'a self, key: &str) -> Option<&'a Value> {
|
||||
match *self {
|
||||
Value::Object(ref map) => {
|
||||
match map.get(key) {
|
||||
Some(json_value) => Some(json_value),
|
||||
None => {
|
||||
for (_, v) in map.iter() {
|
||||
match v.search(key) {
|
||||
x if x.is_some() => return x,
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the `Value` is an Object. Returns false otherwise.
|
||||
pub fn is_object(&self) -> bool {
|
||||
self.as_object().is_some()
|
||||
|
@ -1276,15 +1276,6 @@ fn test_missing_renamed_field() {
|
||||
assert_eq!(value, Foo { x: Some(5) });
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_path() {
|
||||
let obj: Value = from_str(r#"{"x": {"a": 1}, "y": 2}"#).unwrap();
|
||||
|
||||
assert!(obj.find_path(&["x", "a"]).unwrap() == &1.into());
|
||||
assert!(obj.find_path(&["y"]).unwrap() == &2.into());
|
||||
assert!(obj.find_path(&["z"]).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_serialize_seq_with_no_len() {
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
|
Loading…
x
Reference in New Issue
Block a user