pub struct Object { /* private fields */ }Implementations§
Source§impl Object
impl Object
Sourcepub fn add(&self, key: &str, value: Value) -> Self
pub fn add(&self, key: &str, value: Value) -> Self
Adds a field to an object.
assert_eq!(Some("test".to_string()),
Object::new().add("test", String("test".to_string())).get_string("test"));Sourcepub fn add_array(&self, key: &str, value: Array) -> Self
pub fn add_array(&self, key: &str, value: Array) -> Self
Adds a field to an object as an array.
assert_eq!(Some(1),
Object::new()
.add_array("test", Array::new().add_integer(1))
.get_array("test").and_then(|a| a.get_integer(0).ok()?));Sourcepub fn add_bool(&self, key: &str, value: bool) -> Self
pub fn add_bool(&self, key: &str, value: bool) -> Self
Adds a field to an object as a bool.
assert_eq!(Some(true), Object::new().add_bool("test", true).get_bool("test"));Sourcepub fn add_decimal(&self, key: &str, value: f64) -> Self
pub fn add_decimal(&self, key: &str, value: f64) -> Self
Adds a field to an object as a decimal.
assert_eq!(Some(3.0), Object::new().add_decimal("test", 3.0).get_decimal("test"));Sourcepub fn add_integer(&self, key: &str, value: i128) -> Self
pub fn add_integer(&self, key: &str, value: i128) -> Self
Adds a field to an object as an integer.
assert_eq!(Some(3), Object::new().add_integer("test", 3).get_integer("test"));Sourcepub fn add_number(&self, key: &str, value: Number) -> Self
pub fn add_number(&self, key: &str, value: Number) -> Self
Adds a field to an object as a number.
assert_eq!(Some(3),
Object::new()
.add_number("test", Integer(3))
.get_number("test").and_then(|n| n.as_integer()));Sourcepub fn add_object(&self, key: &str, value: Object) -> Self
pub fn add_object(&self, key: &str, value: Object) -> Self
Adds a field to an object as an object.
assert_eq!(Some(1),
Object::new()
.add_object("test", Object::new().add_integer("test", 1))
.get_object("test").and_then(|o| o.get_integer("test")));Sourcepub fn add_string(&self, key: &str, value: &str) -> Self
pub fn add_string(&self, key: &str, value: &str) -> Self
Adds a field to an object as an integer.
assert_eq!(Some("test".to_string()),
Object::new().add_string("test", "test").get_string("test"));Sourcepub fn get(&self, key: &str) -> Option<&Value>
pub fn get(&self, key: &str) -> Option<&Value>
Returns a value from the object if it exists at the given key.
let object = Object::new().add_string("test1", "test");
assert_eq!(Some("test".to_string()), object.get_string("test1"));
assert_eq!(None, object.get_string("test2"));Sourcepub fn get_array(&self, key: &str) -> Option<Array>
pub fn get_array(&self, key: &str) -> Option<Array>
Returns a value from the object if it exists at the given key and if it is an array.
let object = Object::new()
.add_array("test1", Array::new().add_integer(3))
.add_integer("test2", 3);
assert_eq!(Some(3), object.get_array("test1").and_then(|a| a.get_integer(0).ok()?));
assert_eq!(None, object.get_array("test2"));Sourcepub fn get_bool(&self, key: &str) -> Option<bool>
pub fn get_bool(&self, key: &str) -> Option<bool>
Returns a value from the object if it exists at the given key and if it is a bool.
let object = Object::new()
.add_bool("test1", true)
.add_integer("test2", 3);
assert_eq!(Some(true), object.get_bool("test1"));
assert_eq!(None, object.get_string("test2"));Sourcepub fn get_decimal(&self, key: &str) -> Option<f64>
pub fn get_decimal(&self, key: &str) -> Option<f64>
Returns a value from the object if it exists at the given key and if it is a decimal.
let object = Object::new()
.add_decimal("test1", 3.0)
.add_integer("test2", 3);
assert_eq!(Some(3.0), object.get_decimal("test1"));
assert_eq!(None, object.get_string("test2"));Sourcepub fn get_integer(&self, key: &str) -> Option<i128>
pub fn get_integer(&self, key: &str) -> Option<i128>
Returns a value from the object if it exists at the given key and if it is an integer.
let object = Object::new()
.add_decimal("test1", 3.0)
.add_integer("test2", 3);
assert_eq!(Some(3.0), object.get_decimal("test1"));
assert_eq!(None, object.get_string("test2"));Sourcepub fn get_number(&self, key: &str) -> Option<Number>
pub fn get_number(&self, key: &str) -> Option<Number>
Returns a value from the object if it exists at the given key and if it is a number.
let object = Object::new()
.add_number("test1", Number::Decimal(3.0))
.add_integer("test2", 3);
assert_eq!(Some(3.0), object.get_number("test1").and_then(|n| n.as_decimal()));
assert_eq!(None, object.get_string("test2"));Sourcepub fn get_object(&self, key: &str) -> Option<Object>
pub fn get_object(&self, key: &str) -> Option<Object>
Returns a value from the object if it exists at the given key and if it is an object.
let object = Object::new()
.add_object("test1", Object::new().add_integer("test", 3))
.add_integer("test2", 3);
assert_eq!(Some(3), object.get_object("test1").and_then(|a| a.get_integer("test")));
assert_eq!(None, object.get_array("test2"));Sourcepub fn get_string(&self, key: &str) -> Option<String>
pub fn get_string(&self, key: &str) -> Option<String>
Returns a value from the object if it exists at the given key and if it is a string.
let object = Object::new()
.add_string("test1", "test")
.add_integer("test2", 3);
assert_eq!(Some("test".to_string()), object.get_string("test1"));
assert_eq!(None, object.get_string("test2"));Sourcepub fn has_key(&self, key: &str) -> bool
pub fn has_key(&self, key: &str) -> bool
Returns true if the object has a field with the given key.
let object = Object::new().add_string("test1", "test");
assert_eq!(true, object.has_key("test1"));
assert_eq!(false, object.has_key("test2"));Sourcepub fn iter(&self) -> ObjectIter<'_> ⓘ
pub fn iter(&self) -> ObjectIter<'_> ⓘ
Returns an iterator over the key/value tuples of the object.
let object = Object::new().add_string("test1", "test1").add_integer("test2", 1);
assert_eq!(object, Object::from_iter(object.iter()));