[][src]Trait json_dotpath::DotPaths

pub trait DotPaths {
    fn dot_get<T>(&self, path: &str) -> Option<T>
    where
        T: DeserializeOwned
;
fn dot_get_mut(&mut self, path: &str) -> Option<&mut Value>;
fn dot_set<T>(&mut self, path: &str, value: T)
    where
        T: Serialize
;
fn dot_replace<T, U>(&mut self, path: &str, value: T) -> Option<U>
    where
        T: Serialize,
        U: DeserializeOwned
;
fn dot_take<T>(&mut self, path: &str) -> Option<T>
    where
        T: DeserializeOwned
; fn dot_get_or<T>(&self, path: &str, def: T) -> T
    where
        T: DeserializeOwned
, { ... }
fn dot_get_or_default<T>(&self, path: &str) -> T
    where
        T: DeserializeOwned + Default
, { ... }
fn dot_remove(&mut self, path: &str) -> bool { ... } }

Access and mutate nested JSON elements by dotted paths

The path is composed of keys separated by dots, e.g. foo.bar.1.

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.

Required methods

fn dot_get<T>(&self, path: &str) -> Option<T> where
    T: DeserializeOwned

Get an item by path, if present.

JSON null becomes None, same as unpopulated path.

Special keys

Arrays can be indexed by special keys for reading:

  • > ... last element

Panics

  • If the path attempts to index into a scalar (e.g. "foo.bar" in {"foo": 123})
  • If the path uses invalid key in an array or map

fn dot_get_mut(&mut self, path: &str) -> Option<&mut Value>

Get a mutable reference to an item

Special keys

see dot_get()

Panics

see dot_get()

fn dot_set<T>(&mut self, path: &str, value: T) where
    T: Serialize

Insert an item by path.

Special keys

Arrays can be indexed by special keys:

  • + or > ... append
  • - or < ... prepend
  • >n ... insert after an index n
  • <n ... insert before an index n

Panics

see dot_get()

fn dot_replace<T, U>(&mut self, path: &str, value: T) -> Option<U> where
    T: Serialize,
    U: DeserializeOwned

Replace a value by path with a new value. The value types do not have to match.

Panics

see dot_get()

fn dot_take<T>(&mut self, path: &str) -> Option<T> where
    T: DeserializeOwned

Get an item using a path, removing it from the store. If no item was stored under this path, then None is returned.

Panics

see dot_get()

Loading content...

Provided methods

fn dot_get_or<T>(&self, path: &str, def: T) -> T where
    T: DeserializeOwned

Get an item, or a default value.

Special keys

see dot_get()

Panics

see dot_get()

fn dot_get_or_default<T>(&self, path: &str) -> T where
    T: DeserializeOwned + Default

Get an item, or a default value using the Default trait

Special keys

see dot_get()

Panics

see dot_get()

fn dot_remove(&mut self, path: &str) -> bool

Remove an item matching a key. Returns true if any item was removed.

Panics

see dot_get()

Loading content...

Implementations on Foreign Types

impl DotPaths for Value[src]

impl DotPaths for Map<String, Value>[src]

impl DotPaths for Vec<Value>[src]

Loading content...

Implementors

Loading content...