pub struct Object { /* private fields */ }Expand description
Represents a JSON object.
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()?));pub fn add_array_p(&self, pointer: &str, value: &Array) -> Option<Self>
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"));pub fn add_bool_p(&self, pointer: &str, value: bool) -> Option<Self>
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"));pub fn add_decimal_p(&self, pointer: &str, value: f64) -> Option<Self>
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"));pub fn add_integer_p(&self, pointer: &str, value: i128) -> Option<Self>
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()));pub fn add_number_p(&self, pointer: &str, value: Number) -> Option<Self>
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")));pub fn add_object_p(&self, pointer: &str, value: &Object) -> Option<Self>
pub fn add_p(&self, pointer: &str, value: &Value) -> Option<Self>
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"));pub fn add_string_p(&self, pointer: &str, value: &str) -> Option<Self>
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.
Otherwise, None is returned.
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"));pub fn get_array_p(&self, pointer: &str) -> Option<Array>
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.
Otherwise, None is returned.
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"));pub fn get_bool_p(&self, pointer: &str) -> Option<bool>
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.
Otherwise, None is returned.
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"));pub fn get_decimal_p(&self, pointer: &str) -> Option<f64>
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.
Otherwise, None is returned.
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"));pub fn get_integer_p(&self, pointer: &str) -> Option<i128>
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.
Otherwise, None is returned.
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"));pub fn get_number_p(&self, pointer: &str) -> Option<Number>
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.
Otherwise, None is returned.
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"));pub fn get_object_p(&self, pointer: &str) -> Option<Object>
pub fn get_p(&self, pointer: &str) -> Option<Value>
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.
Otherwise, None is returned.
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"));pub fn get_string_p(&self, pointer: &str) -> Option<String>
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()));Sourcepub fn merge(&self, other: &Object) -> Self
pub fn merge(&self, other: &Object) -> Self
Merges the other object into this one, overwriting existing entries.
let object = Object::new().add_string("test1", "test1").add_integer("test2", 1);
let other = Object::new().add_string("test1", "changed").add_integer("test3", 2);
let merged = Object::new().add_string("test1", "changed").add_integer("test2", 1).add_integer("test3", 2);
assert_eq!(merged, object.merge(&other));Sourcepub fn remove(&self, key: &str) -> Self
pub fn remove(&self, key: &str) -> Self
Removes the field with the given key from the object.
let object = Object::new().add_string("test", "test");
assert_eq!(true, object.has_key("test"));
assert_eq!(false, object.remove("test").has_key("test"));pub fn remove_p(&self, pointer: &str) -> Option<Self>
Sourcepub fn set(&self, key: &str, value: &Value) -> Self
pub fn set(&self, key: &str, value: &Value) -> Self
Sets a field to an object.
assert_eq!(Some("test".to_string()),
Object::new().set("test", &String("test".to_string())).get_string("test"));Sourcepub fn set_array(&self, key: &str, value: &Array) -> Self
pub fn set_array(&self, key: &str, value: &Array) -> Self
Sets a field to an object as an array.
assert_eq!(Some(1),
Object::new()
.set_array("test", &Array::new().add_integer(1))
.get_array("test").and_then(|a| a.get_integer(0).ok()?));pub fn set_array_p(&self, pointer: &str, value: &Array) -> Option<Self>
Sourcepub fn set_bool(&self, key: &str, value: bool) -> Self
pub fn set_bool(&self, key: &str, value: bool) -> Self
Sets a field to an object as a bool.
assert_eq!(Some(true), Object::new().set_bool("test", true).get_bool("test"));pub fn set_bool_p(&self, pointer: &str, value: bool) -> Option<Self>
Sourcepub fn set_decimal(&self, key: &str, value: f64) -> Self
pub fn set_decimal(&self, key: &str, value: f64) -> Self
Sets a field to an object as a decimal.
assert_eq!(Some(3.0), Object::new().set_decimal("test", 3.0).get_decimal("test"));pub fn set_decimal_p(&self, pointer: &str, value: f64) -> Option<Self>
Sourcepub fn set_integer(&self, key: &str, value: i128) -> Self
pub fn set_integer(&self, key: &str, value: i128) -> Self
Sets a field to an object as an integer.
assert_eq!(Some(3), Object::new().set_integer("test", 3).get_integer("test"));pub fn set_integer_p(&self, pointer: &str, value: i128) -> Option<Self>
Sourcepub fn set_number(&self, key: &str, value: Number) -> Self
pub fn set_number(&self, key: &str, value: Number) -> Self
Sets a field to an object as a number.
assert_eq!(Some(3),
Object::new()
.set_number("test", Integer(3))
.get_number("test").and_then(|n| n.as_integer()));pub fn set_number_p(&self, pointer: &str, value: Number) -> Option<Self>
Sourcepub fn set_object(&self, key: &str, value: &Object) -> Self
pub fn set_object(&self, key: &str, value: &Object) -> Self
Sets a field to an object as an object.
assert_eq!(Some(1),
Object::new()
.set_object("test", &Object::new().add_integer("test", 1))
.get_object("test").and_then(|o| o.get_integer("test")));pub fn set_object_p(&self, pointer: &str, value: &Object) -> Option<Self>
pub fn set_p(&self, pointer: &str, value: &Value) -> Option<Self>
Sourcepub fn set_string(&self, key: &str, value: &str) -> Self
pub fn set_string(&self, key: &str, value: &str) -> Self
Sets a field to an object as an integer.
assert_eq!(Some("test".to_string()),
Object::new().set_string("test", "test").get_string("test"));pub fn set_string_p(&self, pointer: &str, value: &str) -> Option<Self>
Trait Implementations§
Source§impl Display for Object
impl Display for Object
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Converts a JSON object to a string.
let data = r#"
{
"string": "string",
"int": 43,
"float": 5.8,
"boolean": true,
"object": {"test": "test"},
"array": [
"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());impl Eq for Object
Source§impl<'a> IntoIterator for &'a Object
impl<'a> IntoIterator for &'a Object
impl StructuralPartialEq for Object
Auto Trait Implementations§
impl Freeze for Object
impl RefUnwindSafe for Object
impl Send for Object
impl Sync for Object
impl Unpin for Object
impl UnsafeUnpin for Object
impl UnwindSafe for Object
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.