pub struct Array { /* private fields */ }Implementations§
Source§impl Array
impl Array
Sourcepub fn add(&self, value: Value) -> Self
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());Sourcepub fn add_array(&self, value: Array) -> Self
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()));Sourcepub fn add_bool(&self, value: bool) -> Self
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());Sourcepub fn add_decimal(&self, value: f64) -> Self
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());Sourcepub fn add_integer(&self, value: i128) -> Self
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());Sourcepub fn add_number(&self, value: Number) -> Self
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());Sourcepub fn add_object(&self, value: Object) -> Self
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")));Sourcepub fn add_string(&self, value: &str) -> Self
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());Sourcepub fn get(&self, index: usize) -> Result<Value, JsonIndexError>
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.
Sourcepub fn get_array(&self, index: usize) -> Result<Option<Array>, JsonIndexError>
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());Sourcepub fn get_bool(&self, index: usize) -> Result<Option<bool>, JsonIndexError>
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());Sourcepub fn get_decimal(&self, index: usize) -> Result<Option<f64>, JsonIndexError>
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());Sourcepub fn get_integer(&self, index: usize) -> Result<Option<i128>, JsonIndexError>
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());Sourcepub fn get_number(&self, index: usize) -> Result<Option<Number>, JsonIndexError>
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());Sourcepub fn get_object(&self, index: usize) -> Result<Option<Object>, JsonIndexError>
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());Sourcepub fn get_string(&self, index: usize) -> Result<Option<String>, JsonIndexError>
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());Sourcepub fn is_empty(&self) -> bool
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());Sourcepub fn iter(&self) -> ArrayIter<'_> ⓘ
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()));Sourcepub fn len(&self) -> usize
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());pub fn new() -> Self
Sourcepub fn remove(&self, index: usize) -> Result<Self, JsonIndexError>
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());Sourcepub fn set(&self, index: usize, value: Value) -> Result<Self, JsonIndexError>
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.
Sourcepub fn set_array(
&self,
index: usize,
value: Array,
) -> Result<Self, JsonIndexError>
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());Sourcepub fn set_bool(
&self,
index: usize,
value: bool,
) -> Result<Self, JsonIndexError>
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());Sourcepub fn set_decimal(
&self,
index: usize,
value: f64,
) -> Result<Self, JsonIndexError>
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());Sourcepub fn set_integer(
&self,
index: usize,
value: i128,
) -> Result<Self, JsonIndexError>
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());Sourcepub fn set_number(
&self,
index: usize,
value: Number,
) -> Result<Self, JsonIndexError>
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());Sourcepub fn set_object(
&self,
index: usize,
value: Object,
) -> Result<Self, JsonIndexError>
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());Sourcepub fn set_string(
&self,
index: usize,
value: &str,
) -> Result<Self, JsonIndexError>
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());