#serde-json #utilities #value

value-ext

Serde Json Value Extension Trait and Json Utilities

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

Download history 2303/week @ 2025-12-21 2294/week @ 2025-12-28 3948/week @ 2026-01-04 2645/week @ 2026-01-11 2387/week @ 2026-01-18 2180/week @ 2026-01-25 2545/week @ 2026-02-01 4247/week @ 2026-02-08 3890/week @ 2026-02-15 5247/week @ 2026-02-22 5506/week @ 2026-03-01 6265/week @ 2026-03-08 6414/week @ 2026-03-15 5329/week @ 2026-03-22 7449/week @ 2026-03-29 6777/week @ 2026-04-05

26,811 downloads per month
Used in 66 crates (4 directly)

MIT/Apache

22KB
368 lines

Value Extension Traits

LLM API Reference

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 type T.
  • x_get_as<T>: Returns a reference or copy (via AsType) to avoid unnecessary allocations.
  • x_get_str, x_get_i64, x_get_f64, x_get_bool: Type-specific shortcuts for x_get_as.
  • x_insert: Inserts a value at a path, creating parent objects if necessary.
  • x_take: Replaces the value at a path with Null and 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_pretty method 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