Skip to main content

JsonPaths

Trait JsonPaths 

Source
pub trait JsonPaths {
    // Required method
    fn paths_and_values(&self) -> BTreeMap<String, JsonScalar>;
}
Expand description

Flattens a JSON value into dotted paths and leaf values.

This is the Rust form of PathsAndValuesDictionary.

Required Methods§

Source

fn paths_and_values(&self) -> BTreeMap<String, JsonScalar>

Walk this JSON value and return every leaf as path -> scalar.

Object keys and array indexes are joined with .. A scalar root (string, number, bool, or null) is stored under the empty path "". Empty objects and empty arrays contribute no paths, because they have no leaves.

§Examples
use json_traits::{JsonPaths, JsonScalar};
use serde_json::json;

let document = json!({
    "prop1": { "prop2": "value" },
    "contacts": [
        { "info": { "name": "Stewie" } },
        { "info": { "number": 12 } },
        { "info": { "isAwesome": true } }
    ]
});

let paths = document.paths_and_values();
assert_eq!(paths.get("prop1.prop2"), Some(&JsonScalar::from("value")));
assert_eq!(
    paths.get("contacts.1.info.number"),
    Some(&JsonScalar::from_integer(12))
);
assert_eq!(
    paths.get("contacts.2.info.isAwesome"),
    Some(&JsonScalar::from(true))
);

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl JsonPaths for Value

Implementors§