The Value type provides an owned JSON DOM that allows for easy access and modification of data using indexing. It is slower than BorrowedValue but avoids lifetime complexities.
- Array Access: Use
usize indexing to access or mutate array elements. - Object Access: Use
&str indexing to access or mutate object values by key.
Note: Indexing with [] will panic if the index is out of bounds or the key does not exist. For safe access, use the ValueTrait methods (like as_array, as_object, etc.) or the get methods provided by the traits.
use simd_json::{OwnedValue, json};
use simd_json::prelude::*;
// Access via array indexes
let mut a = json!([1, 2, 3]);
assert_eq!(a[1], 2);
a[1] = 42.into();
assert_eq!(a[1], 42);
// Access via object keys
let mut b = json!({"key": "not the value"});
assert_eq!(b["key"], "not the value");
b["key"] = "value".into();
assert_eq!(b["key"], "value");