Function quickjs_runtime::quickjs_utils::json::stringify_q

source ·
pub fn stringify_q(
    q_ctx: &QuickJsRealmAdapter,
    input: &QuickJsValueAdapter,
    opt_space: Option<QuickJsValueAdapter>,
) -> Result<QuickJsValueAdapter, JsError>
Expand description

Stringify an Object in script

§Example

use quickjs_runtime::builder::QuickJsRuntimeBuilder;
use quickjs_runtime::quickjs_utils::{json, objects, primitives};
let rt = QuickJsRuntimeBuilder::new().build();
rt.exe_rt_task_in_event_loop(|q_js_rt| {
    let q_ctx = q_js_rt.get_main_realm();
    let obj_ref = objects::create_object_q(q_ctx).ok().unwrap();
    objects::set_property_q(q_ctx, &obj_ref, "a", &primitives::from_i32(741)).ok().unwrap();
    let str_ref = json::stringify_q(q_ctx, &obj_ref, None).ok().unwrap();
    let str_str = primitives::to_string_q(q_ctx, &str_ref).ok().unwrap();
    assert_eq!("{\"a\":741}", str_str);
});
rt.gc_sync();