pub struct Object(/* private fields */);
Expand description
Represents the JSON object. The inner implement is a key-value array. Its order is as same as origin JSON.
§Examples
use sonic_rs::{from_str, Object};
let mut obj: Object = from_str(r#"{"a": 1, "b": true, "c": null}"#).unwrap();
assert_eq!(obj["a"], 1);
assert_eq!(obj.insert(&"d", "e"), None);
assert_eq!(obj["d"], "e");
assert_eq!(obj.len(), 4);
§Warning
The key in Object
is not sorted and the get
operation is O(n). And Object
is allowed to
have duplicated keys.
§Examples
use sonic_rs::{from_str, Object};
let obj: Object = from_str(r#"{"a": 1, "a": true, "a": null}"#).unwrap();
assert_eq!(obj["a"], 1);
assert_eq!(obj.len(), 3); // allow duplicated keys
If you care about that, recommend to use HashMap
or BTreeMap
instead. The parse performance
is slower than Object
.
Implementations§
Source§impl Object
impl Object
Sourcepub fn into_value(self) -> Value
pub fn into_value(self) -> Value
Returns the inner Value
.
Sourcepub fn new() -> Object
pub fn new() -> Object
Create a new empty object.
§Example
use sonic_rs::{from_str, json, object, prelude::*, Object};
let mut obj: Object = from_str("{}").unwrap();
obj.insert(&"arr", object! {});
obj.insert(&"a", 1);
obj.insert(&"arr2", Object::new());
obj["a"] = json!(123);
obj["arr2"] = json!("hello world");
assert_eq!(obj["a"], 123);
assert_eq!(obj["arr2"], "hello world");
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create a new empty object with capacity.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clear the object, make it as empty but keep the allocated memory.
§Examples
use sonic_rs::{json, object};
let mut obj = object! {"a": 1, "b": true, "c": null};
obj.clear();
assert!(obj.is_empty());
assert!(obj.capacity() >= 3);
Sourcepub fn get<Q: AsRef<str>>(&self, key: &Q) -> Option<&Value>
pub fn get<Q: AsRef<str>>(&self, key: &Q) -> Option<&Value>
Returns a reference to the value corresponding to the key.
The key may be AsRef<str>
.
§Examples
use sonic_rs::{json, object};
let mut obj = object! {"a": 1, "b": true, "c": null};
obj.insert(&"d", "e");
assert_eq!(obj.get(&"d").unwrap(), "e");
assert_eq!(obj.get(&"f"), None);
assert_eq!(obj.get(&"a").unwrap(), 1);
Sourcepub fn contains_key<Q: AsRef<str>>(&self, key: &Q) -> bool
pub fn contains_key<Q: AsRef<str>>(&self, key: &Q) -> bool
Returns true
if the map contains a value for the specified key.
The key may be AsRef<str>
.
§Examples
use sonic_rs::object;
let mut obj = object! {"a": 1, "b": true, "c": null};
obj.insert(&"d", "e");
assert_eq!(obj.contains_key(&"d"), true);
assert_eq!(obj.contains_key(&"a"), true);
assert_eq!(obj.contains_key(&"e"), false);
Sourcepub fn get_mut<Q: AsRef<str>>(&mut self, key: &Q) -> Option<&mut Value>
pub fn get_mut<Q: AsRef<str>>(&mut self, key: &Q) -> Option<&mut Value>
Returns a mutable reference to the value corresponding to the key.
The key may be AsRef<str>
.
§Examples
use sonic_rs::object;
let mut obj = object! {"a": 1, "b": true, "c": null};
obj.insert(&"d", "e");
*(obj.get_mut(&"d").unwrap()) = "f".into();
assert_eq!(obj.contains_key(&"d"), true);
assert_eq!(obj["d"], "f");
Sourcepub fn get_key_value<Q: AsRef<str>>(&self, key: &Q) -> Option<(&str, &Value)>
pub fn get_key_value<Q: AsRef<str>>(&self, key: &Q) -> Option<(&str, &Value)>
Returns the key-value pair corresponding to the supplied key.
The key may be AsRef<str>
.
§Examples
use sonic_rs::{object, Value};
let mut obj = object! {"a": 1, "b": true, "c": null};
obj.insert(&"d", "e");
assert_eq!(obj.get_key_value(&"d").unwrap(), ("d", &Value::from("e")));
assert_eq!(obj.get_key_value(&"a").unwrap(), ("a", &Value::from(1)));
assert_eq!(obj.get_key_value(&"e"), None);
Sourcepub fn insert<K: AsRef<str> + ?Sized, V: Into<Value>>(
&mut self,
key: &K,
value: V,
) -> Option<Value>
pub fn insert<K: AsRef<str> + ?Sized, V: Into<Value>>( &mut self, key: &K, value: V, ) -> Option<Value>
Inserts a key-value pair into the object. The Value
is converted from V
.
The key may be AsRef<str>
.
If the object did not have this key present, None
is returned.
If the object did have this key present, the value is updated, and the old
value is returned. The key is not updated, though; this matters for
types that can be ==
without being identical. See the [module-level
documentation] for more.
§Examples
use sonic_rs::{json, object, Value};
let mut obj = object! {"a": 1, "b": true, "c": null};
assert_eq!(obj.len(), 3);
assert_eq!(obj.insert(&"d", "e"), None);
assert_eq!(obj.len(), 4);
assert_eq!(obj["d"], "e");
assert_eq!(obj.insert(&"d", "f").unwrap(), "e");
assert_eq!(obj["d"], "f");
assert_eq!(obj.len(), 4);
assert_eq!(obj.insert(&"d", json!("h")).unwrap(), "f");
assert_eq!(obj["d"], "h");
assert_eq!(obj.insert(&"i", Value::from("j")), None);
assert_eq!(obj.len(), 5);
Sourcepub fn remove<Q: AsRef<str>>(&mut self, key: &Q) -> Option<Value>
pub fn remove<Q: AsRef<str>>(&mut self, key: &Q) -> Option<Value>
Removes a key from the object, returning the value at the key if the key was previously in the object.
The key may be AsRef<str>
.
§Examples
use sonic_rs::object;
let mut obj = object! {"a": 1, "b": true, "c": null};
assert_eq!(obj.remove(&"d"), None);
assert_eq!(obj.remove(&"a").unwrap(), 1);
Sourcepub fn remove_entry<'k, Q: AsRef<str>>(
&mut self,
key: &'k Q,
) -> Option<(&'k str, Value)>
pub fn remove_entry<'k, Q: AsRef<str>>( &mut self, key: &'k Q, ) -> Option<(&'k str, Value)>
Removes a key from the object, returning the stored key and value if the key was previously in the obj.
The key may be AsRef<str>
.
§Examples
use sonic_rs::object;
let mut obj = object! {"a": 1, "b": true, "c": null};
assert_eq!(obj.remove_entry(&"d"), None);
let (key, val) = obj.remove_entry(&"a").unwrap();
assert_eq!(key, "a");
assert_eq!(val, 1);
Sourcepub fn iter(&self) -> Iter<'_> ⓘ
pub fn iter(&self) -> Iter<'_> ⓘ
Returns an immutable iterator over the key-value pairs of the object.
§Examples
use sonic_rs::object;
let obj = object! {"a": 1, "b": true, "c": null};
for (key, value) in obj.iter() {
println!("{}: {}", key, value);
}
Sourcepub fn iter_mut(&mut self) -> IterMut<'_> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_> ⓘ
Returns an mutable iterator over the key-value pairs of the object.
§Examples
use sonic_rs::{object, Value};
let mut obj = object! {"a": 1, "b": true, "c": null};
for (key, value) in obj.iter_mut() {
*value = Value::from(key);
}
assert_eq!(obj["a"], "a");
assert_eq!(obj["b"], "b");
assert_eq!(obj["c"], "c");
Sourcepub fn entry<'a, Q: AsRef<str> + ?Sized>(&'a mut self, key: &Q) -> Entry<'a>
pub fn entry<'a, Q: AsRef<str> + ?Sized>(&'a mut self, key: &Q) -> Entry<'a>
Gets the given key’s corresponding entry in the object for in-place manipulation.
§Examples
use sonic_rs::{object, Value};
let mut obj = object! {};
for i in 0..10 {
obj.entry(&i.to_string()).or_insert(1);
}
for i in 0..10 {
obj.entry(&i.to_string())
.and_modify(|v| *v = Value::from(i + 1));
}
assert_eq!(obj[&"1"], 2);
assert_eq!(obj[&"2"], 3);
assert_eq!(obj[&"3"], 4);
assert_eq!(obj.get(&"10"), None);
Sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
Retains only the elements specified by the predicate.
In other words, remove all pairs (k, v)
for which f(&k, &mut v)
returns false
.
The elements are visited in unsorted (and unspecified) order.
§Examples
use sonic_rs::object;
let mut obj = object! {"a": 1, "b": true, "c": null};
obj.retain(|key, _| key == "a");
assert_eq!(obj.len(), 1);
assert_eq!(obj["a"], 1);
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Object
impl<'de> Deserialize<'de> for Object
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<'a, K: AsRef<str> + ?Sized, V: Clone + Debug + Into<Value> + 'a> Extend<(&'a K, &'a V)> for Object
impl<'a, K: AsRef<str> + ?Sized, V: Clone + Debug + Into<Value> + 'a> Extend<(&'a K, &'a V)> for Object
Source§fn extend<I: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: I)
Extend a Object
with the contents of an iterator.
§Examples
use std::collections::HashMap;
use sonic_rs::{json, object, Object, Value};
let mut obj = object![];
let mut map: HashMap<&str, Value> = {
let mut map = HashMap::new();
map.insert("sonic", json!(40));
map.insert("rs", json!(null));
map
};
obj.extend(&map);
assert_eq!(obj, object! {"sonic": 40, "rs": null});
obj.extend(&object! {"object": [1, 2, 3]});
assert_eq!(obj, object! {"sonic": 40, "rs": null, "object": [1, 2, 3]});
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<'a, K: AsRef<str>, V: Clone + Into<Value> + 'a> FromIterator<(K, &'a V)> for Object
impl<'a, K: AsRef<str>, V: Clone + Into<Value> + 'a> FromIterator<(K, &'a V)> for Object
Source§fn from_iter<T: IntoIterator<Item = (K, &'a V)>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = (K, &'a V)>>(iter: T) -> Self
Create a Object
by collecting an iterator of key-value pairs.
§Examples
use std::collections::HashMap;
use sonic_rs::{object, Object, Value};
let mut map = HashMap::new();
map.insert("sonic_rs", 40);
map.insert("json", 2);
let x: Object = map.iter().collect();
assert_eq!(x, object! {"sonic_rs": 40, "json": 2});
let x = Object::from_iter(&object! {"sonic_rs": 40, "json": 2});
assert_eq!(x, object! {"sonic_rs": 40, "json": 2});
Source§impl<'a> IntoIterator for &'a Object
impl<'a> IntoIterator for &'a Object
Source§impl<'a> IntoIterator for &'a mut Object
impl<'a> IntoIterator for &'a mut 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 !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§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)