7 releases
Uses new Rust 2024
| 0.1.3 | Feb 8, 2026 |
|---|---|
| 0.1.2 | Feb 20, 2025 |
| 0.1.1 | Nov 11, 2024 |
| 0.0.3 | Sep 22, 2024 |
#215 in Data structures
26,811 downloads per month
Used in 66 crates
(4 directly)
22KB
368 lines
Value Extension Traits
value-ext is a Rust crate that provides the JsonValueExt extension trait for serde_json::Value. It offers a more ergonomic and type-safe way to interact with JSON data, simplifying common tasks like getting, taking, inserting, and traversing JSON structures using direct keys or JSON Pointer paths.
Methods Overview
x_get<T>: Returns an owned value of typeT.x_get_as<T>: Returns a reference or copy (viaAsType) to avoid unnecessary allocations.x_get_str,x_get_i64,x_get_f64,x_get_bool: Type-specific shortcuts forx_get_as.x_insert: Inserts a value at a path, creating parent objects if necessary.x_take: Replaces the value at a path withNulland returns the original.x_remove: Removes the property entirely and returns the value.x_merge: Performs a shallow merge of another JSON object.x_walk: Traverses the JSON tree with a callback(parent_map, key) -> bool.x_pretty: Returns a formatted JSON string.
This trait enhances the serde_json::Value API by providing a more fluid interface for dynamic JSON manipulation in Rust.
Key Features
- Ergonomic Getters: Retrieve values as owned types (
x_get) or zero-copy references (x_get_as,x_get_str, etc.). - Pointer Support: Use standard JSON Pointers (e.g.,
/path/to/value) or direct property names. - Mutations: Easily insert (
x_insert), take (x_take), or remove (x_remove) values. - Deep Traversal: Walk through the entire JSON tree with
x_walk. - Pretty Printing: Convenient
x_prettymethod for debugging and logging.
Usage
Add value-ext to your Cargo.toml and import the trait.
use serde_json::{json, Value};
use value_ext::JsonValueExt;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut data = json!({
"project": "value-ext",
"settings": {
"retries": 3
}
});
// Get a value using a JSON Pointer
let retries: i64 = data.x_get("/settings/retries")?;
assert_eq!(retries, 3);
// Get a string reference (zero-copy)
let name = data.x_get_str("project")?;
assert_eq!(name, "value-ext");
// Insert a new nested value (creates parents if missing)
data.x_insert("/settings/timeout", 30)?;
// Remove a value and return it
let project: String = data.x_remove("project")?;
// Pretty print
println!("{}", data.x_pretty()?);
Ok(())
}
Dependencies
~0.5–1.5MB
~30K SLoC