Skip to main content

JsonPointer

Struct JsonPointer 

Source
pub struct JsonPointer { /* private fields */ }
Expand description

Represents a JSON pointer.

Implementations§

Source§

impl JsonPointer

Source

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()))
       }));
Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn array_index(&self) -> Option<usize>

If the pointer refers to a value in an array, the index within the array is returned.

Source

pub fn child(&self, segment: &str) -> Self

Returns a JSON pointer with an extra path segment.

Source

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)));
Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn new() -> Self

Creates a JSON pointer that refers to the root.

Source

pub fn parent(&self) -> Self

Returns a JSON pointer that refers to the parent.

Source

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()))));
Source

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()))
       }));
Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn set_object( pointer: &str, target: &Value, value: &Object, ) -> Option<Value>

Set a JSON object in a JSON object or array at the location specified by the JSON pointer.

Source

pub fn set_string(pointer: &str, target: &Value, value: &str) -> Option<Value>

Set a string value in a JSON object or array at the location specified by the JSON pointer.

Trait Implementations§

Source§

impl Clone for JsonPointer

Source§

fn clone(&self) -> JsonPointer

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for JsonPointer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for JsonPointer

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for JsonPointer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Convert a JSON pointer to a string.

let p = "/a/b~1c/~0d";

assert_eq!(p, JsonPointer::from_str(p).unwrap().to_string())
Source§

impl FromStr for JsonPointer

Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Create a JSON pointer from a string.

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

impl Hash for JsonPointer

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for JsonPointer

Source§

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())
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for JsonPointer

Source§

fn eq(&self, other: &JsonPointer) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for JsonPointer

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for JsonPointer

Source§

impl StructuralPartialEq for JsonPointer

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.