pub struct JsonPointer { /* private fields */ }Expand description
Represents a JSON pointer.
Implementations§
Source§impl JsonPointer
impl JsonPointer
Sourcepub fn add(&self, target: &Value, value: &Value) -> Option<Value>
pub fn add(&self, target: &Value, value: &Value) -> Option<Value>
Add a value to a JSON object or array at the location specified by the JSON pointer.
let data = r#"
[
"string",
{"test": "test"},
["string"]
]"#;
let array = Value::from_str(data)?.as_array().unwrap();
assert_eq!(array.insert_string(0, "string2").ok().map(|a| Value::Array(a)),
JsonPointer::from_str("/0")
.ok()
.and_then(|p| {
p.add(&Value::Array(array.clone()), &Value::String("string2".to_string()))
}));
assert_eq!(None,
JsonPointer::from_str("/4")
.ok()
.and_then(|p| {
p.add(&Value::Array(array.clone()), &Value::String("string2".to_string()))
}));
assert_eq!(array.insert_integer(3, 0).ok().map(|a| Value::Array(a)),
JsonPointer::from_str("/-")
.ok()
.and_then(|p| p.add(&Value::Array(array.clone()), &Value::Number(Integer(0)))));
assert_eq!(array.set_object(
1,
&Object::new().add_string("test", "test").add_string("test2", "test2")
).ok().map(|a| Value::Array(a)),
JsonPointer::from_str("/1/test2")
.ok()
.and_then(|p| {
p.add(&Value::Array(array.clone()), &Value::String("test2".to_string()))
}));Sourcepub fn add_array(pointer: &str, target: &Value, value: &Array) -> Option<Value>
pub fn add_array(pointer: &str, target: &Value, value: &Array) -> Option<Value>
Add a JSON array to a JSON object or array at the location specified by the JSON pointer.
Sourcepub fn add_bool(pointer: &str, target: &Value, value: bool) -> Option<Value>
pub fn add_bool(pointer: &str, target: &Value, value: bool) -> Option<Value>
Add a Boolean value to a JSON object or array at the location specified by the JSON pointer.
Sourcepub fn add_decimal(pointer: &str, target: &Value, value: f64) -> Option<Value>
pub fn add_decimal(pointer: &str, target: &Value, value: f64) -> Option<Value>
Add a decimal value to a JSON object or array at the location specified by the JSON pointer.
Sourcepub fn add_integer(pointer: &str, target: &Value, value: i128) -> Option<Value>
pub fn add_integer(pointer: &str, target: &Value, value: i128) -> Option<Value>
Add an integer value to a JSON object or array at the location specified by the JSON pointer.
Sourcepub fn add_number(pointer: &str, target: &Value, value: Number) -> Option<Value>
pub fn add_number(pointer: &str, target: &Value, value: Number) -> Option<Value>
Add a number to a JSON object or array at the location specified by the JSON pointer.
Sourcepub fn add_object(
pointer: &str,
target: &Value,
value: &Object,
) -> Option<Value>
pub fn add_object( pointer: &str, target: &Value, value: &Object, ) -> Option<Value>
Add a JSON object to a JSON object or array at the location specified by the JSON pointer.
Sourcepub fn add_string(pointer: &str, target: &Value, value: &str) -> Option<Value>
pub fn add_string(pointer: &str, target: &Value, value: &str) -> Option<Value>
Add a string value to a JSON object or array at the location specified by the JSON pointer.
Sourcepub fn array_index(&self) -> Option<usize>
pub fn array_index(&self) -> Option<usize>
If the pointer refers to a value in an array, the index within the array is returned.
Sourcepub fn get(&self, target: &Value) -> Option<Value>
pub fn get(&self, target: &Value) -> Option<Value>
Get a value from a JSON object or array through a JSON pointer.
let data = r#"
{
"string": "string",
"int": 43,
"float": 5.8,
"boolean": true,
"object": {"test": "test"},
"array": [
"string",
1,
3.0,
false,
{"test": "test"},
[1]
],
"es/cape": true
}"#;
let object = Value::from_str(data)?;
assert_eq!(Some(Value::String("string".to_string())),
JsonPointer::from_str("/string").ok().and_then(|p| p.get(&object)));
assert_eq!(Some(Value::String("test".to_string())),
JsonPointer::from_str("/object/test").ok().and_then(|p| p.get(&object)));
assert_eq!(None, JsonPointer::from_str("/object/test2").ok().and_then(|p| p.get(&object)));
assert_eq!(None, JsonPointer::from_str("/object2/test").ok().and_then(|p| p.get(&object)));
assert_eq!(Some(Value::String("test".to_string())),
JsonPointer::from_str("/array/4/test").ok().and_then(|p| p.get(&object)));
assert_eq!(None, JsonPointer::from_str("/array/3/test").ok().and_then(|p| p.get(&object)));
assert_eq!(None, JsonPointer::from_str("/array2/4/test").ok().and_then(|p| p.get(&object)));
assert_eq!(Some(Value::Bool(true)),
JsonPointer::from_str("/es~1cape").ok().and_then(|p| p.get(&object)));
assert_eq!(Some(object.clone()), JsonPointer::from_str("/").ok().and_then(|p| p.get(&object)));Sourcepub fn get_array(pointer: &str, target: &Value) -> Option<Array>
pub fn get_array(pointer: &str, target: &Value) -> Option<Array>
Get a value from a JSON object or array through a JSON pointer if it is an array.
Otherwise, None is returned.
Sourcepub fn get_bool(pointer: &str, target: &Value) -> Option<bool>
pub fn get_bool(pointer: &str, target: &Value) -> Option<bool>
Get a value from a JSON object or array through a JSON pointer if it is a Boolean.
Otherwise, None is returned.
Sourcepub fn get_decimal(pointer: &str, target: &Value) -> Option<f64>
pub fn get_decimal(pointer: &str, target: &Value) -> Option<f64>
Get a value from a JSON object or array through a JSON pointer if it is a decimal.
Otherwise, None is returned.
Sourcepub fn get_integer(pointer: &str, target: &Value) -> Option<i128>
pub fn get_integer(pointer: &str, target: &Value) -> Option<i128>
Get a value from a JSON object or array through a JSON pointer if it is an integer.
Otherwise, None is returned.
Sourcepub fn get_number(pointer: &str, target: &Value) -> Option<Number>
pub fn get_number(pointer: &str, target: &Value) -> Option<Number>
Get a value from a JSON object or array through a JSON pointer if it is a number.
Otherwise, None is returned.
Sourcepub fn get_object(pointer: &str, target: &Value) -> Option<Object>
pub fn get_object(pointer: &str, target: &Value) -> Option<Object>
Get a value from a JSON object or array through a JSON pointer if it is an object.
Otherwise, None is returned.
Sourcepub fn get_string(pointer: &str, target: &Value) -> Option<String>
pub fn get_string(pointer: &str, target: &Value) -> Option<String>
Get a value from a JSON object or array through a JSON pointer if it is a string.
Otherwise, None is returned.
Sourcepub fn remove(&self, target: &Value) -> Option<Value>
pub fn remove(&self, target: &Value) -> Option<Value>
Remove a value in a JSON object or array at the location specified by the JSON pointer.
let data = r#"
[
"string",
{"test": "test", "test2": "test2"},
["string"]
]"#;
let array = Value::from_str(data)?.as_array().unwrap();
assert_eq!(array.remove(0).ok().map(|a| Value::Array(a)),
JsonPointer::from_str("/0")
.ok()
.and_then(|p| p.remove(&Value::Array(array.clone()))));
assert_eq!(None,
JsonPointer::from_str("/4")
.ok()
.and_then(|p| p.remove(&Value::Array(array.clone()))));
assert_eq!(array.set_object(1, &Object::new().add_string("test", "test"))
.ok().map(|a| Value::Array(a)),
JsonPointer::from_str("/1/test2")
.ok()
.and_then(|p| p.remove(&Value::Array(array.clone()))));Sourcepub fn set(&self, target: &Value, value: &Value) -> Option<Value>
pub fn set(&self, target: &Value, value: &Value) -> Option<Value>
Set a value in a JSON object or array at the location specified by the JSON pointer.
let data = r#"
[
"string",
{"test": "test"},
["string"]
]"#;
let array = Value::from_str(data)?.as_array().unwrap();
assert_eq!(array.set_string(0, "string2").ok().map(|a| Value::Array(a)),
JsonPointer::from_str("/0")
.ok()
.and_then(|p| {
p.set(&Value::Array(array.clone()), &Value::String("string2".to_string()))
}));
assert_eq!(None,
JsonPointer::from_str("/4")
.ok()
.and_then(|p| {
p.set(&Value::Array(array.clone()), &Value::String("string2".to_string()))
}));
assert_eq!(array.set_object(1, &Object::new().add_string("test", "test2"))
.ok().map(|a| Value::Array(a)),
JsonPointer::from_str("/1/test")
.ok()
.and_then(|p| {
p.set(&Value::Array(array.clone()), &Value::String("test2".to_string()))
}));Sourcepub fn set_array(pointer: &str, target: &Value, value: &Array) -> Option<Value>
pub fn set_array(pointer: &str, target: &Value, value: &Array) -> Option<Value>
Set an array in a JSON object or array at the location specified by the JSON pointer.
Sourcepub fn set_bool(pointer: &str, target: &Value, value: bool) -> Option<Value>
pub fn set_bool(pointer: &str, target: &Value, value: bool) -> Option<Value>
Set a Boolean value in a JSON object or array at the location specified by the JSON pointer.
Sourcepub fn set_decimal(pointer: &str, target: &Value, value: f64) -> Option<Value>
pub fn set_decimal(pointer: &str, target: &Value, value: f64) -> Option<Value>
Set a decimal value in a JSON object or array at the location specified by the JSON pointer.
Sourcepub fn set_integer(pointer: &str, target: &Value, value: i128) -> Option<Value>
pub fn set_integer(pointer: &str, target: &Value, value: i128) -> Option<Value>
Set an integer value in a JSON object or array at the location specified by the JSON pointer.
Sourcepub fn set_number(pointer: &str, target: &Value, value: Number) -> Option<Value>
pub fn set_number(pointer: &str, target: &Value, value: Number) -> Option<Value>
Set a number in a JSON object or array at the location specified by the JSON pointer.
Trait Implementations§
Source§impl Clone for JsonPointer
impl Clone for JsonPointer
Source§fn clone(&self) -> JsonPointer
fn clone(&self) -> JsonPointer
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for JsonPointer
impl Debug for JsonPointer
Source§impl Default for JsonPointer
impl Default for JsonPointer
Source§impl Display for JsonPointer
impl Display for JsonPointer
Source§impl FromStr for JsonPointer
impl FromStr for JsonPointer
Source§impl Hash for JsonPointer
impl Hash for JsonPointer
Source§impl Ord for JsonPointer
impl Ord for JsonPointer
Source§fn cmp(&self, other: &Self) -> Ordering
fn cmp(&self, other: &Self) -> Ordering
The comparison takes into account array indexes, which are compared numerically.
assert!(JsonPointer::from_str("/a").unwrap() == JsonPointer::from_str("/a").unwrap());
assert!(JsonPointer::from_str("/a/b").unwrap() < JsonPointer::from_str("/a/c").unwrap());
assert!(JsonPointer::from_str("/a/b/0").unwrap() < JsonPointer::from_str("/a/b/1").unwrap());
assert!(JsonPointer::from_str("/a/b/10").unwrap() > JsonPointer::from_str("/a/b/2").unwrap());
assert!(JsonPointer::from_str("/a/b/-").unwrap() > JsonPointer::from_str("/a/b/2").unwrap());
assert!(JsonPointer::from_str("/a/b/1").unwrap() < JsonPointer::from_str("/a/b/-").unwrap())