pub trait DotPaths {
// Required methods
fn dot_get<T>(&self, path: &str) -> Result<Option<T>>
where T: DeserializeOwned;
fn dot_has_checked(&self, path: &str) -> Result<bool>;
fn dot_get_mut(&mut self, path: &str) -> Result<&mut Value>;
fn dot_set<T>(&mut self, path: &str, value: T) -> Result<()>
where T: Serialize;
fn dot_replace<NEW, OLD>(
&mut self,
path: &str,
value: NEW,
) -> Result<Option<OLD>>
where NEW: Serialize,
OLD: DeserializeOwned;
fn dot_take<T>(&mut self, path: &str) -> Result<Option<T>>
where T: DeserializeOwned;
// Provided methods
fn dot_get_or<T>(&self, path: &str, def: T) -> Result<T>
where T: DeserializeOwned { ... }
fn dot_get_or_default<T>(&self, path: &str) -> Result<T>
where T: DeserializeOwned + Default { ... }
fn dot_has(&self, path: &str) -> bool { ... }
fn dot_remove(&mut self, path: &str) -> Result<()> { ... }
}Expand description
Access and mutate nested JSON elements by dotted paths
The path is composed of keys separated by dots, e.g. foo.bar.1.
All symbols in a path may be escaped by backslash (\) to have them treated literally,
e.g. to access a key containing a period.
Arrays are indexed by numeric strings or special keys (see dot_get() and dot_set()).
This trait is implemented for serde_json::Value, specifically the
Map, Array, and Null variants. Empty path can also be used to access a scalar.
Methods on this trait do not panic, errors are passed to the caller.
Required Methods§
Sourcefn dot_get<T>(&self, path: &str) -> Result<Option<T>>where
T: DeserializeOwned,
fn dot_get<T>(&self, path: &str) -> Result<Option<T>>where
T: DeserializeOwned,
Get an item by path, if present.
If the element does not exist or is null, None is returned.
Accessing array index out of range raises Err(BadIndex).
The path does not need to reach a leaf node, i.e. it is possible to extract a subtree of a JSON object this way.
§Special symbols
>… last element of an array<… first element of an array (same as0)
Sourcefn dot_has_checked(&self, path: &str) -> Result<bool>
fn dot_has_checked(&self, path: &str) -> Result<bool>
Check if a value exists under a dotted path. Returns error if the path is invalid, a string key was used to index into an array.
§Special symbols
>… last element of an array<… first element of an array (same as0)
Sourcefn dot_get_mut(&mut self, path: &str) -> Result<&mut Value>
fn dot_get_mut(&mut self, path: &str) -> Result<&mut Value>
Get a mutable reference to an item
If the path does not exist but a value on the path can be created (i.e. because the path
reaches null, array or object), a null value is inserted in that location (creating
its parent nodes as needed) and a mutable reference to this new null node is returned.
The path does not need to reach a leaf node, i.e. it is possible to extract a subtree of a JSON object this way.
§Special keys
>… last element of an array<… first element of an array (same as0)
Sourcefn dot_set<T>(&mut self, path: &str, value: T) -> Result<()>where
T: Serialize,
fn dot_set<T>(&mut self, path: &str, value: T) -> Result<()>where
T: Serialize,
Insert an item by path. The original value is dropped, if any.
§Special symbols
Arrays can be modified using special keys in the path:
+or>>… append-or<<… prepend>n… insert after an indexn<n… insert before an indexn>… last element of an array<… first element of an array (same as0)
Sourcefn dot_replace<NEW, OLD>(
&mut self,
path: &str,
value: NEW,
) -> Result<Option<OLD>>where
NEW: Serialize,
OLD: DeserializeOwned,
fn dot_replace<NEW, OLD>(
&mut self,
path: &str,
value: NEW,
) -> Result<Option<OLD>>where
NEW: Serialize,
OLD: DeserializeOwned,
Replace a value by path with a new value. The value types do not have to match.
Returns Ok(None) if the path was previously empty or null.
§Special keys
>… last element of an array<… first element of an array (same as0)
Sourcefn dot_take<T>(&mut self, path: &str) -> Result<Option<T>>where
T: DeserializeOwned,
fn dot_take<T>(&mut self, path: &str) -> Result<Option<T>>where
T: DeserializeOwned,
Get an item using a path, removing it from the object.
Value becomes null when taken by an empty path, map entry is removed,
and array item is extracted, shifting the remainder forward.
Returns Ok(None) if the path was previously empty or null.
§Special keys
>… last element of an array<… first element of an array (same as0)
Provided Methods§
Sourcefn dot_get_or<T>(&self, path: &str, def: T) -> Result<T>where
T: DeserializeOwned,
fn dot_get_or<T>(&self, path: &str, def: T) -> Result<T>where
T: DeserializeOwned,
Get an item by path, or a default value if it does not exist.
This method is best suited for JSON objects (Map) or nullable fields.
See dot_get() for more details.
Sourcefn dot_get_or_default<T>(&self, path: &str) -> Result<T>where
T: DeserializeOwned + Default,
fn dot_get_or_default<T>(&self, path: &str) -> Result<T>where
T: DeserializeOwned + Default,
Get an item, or a default value using the Default trait
This method is best suited for JSON objects (Map) or nullable fields.
See dot_get() for more details.
Sourcefn dot_has(&self, path: &str) -> bool
fn dot_has(&self, path: &str) -> bool
Check if a value exists under a dotted path. Returns false also when the path is invalid.
Use dot_has_checked if you want to distinguish non-existent values from path errors.
§Special symbols
>… last element of an array<… first element of an array (same as0)
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.