Skip to main content

to_value

Function to_value 

Source
pub fn to_value<'facet, T: Facet<'facet>>(
    value: &T,
) -> Result<Value, SerializeError<ToValueError>>
Expand description

Serialize a value implementing Facet into a Value.

This is the main entry point for converting a typed Rust value into a dynamic Value that can be inspected, modified, or serialized to various formats.

ยงExample

use facet::Facet;
use facet_value::to_value;

#[derive(Facet)]
struct Point {
    x: i32,
    y: i32,
}

let point = Point { x: 10, y: 20 };
let value = to_value(&point).unwrap();

// Access fields dynamically
let obj = value.as_object().unwrap();
assert_eq!(obj.get("x").unwrap().as_number().unwrap().to_i64(), Some(10));
assert_eq!(obj.get("y").unwrap().as_number().unwrap().to_i64(), Some(20));