#serde-json #json-object #value #utilities

value-ext

Serde Json Value Extension Trait (more Value type later)

5 releases

0.1.1 Nov 11, 2024
0.1.0 Nov 10, 2024
0.0.3 Sep 22, 2024
0.0.2 Sep 19, 2024
0.0.1 Sep 4, 2024

#424 in Data structures

Download history 76/week @ 2024-10-19 28/week @ 2024-10-26 41/week @ 2024-11-02 297/week @ 2024-11-09 128/week @ 2024-11-16 91/week @ 2024-11-23 260/week @ 2024-11-30 141/week @ 2024-12-07 164/week @ 2024-12-14 35/week @ 2024-12-21 98/week @ 2024-12-28 211/week @ 2025-01-04 118/week @ 2025-01-11 246/week @ 2025-01-18 135/week @ 2025-01-25 126/week @ 2025-02-01

647 downloads per month
Used in 5 crates (2 directly)

MIT/Apache

19KB
257 lines

Value Extension Traits

An extension trait for working with JSON values in a more convenient way. JsonValueExt offers convenient methods for interacting with serde_json::Value objects, simplifying tasks like getting, taking, inserting, traversing, and pretty-printing JSON data while ensuring type safety with Serde's serialization and deserialization.

Provided Methods

  • x_new_object: Creates a new Value::Object.
  • x_get: Returns a value of a specified type T from a JSON object using either a direct name or a pointer path.
  • x_get_as: Returns a reference of a specified type T from a JSON object using either a direct name or a pointer path, avoiding allocations for types that implement AsType.
  • x_get_str: Returns a &str from a JSON object using either a direct name or a pointer path.
  • x_get_i64: Returns an i64 from a JSON object using either a direct name or a pointer path.
  • x_get_f64: Returns an f64 from a JSON object using either a direct name or a pointer path.
  • x_get_bool: Returns a bool from a JSON object using either a direct name or a pointer path.
  • x_take: Takes a value from a JSON object using a specified name or pointer path, replacing it with Null.
  • x_insert: Inserts a value of type T into a JSON object at the specified name or pointer path, creating any missing objects along the way.
  • x_walk: Traverses all properties within the JSON value tree, applying a user-provided callback function to each property.
  • x_pretty: Returns a pretty-printed string representation of the JSON value.

Usage

This trait is intended to be used with serde_json::Value objects. It is particularly useful when you need to manipulate JSON structures dynamically or when the structure of the JSON is not known at compile time.

use serde_json::{Value, Map};
use serde::de::DeserializeOwned;
use serde::Serialize;
use your_crate::JsonValueExt;

fn example_usage(json: &mut Value) -> Result<(), Box<dyn std::error::Error>> {
    // Create a new object
    let new_object = Value::x_new_object();
    
    // Get a value from JSON
    let name: String = json.x_get("/name")?;

    // Take a value from JSON, replacing it with `Null`
    let age: u32 = json.x_take("age")?;

    // Insert a new value into JSON
    json.x_insert("city", "New York")?;

    // Walk through the JSON properties
    json.x_walk(|parent_map, property_name| {
        println!("Property: {}", property_name);
        true // Continue traversal
    });

    // Get a pretty-printed JSON string
    let pretty_json = json.x_pretty()?;
    println!("{}", pretty_json);

    Ok(())
}

This trait enhances the serde_json::Value API by adding more type-safe and convenient methods for manipulating JSON data in Rust.

Dependencies

~0.7–1.7MB
~35K SLoC