Skip to main content

JsonValueExt

Trait JsonValueExt 

Source
pub trait JsonValueExt {
Show 19 methods // Required methods fn x_new_object() -> Value; fn x_contains<T: DeserializeOwned>(&self, name_or_pointer: &str) -> bool; fn x_get<T: DeserializeOwned>( &self, name_or_pointer: &str, ) -> Result<T, JsonValueExtError>; fn x_get_as<'a, T: AsType<'a>>( &'a self, name_or_pointer: &str, ) -> Result<T, JsonValueExtError>; fn x_get_object( &self, name_or_pointer: &str, ) -> Result<&Map<String, Value>, JsonValueExtError>; fn x_take<T: DeserializeOwned>( &mut self, name_or_pointer: &str, ) -> Result<T, JsonValueExtError>; fn x_remove<T: DeserializeOwned>( &mut self, name_or_pointer: &str, ) -> Result<T, JsonValueExtError>; fn x_insert<T: Serialize>( &mut self, name_or_pointer: &str, value: T, ) -> Result<(), JsonValueExtError>; fn x_merge(&mut self, other: Value) -> Result<(), JsonValueExtError>; fn x_walk<F>(&mut self, callback: F) -> bool where F: FnMut(&mut Map<String, Value>, &str) -> bool; fn x_pretty(&self) -> Result<String, JsonValueExtError>; // Provided methods fn x_get_str( &self, name_or_pointer: &str, ) -> Result<&str, JsonValueExtError> { ... } fn x_get_i64(&self, name_or_pointer: &str) -> Result<i64, JsonValueExtError> { ... } fn x_get_f64(&self, name_or_pointer: &str) -> Result<f64, JsonValueExtError> { ... } fn x_get_bool( &self, name_or_pointer: &str, ) -> Result<bool, JsonValueExtError> { ... } fn x_get_strs( &self, name_or_pointer: &str, ) -> Result<Vec<&str>, JsonValueExtError> { ... } fn x_get_i64s( &self, name_or_pointer: &str, ) -> Result<Vec<i64>, JsonValueExtError> { ... } fn x_get_f64s( &self, name_or_pointer: &str, ) -> Result<Vec<f64>, JsonValueExtError> { ... } fn x_get_bools( &self, name_or_pointer: &str, ) -> Result<Vec<bool>, JsonValueExtError> { ... }
}
Expand description

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_get: Returns an owned 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.
  • 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_get_strs: Returns a Vec<&str> from a JSON array using either a direct name or a pointer path.
  • x_get_i64s: Returns a Vec<i64> from a JSON array using either a direct name or a pointer path.
  • x_get_f64s: Returns a Vec<f64> from a JSON array using either a direct name or a pointer path.
  • x_get_bools: Returns a Vec<bool> from a JSON array using either a direct name or a pointer path.
  • x_get_object: Returns a reference to the object map at the specified name or pointer path.
  • x_take: Takes a value from a JSON object using a specified name or pointer path, replacing it with Null.
  • x_remove: Removes the value at the specified name or pointer path from the JSON object and returns it, leaving no placeholder in the object (unlike x_take).
  • x_insert: Inserts a new 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 in the JSON value tree and calls the callback function on each.
  • x_pretty: Returns a pretty-printed string representation of the JSON value.

Required Methods§

Source

fn x_new_object() -> Value

Source

fn x_contains<T: DeserializeOwned>(&self, name_or_pointer: &str) -> bool

Source

fn x_get<T: DeserializeOwned>( &self, name_or_pointer: &str, ) -> Result<T, JsonValueExtError>

Returns an owned type T for a given name or pointer path. Note: This will create a clone of the matched Value.

  • name_or_pointer: Can be a direct name or a pointer path (if it starts with ‘/’).
Source

fn x_get_as<'a, T: AsType<'a>>( &'a self, name_or_pointer: &str, ) -> Result<T, JsonValueExtError>

Returns a reference of type T (or a copy for copy types) for a given name or pointer path. Use this one over x_get to avoid string allocation.

  • name_or_pointer: Can be a direct name or a pointer path (if it starts with ‘/’).
Source

fn x_get_object( &self, name_or_pointer: &str, ) -> Result<&Map<String, Value>, JsonValueExtError>

Returns a reference to the object map at the given name or pointer path.

  • name_or_pointer: Can be a direct name or a pointer path (if it starts with ‘/’).
Source

fn x_take<T: DeserializeOwned>( &mut self, name_or_pointer: &str, ) -> Result<T, JsonValueExtError>

Takes the value at the specified name or pointer path and replaces it with Null.

  • name_or_pointer: Can be a direct name or a pointer path (if it starts with ‘/’).
Source

fn x_remove<T: DeserializeOwned>( &mut self, name_or_pointer: &str, ) -> Result<T, JsonValueExtError>

Removes the value at the specified name or pointer path from the JSON object and returns it without leaving a placeholder, unlike x_take.

Source

fn x_insert<T: Serialize>( &mut self, name_or_pointer: &str, value: T, ) -> Result<(), JsonValueExtError>

Inserts a new value of type T at the specified name or pointer path. This method creates missing Value::Object entries as needed.

  • name_or_pointer: Can be a direct name or a pointer path (if it starts with ‘/’).
Source

fn x_merge(&mut self, other: Value) -> Result<(), JsonValueExtError>

Merges another JSON object into this one (shallow merge).

  • If self is not an Object, returns error.
  • If other is Null, does nothing.
  • If other is not an Object, returns error.
Source

fn x_walk<F>(&mut self, callback: F) -> bool
where F: FnMut(&mut Map<String, Value>, &str) -> bool,

Walks through all properties in the JSON value tree and calls the callback function on each.

  • The callback signature is (parent_map, property_name) -> bool.
    • Returns false to stop the traversal; returns true to continue.

Returns:

  • true if the traversal completes without stopping early.
  • false if the traversal was stopped early because the callback returned false.
Source

fn x_pretty(&self) -> Result<String, JsonValueExtError>

Returns a pretty-printed string representation of the JSON value.

Provided Methods§

Source

fn x_get_str(&self, name_or_pointer: &str) -> Result<&str, JsonValueExtError>

Returns a &str if present (shortcut for x_get_as::<&str>(...))

Source

fn x_get_i64(&self, name_or_pointer: &str) -> Result<i64, JsonValueExtError>

Returns an i64 if present (shortcut for x_get_as::<i64>(...))

Source

fn x_get_f64(&self, name_or_pointer: &str) -> Result<f64, JsonValueExtError>

Returns an f64 if present (shortcut for x_get_as::<f64>(...))

Source

fn x_get_bool(&self, name_or_pointer: &str) -> Result<bool, JsonValueExtError>

Returns a bool if present (shortcut for x_get_as::<bool>(...))

Source

fn x_get_strs( &self, name_or_pointer: &str, ) -> Result<Vec<&str>, JsonValueExtError>

Returns a Vec<&str> if present (shortcut for x_get_as::<Vec<&str>>(...))

Source

fn x_get_i64s( &self, name_or_pointer: &str, ) -> Result<Vec<i64>, JsonValueExtError>

Returns a Vec<i64> if present (shortcut for x_get_as::<Vec<i64>>(...))

Source

fn x_get_f64s( &self, name_or_pointer: &str, ) -> Result<Vec<f64>, JsonValueExtError>

Returns a Vec<f64> if present (shortcut for x_get_as::<Vec<f64>>(...))

Source

fn x_get_bools( &self, name_or_pointer: &str, ) -> Result<Vec<bool>, JsonValueExtError>

Returns a Vec<bool> if present (shortcut for x_get_as::<Vec<bool>>(...))

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl JsonValueExt for Value

Source§

fn x_walk<F>(&mut self, callback: F) -> bool
where F: FnMut(&mut Map<String, Value>, &str) -> bool,

Walks through all properties of a JSON value tree and calls the callback function on each property.

  • The callback signature is (parent_map, property_name) -> bool.
    • Return false from the callback to stop the traversal; return true to continue.

Returns:

  • true if the traversal completed to the end without being stopped early.
  • false if the traversal was stopped early because the callback returned false.
Source§

fn x_new_object() -> Value

Source§

fn x_contains<T: DeserializeOwned>(&self, name_or_pointer: &str) -> bool

Source§

fn x_get<T: DeserializeOwned>( &self, name_or_pointer: &str, ) -> Result<T, JsonValueExtError>

Source§

fn x_get_object( &self, name_or_pointer: &str, ) -> Result<&Map<String, Value>, JsonValueExtError>

Source§

fn x_get_as<'a, T: AsType<'a>>( &'a self, name_or_pointer: &str, ) -> Result<T, JsonValueExtError>

Source§

fn x_take<T: DeserializeOwned>( &mut self, name_or_pointer: &str, ) -> Result<T, JsonValueExtError>

Source§

fn x_remove<T: DeserializeOwned>( &mut self, name_or_pointer: &str, ) -> Result<T, JsonValueExtError>

Source§

fn x_insert<T: Serialize>( &mut self, name_or_pointer: &str, value: T, ) -> Result<(), JsonValueExtError>

Source§

fn x_merge(&mut self, other: Value) -> Result<(), JsonValueExtError>

Source§

fn x_pretty(&self) -> Result<String, JsonValueExtError>

Implementors§