Skip to main content

ValueExt

Trait ValueExt 

Source
pub trait ValueExt {
    // Required methods
    fn get_str(&self, field: &str) -> Option<String>;
    fn get_str_or(&self, field: &str, default: &str) -> String;
    fn get_str_required(&self, field: &str) -> Result<String, JacsError>;
    fn get_i64(&self, key: &str) -> Option<i64>;
    fn get_bool(&self, key: &str) -> Option<bool>;
    fn as_string(&self) -> String;
    fn get_path(&self, path: &[&str]) -> Option<&Value>;
    fn get_path_str(&self, path: &[&str]) -> Option<String>;
    fn get_path_str_or(&self, path: &[&str], default: &str) -> String;
    fn get_path_str_required(&self, path: &[&str]) -> Result<String, JacsError>;
    fn get_path_array(&self, path: &[&str]) -> Option<&Vec<Value>>;
    fn get_path_array_required(
        &self,
        path: &[&str],
    ) -> Result<&Vec<Value>, JacsError>;
}
Expand description

Extension trait for serde_json::Value providing convenient accessor methods.

These helpers reduce boilerplate for common JSON access patterns like:

  • value.get("field").and_then(|v| v.as_str()) -> value.get_str("field")
  • value["a"]["b"].as_str().unwrap_or("") -> value.get_path_str_or(&["a", "b"], "")
  • value["a"]["b"].as_str().ok_or_else(...) -> value.get_path_str_required(&["a", "b"])

Required Methods§

Source

fn get_str(&self, field: &str) -> Option<String>

Gets a string field, returning Some(String) if present and a string.

Source

fn get_str_or(&self, field: &str, default: &str) -> String

Gets a string field, returning the provided default if missing or not a string.

Source

fn get_str_required(&self, field: &str) -> Result<String, JacsError>

Gets a required string field, returning an error if missing.

Source

fn get_i64(&self, key: &str) -> Option<i64>

Gets an i64 field, returning Some(i64) if present and numeric.

Source

fn get_bool(&self, key: &str) -> Option<bool>

Gets a bool field, returning Some(bool) if present and boolean.

Source

fn as_string(&self) -> String

Serializes the value to a pretty-printed JSON string.

Source

fn get_path(&self, path: &[&str]) -> Option<&Value>

Traverses a path of keys and returns the value at that path.

§Example
let sig = value.get_path(&["jacsSignature", "publicKeyHash"]);
Source

fn get_path_str(&self, path: &[&str]) -> Option<String>

Traverses a path of keys and returns the string value at that path.

§Example
let hash = value.get_path_str(&["jacsSignature", "publicKeyHash"]);
Source

fn get_path_str_or(&self, path: &[&str], default: &str) -> String

Traverses a path and returns the string value, or a default if not found.

§Example
let agent_id = value.get_path_str_or(&["jacsSignature", "agentID"], "");
Source

fn get_path_str_required(&self, path: &[&str]) -> Result<String, JacsError>

Traverses a path and returns a required string value, or an error.

The error message includes the full dotted path for debugging.

§Example
let hash = value.get_path_str_required(&["jacsSignature", "publicKeyHash"])?;
Source

fn get_path_array(&self, path: &[&str]) -> Option<&Vec<Value>>

Traverses a path and returns the array value at that path.

Source

fn get_path_array_required( &self, path: &[&str], ) -> Result<&Vec<Value>, JacsError>

Traverses a path and returns a required array, or an error.

Implementations on Foreign Types§

Source§

impl ValueExt for Value

Source§

fn as_string(&self) -> String

Source§

fn get_str(&self, field: &str) -> Option<String>

Source§

fn get_str_or(&self, field: &str, default: &str) -> String

Source§

fn get_str_required(&self, field: &str) -> Result<String, JacsError>

Source§

fn get_i64(&self, key: &str) -> Option<i64>

Source§

fn get_bool(&self, key: &str) -> Option<bool>

Source§

fn get_path(&self, path: &[&str]) -> Option<&Value>

Source§

fn get_path_str(&self, path: &[&str]) -> Option<String>

Source§

fn get_path_str_or(&self, path: &[&str], default: &str) -> String

Source§

fn get_path_str_required(&self, path: &[&str]) -> Result<String, JacsError>

Source§

fn get_path_array(&self, path: &[&str]) -> Option<&Vec<Value>>

Source§

fn get_path_array_required( &self, path: &[&str], ) -> Result<&Vec<Value>, JacsError>

Implementors§