quickjs-rs
quickjs-rs copied to clipboard
Convert JsValue (Serialize, Deserialize) to standart rust types
Is there any way to convert
{"x": Int(1), "y": Int(2)}
into HashMap<String, i32> - {"x": 1, "y": 2}
or
Array([Int(1), Int(2), Int(3)])
into Vec<i32>[1,2,3]
?
Full code:
let context = Context::new().unwrap();
let value = context
.eval_as::<JsValue>(" var x = eval([1,2,3]); JSON.stringify(x); x")
.unwrap();
println!("{:?}", value);
match value {
JsValue::Null => {}
JsValue::Bool(x) => println!("bool = {:?}", x),
JsValue::Int(x) => println!("int = {:?}", x),
JsValue::Float(x) => println!("float = {:?}", x),
JsValue::String(x) => println!("string = {:?}", x),
JsValue::Array(x) => println!("array = {:?}", x),
JsValue::Object(x) => println!("object = {:?}", x),
JsValue::__NonExhaustive => {}
};