Array

Struct Array 

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

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_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_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_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_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_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_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 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.

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_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.

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_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.

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_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.

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_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.

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_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.

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_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.

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 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

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

Trait Implementations§

Source§

impl Clone for Array

Source§

fn clone(&self) -> Array

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 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 FromIterator<Value> for Array

Source§

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

Creates a value from an iterator. Read more
Source§

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

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

Creates an iterator from a value. Read more
Source§

impl PartialEq for Array

Source§

fn eq(&self, other: &Self) -> 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.

Auto Trait Implementations§

§

impl Freeze for Array

§

impl RefUnwindSafe for Array

§

impl Send for Array

§

impl Sync for Array

§

impl Unpin 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<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, 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.