Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert JsValue (Serialize, Deserialize) to standart rust types #77

Open
nickmurr opened this issue Aug 15, 2020 · 0 comments
Open

Convert JsValue (Serialize, Deserialize) to standart rust types #77

nickmurr opened this issue Aug 15, 2020 · 0 comments

Comments

@nickmurr
Copy link

nickmurr commented Aug 15, 2020

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 => {}
    };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant