pub trait JsonValueMutTrait {
type ValueType;
type ObjectType;
type ArrayType;
// Required methods
fn as_object_mut(&mut self) -> Option<&mut Self::ObjectType>;
fn as_array_mut(&mut self) -> Option<&mut Self::ArrayType>;
fn pointer_mut<P: IntoIterator>(
&mut self,
path: P,
) -> Option<&mut Self::ValueType>
where P::Item: Index;
fn get_mut<I: Index>(&mut self, index: I) -> Option<&mut Self::ValueType>;
}
Expand description
A trait for all mutable JSON values. Used by mutable Value
.
The Option<V: JsonValueMutTrait>
and Result<V: JsonValueMutTrait, E>
also implement this
trait. The Option::None
or Result::Err(_)
will be viewed as a null value.
Required Associated Types§
Required Methods§
Sourcefn as_object_mut(&mut self) -> Option<&mut Self::ObjectType>
fn as_object_mut(&mut self) -> Option<&mut Self::ObjectType>
Returns the mutable object if self
is an object
.
§Examples
use sonic_rs::{JsonValueMutTrait, json};
use sonic_rs::Value;
let mut value: Value = sonic_rs::from_str(r#"{"a": 1, "b": true}"#).unwrap();
let obj = value.as_object_mut().unwrap();
obj["a"] = json!(2);
assert_eq!(value, json!({"a": 2, "b": true}));
Sourcefn as_array_mut(&mut self) -> Option<&mut Self::ArrayType>
fn as_array_mut(&mut self) -> Option<&mut Self::ArrayType>
Returns the mutable array if self
is an array
.
§Examples
use sonic_rs::{json, JsonValueMutTrait, Value};
let mut value: Value = sonic_rs::from_str(r#"[1, 2, 3]"#).unwrap();
let arr = value.as_array_mut().unwrap();
arr[0] = json!(2);
assert_eq!(value, json!([2, 2, 3]));
Sourcefn pointer_mut<P: IntoIterator>(
&mut self,
path: P,
) -> Option<&mut Self::ValueType>
fn pointer_mut<P: IntoIterator>( &mut self, path: P, ) -> Option<&mut Self::ValueType>
Looks up a value by a path.
The path is an iterator of multiple keys or indexes. It can be a &[&str]
, &[usize]
or a JsonPointer
.
§Examples
let data = json!({
"x": {
"y": ["z", "zz"]
}
});
assert_eq!(data.pointer(&["x", "y"] ).unwrap(), &json!(["z", "zz"]));
assert_eq!(data.pointer(&pointer!["x", "y", 1] ).unwrap(), &json!("zz"));
assert_eq!(data.pointer(&["a", "b"]), None);
Sourcefn get_mut<I: Index>(&mut self, index: I) -> Option<&mut Self::ValueType>
fn get_mut<I: Index>(&mut self, index: I) -> Option<&mut Self::ValueType>
Mutably index into a JSON array or map. A string-like index can be used to access a value in a map, and a usize index can be used to access an element of an array.
Returns None
if the type of self
does not match the type of the
index, for example if the index is a string and self
is an array or a
number. Also returns None
if the given key does not exist in the map
or the given index is not within the bounds of the array.
use sonic_rs::{json, JsonValueMutTrait};
let mut object = json!({ "A": 65, "B": 66, "C": 67 });
*object.get_mut("A").unwrap() = json!(69);
let mut array = json!([ "A", "B", "C" ]);
*array.get_mut(2).unwrap() = json!("D");
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.