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

json::from_value expected Value found &Value #93

Closed
linuxaged opened this issue Jun 26, 2015 · 2 comments
Closed

json::from_value expected Value found &Value #93

linuxaged opened this issue Jun 26, 2015 · 2 comments
Labels

Comments

@linuxaged
Copy link

When I try to deserialize an array to vector:

let data: Value = json::from_str(&s).unwrap();
let meshes = data.find("meshes").unwrap();
let mesh_array = meshes.as_array().unwrap();
let mesh = mesh_array[0].as_object().unwrap();

let vertices: Vec<f64> = (json::from_value (mesh.get("vertices").unwrap()) ).unwrap();

get the error:

src/main.rs:17:49: 17:78 error: mismatched types:
 expected `serde::json::value::Value`,
    found `&serde::json::value::Value`
(expected enum `serde::json::value::Value`,
    found &-ptr) [E0308]
src/main.rs:17     let vertices: Vec<f64> = (json::from_value (mesh.get("vertices").unwrap()) ).unwrap();
                                                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

unwrap().clone() works here, but it is very consuming right?
is there a pass-by-ref version json::from_value, how to deal with this?

@erickt
Copy link
Member

erickt commented Jun 26, 2015

There isn't a pass-by-ref because I want to retain the option to move strings out of the maps without copies. Do you need to deserialize into a Value? If not you could instead deserialize into a

#[derive(Deserialize)]
struct Mesh {...}

#[derive(Deserialize)]
struct Object {
   Meshes: Vec<Mesh>,
}

@linuxaged
Copy link
Author

The reason I do deserializing by parsing fields one by one is serde not supporting defaults for missing struct fields or ignore unkown fields

if feature like these was added, deserializing will be much more comfortable and faster

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants