Skip to main content

Crate json_traits

Crate json_traits 

Source
Expand description

Server-side helper for clients that address JSON the way leaf-validator does and PATCH individual leaves.

Flatten a document into dotted paths, diff two documents, and match paths against patterns. This is the Rust port of JsonElementExtensions. C# extension methods are traits here. Import a trait before calling its methods. That import is what adds paths_and_values to serde_json::Value, in the same way a using brings an extension method into scope.

use json_traits::{DiffJson, JsonPaths, JsonScalar, PathPattern};
use serde_json::json;

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

let paths = document.paths_and_values();
assert_eq!(paths.get("prop1.prop2"), Some(&JsonScalar::from("value")));
assert!("contacts.0.info.name".is_supported_by(["contacts.*.info.name"]));

Dots inside an object key are not escaped. The key "a.b" and the nested object {"a": {"b": ...}} produce the same path.

Modules§

prelude
The traits that add path, diff, and pattern methods to existing types.

Enums§

JsonScalar
A leaf JSON value reached by a dotted path.

Traits§

DiffJson
Compares JSON values, or maps of paths, by leaf value.
JsonPaths
Flattens a JSON value into dotted paths and leaf values.
PathPattern
Matches dotted paths against patterns.