Function quickjs_runtime::quickjs_utils::json::parse_q
source · pub fn parse_q(
q_ctx: &QuickJsRealmAdapter,
input: &str,
) -> Result<QuickJsValueAdapter, JsError>
Expand description
Parse a JSON string into an Object please note that JSON.parse requires member names to be enclosed in double quotes so {a: 1} and {‘a’: 1} will both fail {“a”: 1} will parse ok
§Example
use quickjs_runtime::esruntimebuilder::EsRuntimeBuilder;
use quickjs_runtime::quickjs_utils::{json, objects, primitives};
use quickjs_runtime::quickjs_utils::json::parse;
let rt = EsRuntimeBuilder::new().build();
rt.add_to_event_queue_sync(|q_js_rt| {
let q_ctx = q_js_rt.get_main_context();
let parse_res = json::parse_q(q_ctx, "{\"aaa\": 165}");
if parse_res.is_err() {
panic!("could not parse: {}", parse_res.err().unwrap());
}
let obj_ref = parse_res.ok().unwrap();
let a_ref = objects::get_property(q_ctx.context, &obj_ref, "aaa").ok().unwrap();
let i = primitives::to_i32(&a_ref).ok().unwrap();
assert_eq!(165, i);
});
rt.gc_sync();