Skip to main content

Array

Struct Array 

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

Represents a JSON array.

Implementations§

Source§

impl Array

Source

pub fn add(&self, value: &Value) -> Self

Adds a JSON value to an array.

assert_eq!(Some(true), Array::new().add(&Value::Bool(true)).get_bool(0).ok().flatten());
Source

pub fn add_array(&self, value: &Array) -> Self

Adds an array to an array.

assert_eq!(
    Some(0),
    Array::new()
        .add_array(&Array::new().add_integer(0))
        .get_array(0).ok().flatten().and_then(|a| a.get_integer(0).ok().flatten()));
Source

pub fn add_array_p(&self, pointer: &str, value: &Array) -> Option<Self>

Source

pub fn add_bool(&self, value: bool) -> Self

Adds a bool to an array.

assert_eq!(Some(true), Array::new().add_bool(true).get_bool(0).ok().flatten());
Source

pub fn add_bool_p(&self, pointer: &str, value: bool) -> Option<Self>

Source

pub fn add_decimal(&self, value: f64) -> Self

Adds a decimal to an array.

assert_eq!(Some(2.0), Array::new().add_decimal(2.0).get_decimal(0).ok().flatten());
Source

pub fn add_decimal_p(&self, pointer: &str, value: f64) -> Option<Self>

Source

pub fn add_integer(&self, value: i128) -> Self

Adds an integer to an array.

assert_eq!(Some(0), Array::new().add_integer(0).get_integer(0).ok().flatten());
Source

pub fn add_integer_p(&self, pointer: &str, value: i128) -> Option<Self>

Source

pub fn add_number(&self, value: Number) -> Self

Adds a number to an array.

assert_eq!(
   Some(Integer(0)),
   Array::new().add_number(Integer(0)).get_number(0).ok().flatten());
Source

pub fn add_number_p(&self, pointer: &str, value: Number) -> Option<Self>

Source

pub fn add_object(&self, value: &Object) -> Self

Adds an object to an array.

assert_eq!(
   Some(0),
   Array::new()
       .add_object(&Object::new().add_integer("test", 0))
       .get_object(0).ok().flatten().and_then(|o| o.get_integer("test")));
Source

pub fn add_object_p(&self, pointer: &str, value: &Object) -> Option<Self>

Source

pub fn add_p(&self, pointer: &str, value: &Value) -> Option<Self>

Source

pub fn add_string(&self, value: &str) -> Self

Adds a string to an array.

assert_eq!(
   Some("test".to_string()),
   Array::new().add_string("test").get_string(0).ok().flatten());
Source

pub fn add_string_p(&self, pointer: &str, value: &str) -> Option<Self>

Source

pub fn append(&self, array: &Array) -> Self

Appends an array to the current array.

assert_eq!(
   Array::new().add_integer(0).add_integer(1),
   Array::new().add_integer(0).append(&Array::new().add_integer(1)))
Source

pub fn get(&self, index: usize) -> Result<Value, JsonIndexError>

Gets a JSON value from an array at a given index, which is positive and less than the length of the array.

Source

pub fn get_array(&self, index: usize) -> Result<Option<Array>, JsonIndexError>

Gets an array from an array at a given index, which is positive and less than the length of the array. If the value at the position is not an array, None is returned.

let array = Array::new().add_number(Integer(0));

assert_eq!(Some(Integer(0)), array.get_number(0).ok().flatten());
assert_eq!(None, array.get_string(0).ok().flatten());
assert_eq!(Some(JsonIndexError { index: 1, len: 1, }), array.get_number(1).err());
Source

pub fn get_array_p(&self, pointer: &str) -> Option<Array>

Source

pub fn get_bool(&self, index: usize) -> Result<Option<bool>, JsonIndexError>

Gets a bool from an array at a given index, which is positive and less than the length of the array. If the value at the position is not a Boolean, None is returned.

let array = Array::new().add_bool(true);

assert_eq!(Some(true), array.get_bool(0).ok().flatten());
assert_eq!(None, array.get_string(0).ok().flatten());
assert_eq!(Some(JsonIndexError { index: 1, len: 1, }), array.get_bool(1).err());
Source

pub fn get_bool_p(&self, pointer: &str) -> Option<bool>

Source

pub fn get_decimal(&self, index: usize) -> Result<Option<f64>, JsonIndexError>

Gets a decimal from an array at a given index, which is positive and less than the length of the array. If the value at the position is not a decimal, None is returned.

let array = Array::new().add_decimal(2.0);

assert_eq!(Some(2.0), array.get_decimal(0).ok().flatten());
assert_eq!(None, array.get_string(0).ok().flatten());
assert_eq!(Some(JsonIndexError { index: 1, len: 1, }), array.get_decimal(1).err());
Source

pub fn get_decimal_p(&self, pointer: &str) -> Option<f64>

Source

pub fn get_integer(&self, index: usize) -> Result<Option<i128>, JsonIndexError>

Gets an integer from an array at a given index, which is positive and less than the length of the array. If the value at the position is not an integer, None is returned.

let array = Array::new().add_integer(0);

assert_eq!(Some(0), array.get_integer(0).ok().flatten());
assert_eq!(None, array.get_string(0).ok().flatten());
assert_eq!(Some(JsonIndexError { index: 1, len: 1, }), array.get_integer(1).err());
Source

pub fn get_integer_p(&self, pointer: &str) -> Option<i128>

Source

pub fn get_number(&self, index: usize) -> Result<Option<Number>, JsonIndexError>

Gets a number from an array at a given index, which is positive and less than the length of the array. If the value at the position is not a number, None is returned.

let array = Array::new().add_number(Integer(0));

assert_eq!(Some(Integer(0)), array.get_number(0).ok().flatten());
assert_eq!(None, array.get_string(0).ok().flatten());
assert_eq!(Some(JsonIndexError { index: 1, len: 1, }), array.get_number(1).err());
Source

pub fn get_number_p(&self, pointer: &str) -> Option<Number>

Source

pub fn get_object(&self, index: usize) -> Result<Option<Object>, JsonIndexError>

Gets an object from an array at a given index, which is positive and less than the length of the array. If the value at the position is not a JSON object, None is returned.

let array = Array::new().add_object(&Object::new().add_integer("test", 0));

assert_eq!(Some(0),
   array.get_object(0).ok().flatten().and_then(|o| o.get_integer("test")));
assert_eq!(None, array.get_string(0).ok().flatten());
assert_eq!(Some(JsonIndexError { index: 1, len: 1, }), array.get_object(1).err());
Source

pub fn get_object_p(&self, pointer: &str) -> Option<Object>

Source

pub fn get_p(&self, pointer: &str) -> Option<Value>

Source

pub fn get_string(&self, index: usize) -> Result<Option<String>, JsonIndexError>

Gets a string from an array at a given index, which is positive and less than the length of the array. If the value at the position is not a string, None is returned.

let array = Array::new().add_string("test");

assert_eq!(Some("test".to_string()), array.get_string(0).ok().flatten());
assert_eq!(None, array.get_integer(0).ok().flatten());
assert_eq!(Some(JsonIndexError { index: 1, len: 1, }), array.get_string(1).err());
Source

pub fn get_string_p(&self, pointer: &str) -> Option<String>

Source

pub fn insert( &self, index: usize, value: &Value, ) -> Result<Self, JsonIndexError>

Inserts a JSON value to an array at a given index. If the index is equal to the length of the array, the value is added at the end of it.

assert_eq!(Some(true), Array::new().insert(0, &Value::Bool(true))?.get_bool(0).ok().flatten());
Source

pub fn insert_array( &self, index: usize, value: &Array, ) -> Result<Self, JsonIndexError>

Inserts an array to an array at a given index. If the index is equal to the length of the array, the value is added at the end of it.

assert_eq!(
   Some(0),
   Array::new()
       .insert_array(0, &Array::new().insert_integer(0, 0)?)?
       .get_array(0).ok().flatten().and_then(|a| a.get_integer(0).ok().flatten()));
Source

pub fn insert_bool( &self, index: usize, value: bool, ) -> Result<Self, JsonIndexError>

Inserts a bool to an array at a given index. If the index is equal to the length of the array, the value is added at the end of it.

assert_eq!(Some(true), Array::new().insert_bool(0, true)?.get_bool(0).ok().flatten());
Source

pub fn insert_decimal( &self, index: usize, value: f64, ) -> Result<Self, JsonIndexError>

Inserts a decimal to an array at a given index. If the index is equal to the length of the array, the value is added at the end of it.

assert_eq!(Some(2.0), Array::new().insert_decimal(0, 2.0)?.get_decimal(0).ok().flatten());
Source

pub fn insert_integer( &self, index: usize, value: i128, ) -> Result<Self, JsonIndexError>

Inserts an integer to an array at a given index. If the index is equal to the length of the array, the value is added at the end of it.

assert_eq!(Some(0), Array::new().insert_integer(0, 0)?.get_integer(0).ok().flatten());
Source

pub fn insert_number( &self, index: usize, value: Number, ) -> Result<Self, JsonIndexError>

Inserts a number to an array at a given index. If the index is equal to the length of the array, the value is added at the end of it.

assert_eq!(
   Some(Integer(0)),
   Array::new().insert_number(0, Integer(0))?.get_number(0).ok().flatten());
Source

pub fn insert_object( &self, index: usize, value: &Object, ) -> Result<Self, JsonIndexError>

Inserts an object to an array at a given index. If the index is equal to the length of the array, the value is added at the end of it.

assert_eq!(
   Some(0),
   Array::new()
       .insert_object(0, &Object::new().add_integer("test", 0))?
       .get_object(0).ok().flatten().and_then(|o| o.get_integer("test")));
Source

pub fn insert_string( &self, index: usize, value: &str, ) -> Result<Self, JsonIndexError>

Inserts a string to an array at a given index. If the index is equal to the length of the array, the value is added at the end of it.

assert_eq!(
   Some("test".to_string()),
   Array::new().insert_string(0, "test")?.get_string(0).ok().flatten());
Source

pub fn is_empty(&self) -> bool

Indicates if the array is empty or not.

assert_eq!(true, Array::new().is_empty());
assert_eq!(false, Array::new().add_integer(0).is_empty());
Source

pub fn iter(&self) -> ArrayIter<'_>

Returns an iterator over the values of the array.

let array = Array::new().add_string("test").add_integer(1);

assert_eq!(array, Array::from_iter(array.iter()));
Source

pub fn len(&self) -> usize

Returns the length of an array.

assert_eq!(0, Array::new().len());
assert_eq!(1, Array::new().add_integer(0).len());
Source

pub fn new() -> Self

Creates an empty JSON array.

Source

pub fn remove(&self, index: usize) -> Result<Self, JsonIndexError>

Removes a JSON value from an array at a given index, which is positive and less than the length of the array.

assert_eq!(Some(0), Array::new().add_integer(0).remove(0).ok().map(|a| a.len()));
assert_eq!(Some(JsonIndexError { index: 1, len: 0, }), Array::new().remove(1).err());
Source

pub fn remove_p(&self, pointer: &str) -> Option<Self>

Source

pub fn set(&self, index: usize, value: &Value) -> Result<Self, JsonIndexError>

Sets a JSON value in an array at a given index, which is positive and less than the length of the array.

Source

pub fn set_array( &self, index: usize, value: &Array, ) -> Result<Self, JsonIndexError>

Sets an array in an array at a given index, which is positive and less than the length of the array.

let array = Array::new().add_bool(true);

assert_eq!(
   Some(0),
   array
       .set_array(0, &Array::new().add_integer(0))
       .ok()
       .and_then(|a| {
           a.get_array(0).ok().flatten().and_then(|a| a.get_integer(0).ok().flatten())
       }));
assert_eq!(Some(JsonIndexError { index: 1, len: 1, }), array.set_bool(1, false).err());
Source

pub fn set_array_p(&self, pointer: &str, value: &Array) -> Option<Self>

Source

pub fn set_bool( &self, index: usize, value: bool, ) -> Result<Self, JsonIndexError>

Sets a bool in an array at a given index, which is positive and less than the length of the array.

let array = Array::new().add_bool(true);

assert_eq!(
   Some(false),
   array
       .set_bool(0, false)
       .ok()
       .and_then(|a| a.get_bool(0).ok().flatten()));
assert_eq!(Some(JsonIndexError { index: 1, len: 1, }), array.set_bool(1, false).err());
Source

pub fn set_bool_p(&self, pointer: &str, value: bool) -> Option<Self>

Source

pub fn set_decimal( &self, index: usize, value: f64, ) -> Result<Self, JsonIndexError>

Sets a decimal in an array at a given index, which is positive and less than the length of the array.

let array = Array::new().add_bool(true);

assert_eq!(
   Some(3.0),
   array
       .set_decimal(0, 3.0)
       .ok()
       .and_then(|a| a.get_decimal(0).ok().flatten()));
assert_eq!(Some(JsonIndexError { index: 1, len: 1, }), array.set_decimal(1, 1.0).err());
Source

pub fn set_decimal_p(&self, pointer: &str, value: f64) -> Option<Self>

Source

pub fn set_integer( &self, index: usize, value: i128, ) -> Result<Self, JsonIndexError>

Sets an integer in an array at a given index, which is positive and less than the length of the array.

let array = Array::new().add_bool(true);

assert_eq!(
   Some(3),
   array
       .set_integer(0, 3)
       .ok()
       .and_then(|a| a.get_integer(0).ok().flatten()));
assert_eq!(Some(JsonIndexError { index: 1, len: 1, }), array.set_integer(1, 1).err());
Source

pub fn set_integer_p(&self, pointer: &str, value: i128) -> Option<Self>

Source

pub fn set_number( &self, index: usize, value: Number, ) -> Result<Self, JsonIndexError>

Sets a number in an array at a given index, which is positive and less than the length of the array.

let array = Array::new().add_bool(true);

assert_eq!(
   Some(Decimal(3.0)),
   array
       .set_number(0, Decimal(3.0))
       .ok()
       .and_then(|a| a.get_number(0).ok().flatten()));
assert_eq!(
   Some(JsonIndexError { index: 1, len: 1, }),
   array.set_number(1, Decimal(1.0)).err());
Source

pub fn set_number_p(&self, pointer: &str, value: Number) -> Option<Self>

Source

pub fn set_object( &self, index: usize, value: &Object, ) -> Result<Self, JsonIndexError>

Sets an object in an array at a given index, which is positive and less than the length of the array.

let array = Array::new().add_bool(true);

assert_eq!(
   Some("test".to_string()),
   array
       .set_object(0, &Object::new().add_string("test", "test"))
       .ok()
       .and_then(|a| a.get_object(0).ok().flatten().and_then(|o| o.get_string("test"))));
assert_eq!(
   Some(JsonIndexError { index: 1, len: 1, }),
   array.set_object(1, &Object::new()).err());
Source

pub fn set_object_p(&self, pointer: &str, value: &Object) -> Option<Self>

Source

pub fn set_p(&self, pointer: &str, value: &Value) -> Option<Self>

Source

pub fn set_string( &self, index: usize, value: &str, ) -> Result<Self, JsonIndexError>

Sets a string in an array at a given index, which is positive and less than the length of the array.

let array = Array::new().add_bool(true);

assert_eq!(
   Some("test".to_string()),
   array
       .set_string(0, "test")
       .ok()
       .and_then(|a| a.get_string(0).ok().flatten()));
assert_eq!(Some(JsonIndexError { index: 1, len: 1, }), array.set_string(1, "test").err());
Source

pub fn set_string_p(&self, pointer: &str, value: &str) -> Option<Self>

Trait Implementations§

Source§

impl Clone for Array

Source§

fn clone(&self) -> Array

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Array

Source§

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

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

impl Default for Array

Source§

fn default() -> Self

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

impl Display for Array

Source§

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

Converts a JSON array to a string.

let data = r#"
   [
       "string",
       1,
       3.0,
       false,
       {"test": "test"},
       [1]
   ]"#;

let v: serde_json::Value = serde_json::from_str(data)?;

assert_eq!(Some(v), serde_json::from_str(&Value::from_str(data)?.to_string()).ok());
Source§

impl Eq for Array

Source§

impl FromIterator<Value> for Array

Source§

fn from_iter<T: IntoIterator<Item = Value>>(iter: T) -> Self

Creates a JSON array from a stream of JSON values.

Source§

impl Hash for Array

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<'a> IntoIterator for &'a Array

Source§

fn into_iter(self) -> ArrayIter<'a>

Iterates over the values in a JSON array,

Source§

type Item = Value

The type of the elements being iterated over.
Source§

type IntoIter = ArrayIter<'a>

Which kind of iterator are we turning this into?
Source§

impl PartialEq for Array

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Array

Auto Trait Implementations§

§

impl Freeze for Array

§

impl RefUnwindSafe for Array

§

impl Send for Array

§

impl Sync for Array

§

impl Unpin for Array

§

impl UnsafeUnpin for Array

§

impl UnwindSafe for Array

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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.