json_traits/lib.rs
1//! Server-side helper for clients that address JSON the way
2//! [leaf-validator](https://www.npmjs.com/package/leaf-validator) does and
3//! PATCH individual leaves.
4//!
5//! Flatten a document into dotted paths, diff two documents, and match paths
6//! against patterns. This is the Rust port of
7//! [JsonElementExtensions](https://github.com/stewie1570/JsonElementExtensions).
8//! C# extension methods are traits here. Import a trait before calling its
9//! methods. That import is what adds `paths_and_values` to
10//! [`serde_json::Value`], in the same way a `using` brings an extension method
11//! into scope.
12//!
13//! ```
14//! use json_traits::{DiffJson, JsonPaths, JsonScalar, PathPattern};
15//! use serde_json::json;
16//!
17//! let document = json!({
18//! "prop1": { "prop2": "value" },
19//! "contacts": [{ "info": { "name": "Stewie" } }]
20//! });
21//!
22//! let paths = document.paths_and_values();
23//! assert_eq!(paths.get("prop1.prop2"), Some(&JsonScalar::from("value")));
24//! assert!("contacts.0.info.name".is_supported_by(["contacts.*.info.name"]));
25//! ```
26//!
27//! Dots inside an object key are not escaped. The key `"a.b"` and the nested
28//! object `{"a": {"b": ...}}` produce the same path.
29
30mod diff;
31mod flatten;
32mod path_pattern;
33mod scalar;
34
35pub use diff::DiffJson;
36pub use flatten::JsonPaths;
37pub use path_pattern::PathPattern;
38pub use scalar::JsonScalar;
39
40/// The traits that add path, diff, and pattern methods to existing types.
41pub mod prelude {
42 pub use crate::{DiffJson, JsonPaths, PathPattern};
43}