sonic_rs/index.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
use crate::{
util::{private::Sealed, reborrow::DormantMutRef},
JsonValueMutTrait, JsonValueTrait, PointerNode, Value,
};
impl<I> std::ops::Index<I> for Value
where
I: Index,
{
type Output = Value;
/// Index into an array `Value` using the syntax `value[0]` and index into an
/// object `Value` using the syntax `value["k"]`.
///
/// Returns a null `Value` if the `Value` type does not match the index, or the
/// index does not exist in the array or object.
///
/// For retrieving deeply nested values, you should have a look at the `Value::pointer` method.
///
/// # Examples
///
/// ```
/// use sonic_rs::{json, pointer, JsonValueTrait};
///
/// let data = json!({
/// "x": {
/// "y": ["z", "zz"]
/// }
/// });
///
/// assert_eq!(data["x"]["y"], json!(["z", "zz"]));
/// assert_eq!(data["x"]["y"][0], json!("z"));
///
/// assert_eq!(data["a"], json!(null)); // returns null for undefined values
/// assert_eq!(data["a"]["b"], json!(null)); // does not panic
///
/// // use pointer for retrieving nested values
/// assert_eq!(data.pointer(&pointer!["x", "y", 0]).unwrap(), &json!("z"));
/// ```
#[inline]
fn index(&self, index: I) -> &Value {
static NULL: Value = Value::new();
index.value_index_into(self).unwrap_or(&NULL)
}
}
impl<I: Index> std::ops::IndexMut<I> for Value {
/// Write the index of a mutable `Value`, and use the syntax `value[0] = ...`
/// in an array and `value["k"] = ...` in an object.
///
/// If the index is a number, the value must be an array of length bigger
/// than the index. Indexing into a value that is not an array or an array
/// that is too small will panic.
///
/// If the index is a string, the value must be an object or null which is
/// treated like an empty object. If the key is not already present in the
/// object, it will be inserted with a value of null. Indexing into a value
/// that is neither an object nor null will panic.
///
/// # Examples
///
/// ```
/// # use sonic_rs::json;
/// # use sonic_rs::object;
/// let mut data = json!({ "x": 0, "z": null });
///
/// // replace an existing key
/// data["x"] = json!(1);
///
/// // insert a new key
/// data["y"] = json!([1, 2, 3]);
///
/// // replace an array value
/// data["y"][0] = json!(true);
///
/// // inserted a deeply nested key
/// data["a"]["b"]["c"]["d"] = json!(true);
///
/// //insert an key in a null value
/// data["z"]["zz"] = json!("insert in null");
///
/// data["z"]["zz1"] = object!{}.into();
///
///
/// assert_eq!(data, json!({
/// "x": 1,
/// "y": [true, 2, 3],
/// "a": { "b": {"c": {"d": true}}},
/// "z": {"zz": "insert in null", "zz1": {}}
/// }));
/// ```
#[inline]
fn index_mut(&mut self, index: I) -> &mut Value {
index.index_or_insert(self)
}
}
/// An indexing trait for JSON.
pub trait Index: Sealed {
/// Return None if the index is not already in the array or object.
#[doc(hidden)]
fn value_index_into<'v>(&self, v: &'v Value) -> Option<&'v Value>;
/// Return None if the key is not already in the array or object.
#[doc(hidden)]
fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value>;
/// Panic if array index out of bounds. If key is not already in the object,
/// insert it with a value of null. Panic if Value is a type that cannot be
/// indexed into, except if Value is null then it can be treated as an empty
/// object.
#[doc(hidden)]
fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value;
#[doc(hidden)]
fn as_key(&self) -> Option<&str> {
None
}
#[doc(hidden)]
fn as_index(&self) -> Option<usize> {
None
}
}
impl Index for usize {
fn value_index_into<'v>(&self, v: &'v Value) -> Option<&'v Value> {
if !v.is_array() {
return None;
}
v.get_index(*self)
}
fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value> {
if !v.is_array() {
return None;
}
v.get_index_mut(*self)
}
fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value {
let typ = v.get_type();
let len = v.len();
v.as_array_mut()
.unwrap_or_else(|| panic!("cannot access index in non-array value type {:?}", typ))
.0
.get_index_mut(*self)
.unwrap_or_else(|| panic!("index {} out of bounds (len: {})", *self, len))
}
fn as_index(&self) -> Option<usize> {
Some(*self)
}
}
macro_rules! impl_str_index {
($($t: ty),*) => {
$(
impl Index for &$t {
#[inline]
fn value_index_into<'v>(&self, v: &'v Value) -> Option<&'v Value> {
if !v.is_object() {
return None;
}
v.get_key(*self)
}
#[inline]
fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value> {
if !v.is_object() {
return None;
}
v.get_key_mut(*self).map(|v| v.0)
}
#[inline]
fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value {
if v.is_null() {
*v = Value::new_object_with(8);
}
let typ = v.get_type();
let (obj, mut dormant_obj) = DormantMutRef::new(v);
obj.as_object_mut()
.expect(&format!("cannot access key in non-object value {:?}", typ))
.0
.get_key_mut(*self).map_or_else(|| {
let o = unsafe { dormant_obj.reborrow() };
let inserted = o.append_pair((Into::<Value>::into((*self)), Value::new_null()));
&mut inserted.1
}, |v| v.0)
}
#[inline]
fn as_key(&self) -> Option<&str> {
Some(self.as_ref())
}
}
)*
};
}
impl_str_index!(str, String, faststr::FastStr);
impl Index for PointerNode {
#[inline]
fn value_index_into<'v>(&self, v: &'v Value) -> Option<&'v Value> {
match self {
PointerNode::Index(i) => i.value_index_into(v),
PointerNode::Key(k) => k.value_index_into(v),
}
}
#[inline]
fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value> {
match self {
PointerNode::Index(i) => i.index_into_mut(v),
PointerNode::Key(k) => k.index_into_mut(v),
}
}
#[inline]
fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value {
match self {
PointerNode::Index(i) => i.index_or_insert(v),
PointerNode::Key(k) => k.index_or_insert(v),
}
}
#[inline]
fn as_index(&self) -> Option<usize> {
match self {
PointerNode::Index(i) => Some(*i),
PointerNode::Key(_) => None,
}
}
#[inline]
fn as_key(&self) -> Option<&str> {
match self {
PointerNode::Index(_) => None,
PointerNode::Key(k) => Some(k.as_ref()),
}
}
}
impl<T> Index for &T
where
T: ?Sized + Index,
{
#[inline]
fn value_index_into<'v>(&self, v: &'v Value) -> Option<&'v Value> {
(**self).value_index_into(v)
}
#[inline]
fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value> {
(**self).index_into_mut(v)
}
#[inline]
fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value {
(**self).index_or_insert(v)
}
#[inline]
fn as_index(&self) -> Option<usize> {
(**self).as_index()
}
#[inline]
fn as_key(&self) -> Option<&str> {
(**self).as_key()
}
}